Skip to content

Commit

Permalink
Better error message for break/continue
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski committed May 15, 2024
1 parent a77b4f0 commit fef27d2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
10 changes: 8 additions & 2 deletions askama_parser/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ impl<'a> Node<'a> {
));
let (j, (pws, _, nws)) = p(i)?;
if !s.is_in_loop() {
return Err(nom::Err::Failure(error_position!(i, ErrorKind::Tag)));
return Err(nom::Err::Failure(ErrorContext {
input: i,
message: Some(Cow::Borrowed("you can only `break` inside a `for` loop")),
}));
}
Ok((j, Self::Break(Ws(pws, nws))))
}
Expand All @@ -100,7 +103,10 @@ impl<'a> Node<'a> {
));
let (j, (pws, _, nws)) = p(i)?;
if !s.is_in_loop() {
return Err(nom::Err::Failure(error_position!(i, ErrorKind::Tag)));
return Err(nom::Err::Failure(ErrorContext {
input: i,
message: Some(Cow::Borrowed("you can only `continue` inside a `for` loop")),
}));
}
Ok((j, Self::Continue(Ws(pws, nws))))
}
Expand Down
3 changes: 2 additions & 1 deletion testing/tests/ui/break_outside_of_loop.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
error: failed to parse template source at row 1, column 9 near:
error: you can only `break` inside a `for` loop
failed to parse template source at row 1, column 9 near:
"break%}, have a parsing error!"
--> tests/ui/break_outside_of_loop.rs:3:10
|
Expand Down

0 comments on commit fef27d2

Please sign in to comment.