Skip to content

Commit

Permalink
(#272) Fix exception, improve exception reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Jul 15, 2023
1 parent d3aaa8b commit 29402b4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/WpfMath.Tests/ParserExceptionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,8 @@ let ``Nonexistent color throws a TexParseException``(text: string): unit =
let ``Invalid color numbers throw exceptions``(formula: string, colorModel: string): unit =
let ex = assertParseThrows<TexParseException> formula
Assert.Contains(colorModel, ex.Message)

[<Fact>]
let ``Invalid delimiter test``(): unit =
let ex = assertParseThrows<TexParseException> @"\left{2+2\right\}"
Assert.Contains(@"A delimiter should start from \, but got 2+2\right\", ex.Message)
5 changes: 5 additions & 0 deletions src/WpfMath/Controls/FormulaControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ private static object CoerceFormula(DependencyObject d, object baseValue)
control.SetError(e);
return "";
}
catch (Exception e)
{
control.SetError(new TexParseException("Parser crash! " + e.Message, e));
return "";
}
}

private static object CoerceScaleValue(DependencyObject d, object baseValue)
Expand Down
2 changes: 1 addition & 1 deletion src/XamlMath.Shared/TexFormulaParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ private SymbolAtom ParseDelimiter(SourceSpan value, int start, ref int position)
else
{
if (delimiter[0] != escapeChar)
throw new Exception($"Incorrect parser state: delimiter should start from {escapeChar}: {delimiter}");
throw new TexParseException($"A delimiter should start from {escapeChar}, but got {delimiter}");

// Here goes the fancy business: for non-alphanumeric commands (e.g. \{, \\ etc.) we need to pass them
// through GetDelimeterMapping, but for alphanumeric ones, we don't.
Expand Down

0 comments on commit 29402b4

Please sign in to comment.