From df4d0c55c09a1dc6f227b0bce399202160693445 Mon Sep 17 00:00:00 2001 From: Leo Kettmeir Date: Mon, 16 Jan 2023 01:30:52 +0100 Subject: fix(cli/fmt): show filepath for InvalidData error (#17361) --- cli/tools/fmt.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'cli/tools/fmt.rs') 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 { 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() -- cgit v1.2.3