diff options
author | Leo Kettmeir <crowlkats@toaxl.com> | 2023-01-16 01:30:52 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-16 00:30:52 +0000 |
commit | df4d0c55c09a1dc6f227b0bce399202160693445 (patch) | |
tree | 5bd4f9fc816d019639851a8bcc0cb611e4cde0e6 | |
parent | 7683ba5e907c2dca21f1cfffbd33ae50f8c975a7 (diff) |
fix(cli/fmt): show filepath for InvalidData error (#17361)
-rw-r--r-- | cli/tests/integration/fmt_tests.rs | 6 | ||||
-rw-r--r-- | cli/tests/testdata/fmt/invalid_data.json | bin | 0 -> 51200 bytes | |||
-rw-r--r-- | cli/tests/testdata/fmt/invalid_data.out | 1 | ||||
-rw-r--r-- | cli/tools/fmt.rs | 6 |
4 files changed, 12 insertions, 1 deletions
diff --git a/cli/tests/integration/fmt_tests.rs b/cli/tests/integration/fmt_tests.rs index 144c2b56c..d230f96c0 100644 --- a/cli/tests/integration/fmt_tests.rs +++ b/cli/tests/integration/fmt_tests.rs @@ -189,6 +189,12 @@ itest!(fmt_check_parse_error { exit_code: 1, }); +itest!(fmt_check_invalid_data { + args: "fmt --check fmt/invalid_data.json", + output: "fmt/invalid_data.out", + exit_code: 1, +}); + itest!(fmt_stdin { args: "fmt -", input: Some("const a = 1\n"), diff --git a/cli/tests/testdata/fmt/invalid_data.json b/cli/tests/testdata/fmt/invalid_data.json Binary files differnew file mode 100644 index 000000000..fb9bec94f --- /dev/null +++ b/cli/tests/testdata/fmt/invalid_data.json diff --git a/cli/tests/testdata/fmt/invalid_data.out b/cli/tests/testdata/fmt/invalid_data.out new file mode 100644 index 000000000..7fd5046ee --- /dev/null +++ b/cli/tests/testdata/fmt/invalid_data.out @@ -0,0 +1 @@ +error: [WILDCARD] is not a valid UTF-8 file diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs index f16e30731..d987d02d6 100644 --- a/cli/tools/fmt.rs +++ b/cli/tools/fmt.rs @@ -20,6 +20,7 @@ use crate::util::fs::FileCollector; use crate::util::path::get_extension; use crate::util::text_encoding; use deno_ast::ParsedSource; +use deno_core::anyhow::anyhow; use deno_core::anyhow::bail; use deno_core::anyhow::Context; use deno_core::error::generic_error; @@ -574,7 +575,10 @@ fn read_file_contents(file_path: &Path) -> Result<FileContents, AnyError> { let file_bytes = fs::read(file_path) .with_context(|| format!("Error reading {}", file_path.display()))?; let charset = text_encoding::detect_charset(&file_bytes); - let file_text = text_encoding::convert_to_utf8(&file_bytes, charset)?; + let file_text = text_encoding::convert_to_utf8(&file_bytes, charset) + .map_err(|_| { + anyhow!("{} is not a valid UTF-8 file", file_path.display()) + })?; let had_bom = file_text.starts_with(text_encoding::BOM_CHAR); let text = if had_bom { text_encoding::strip_bom(&file_text).to_string() |