Skip to content

Commit

Permalink
Merge pull request #5982 from jecaro/error-msg-non-utf8
Browse files Browse the repository at this point in the history
Set a better error msg for non uft8 encoded files
  • Loading branch information
Anton-4 authored Nov 14, 2023
2 parents 1c1231b + 0781232 commit 7b1f2d2
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions crates/reporting/src/error/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3205,21 +3205,31 @@ fn to_header_report<'a>(
let surroundings = Region::new(start, *pos);
let region = LineColumnRegion::from_pos(lines.convert_pos(*pos));

let doc = alloc.stack([
alloc.reflow(r"I am expecting a header, but got stuck here:"),
alloc.region_with_subregion(lines.convert_region(surroundings), region),
alloc.concat([
alloc.reflow("I am expecting a module keyword next, one of "),
alloc.keyword("interface"),
alloc.reflow(", "),
alloc.keyword("app"),
alloc.reflow(", "),
alloc.keyword("package"),
alloc.reflow(" or "),
alloc.keyword("platform"),
alloc.reflow("."),
]),
]);
let is_utf8 = alloc
.src_lines
.iter()
.all(|line| std::str::from_utf8(line.as_bytes()).is_ok());

let preamble = if is_utf8 {
vec![
alloc.reflow(r"I am expecting a header, but got stuck here:"),
alloc.region_with_subregion(lines.convert_region(surroundings), region),
]
} else {
vec![alloc.reflow(r"I am expecting a header, but the file is not UTF-8 encoded.")]
};

let doc = alloc.stack(preamble.into_iter().chain([alloc.concat([
alloc.reflow("I am expecting a module keyword next, one of "),
alloc.keyword("interface"),
alloc.reflow(", "),
alloc.keyword("app"),
alloc.reflow(", "),
alloc.keyword("package"),
alloc.reflow(" or "),
alloc.keyword("platform"),
alloc.reflow("."),
])]));

Report {
filename,
Expand Down

0 comments on commit 7b1f2d2

Please sign in to comment.