From d71b9ef7b2ed7d72df132b5e38448fbbac7036be Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 30 Apr 2016 16:53:16 +0100 Subject: Produce nicer error messages in C# --- dotnet/Mammoth.Tests/DocumentConverterTests.cs | 13 ++++++++++++- dotnet/Mammoth/Couscous/HorribleHacks.cs | 4 ++++ .../mammoth/internal/styles/parsing/LineParseException.cs | 15 +++++++++++++++ .../mammoth/internal/styles/parsing/ParseException.cs | 8 ++++++++ dotnet/Mammoth/Mammoth.csproj | 2 ++ 5 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 dotnet/Mammoth/Couscous/mammoth/internal/styles/parsing/LineParseException.cs create mode 100644 dotnet/Mammoth/Couscous/mammoth/internal/styles/parsing/ParseException.cs (limited to 'dotnet') diff --git a/dotnet/Mammoth.Tests/DocumentConverterTests.cs b/dotnet/Mammoth.Tests/DocumentConverterTests.cs index 89ade1c..92a3adf 100644 --- a/dotnet/Mammoth.Tests/DocumentConverterTests.cs +++ b/dotnet/Mammoth.Tests/DocumentConverterTests.cs @@ -151,12 +151,23 @@ namespace Mammoth.Tests { } [Fact] - public void RulesFromPreviouslyAddedStyleMapsStillTakeEffectIfNotOverriden() { + public void RulesFromPreviouslyAddedStyleMapsStillTakeEffectIfNotOverridden() { AssertSuccessfulConversion( ConvertToHtml("underline.docx", mammoth => mammoth.AddStyleMap("u => em").AddStyleMap("strike => del")), "

The Sunset Tree

"); } + [Fact] + public void ErrorIsRaisedIfStyleMapCannotBeParsed() { + var exception = Assert.ThrowsAny(() => + new DocumentConverter().AddStyleMap("p =>\np[style-name=] =>")); + Assert.Equal( + "error reading style map at line 2, character 14: expected token of type _STRING but was of type _CLOSE_SQUARE_BRACKET\n\n" + + "p[style-name=] =>\n" + + " ^", + exception.Message); + } + [Fact] public void CanDisableDefaultStyleMap() { var result = ConvertToHtml("simple-list.docx", mammoth => mammoth.DisableDefaultStyleMap()); diff --git a/dotnet/Mammoth/Couscous/HorribleHacks.cs b/dotnet/Mammoth/Couscous/HorribleHacks.cs index 9f1ac30..c97dc0a 100644 --- a/dotnet/Mammoth/Couscous/HorribleHacks.cs +++ b/dotnet/Mammoth/Couscous/HorribleHacks.cs @@ -26,5 +26,9 @@ namespace Mammoth.Couscous { internal static bool equals(this TokenType first, TokenType second) { return first == second; } + + internal static string getMessage(this System.Exception exception) { + return exception.Message; + } } } diff --git a/dotnet/Mammoth/Couscous/mammoth/internal/styles/parsing/LineParseException.cs b/dotnet/Mammoth/Couscous/mammoth/internal/styles/parsing/LineParseException.cs new file mode 100644 index 0000000..6ca9381 --- /dev/null +++ b/dotnet/Mammoth/Couscous/mammoth/internal/styles/parsing/LineParseException.cs @@ -0,0 +1,15 @@ +using Mammoth.Couscous.java.lang; + +namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.styles.parsing { + internal class LineParseException : RuntimeException { + private readonly Token _token; + + internal LineParseException(Token token, string message) : base(message) { + _token = token; + } + + internal Token getToken() { + return _token; + } + } +} diff --git a/dotnet/Mammoth/Couscous/mammoth/internal/styles/parsing/ParseException.cs b/dotnet/Mammoth/Couscous/mammoth/internal/styles/parsing/ParseException.cs new file mode 100644 index 0000000..73f349c --- /dev/null +++ b/dotnet/Mammoth/Couscous/mammoth/internal/styles/parsing/ParseException.cs @@ -0,0 +1,8 @@ +using Mammoth.Couscous.java.lang; + +namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.styles.parsing { + internal class ParseException : RuntimeException { + internal ParseException(string message) : base(message) { + } + } +} diff --git a/dotnet/Mammoth/Mammoth.csproj b/dotnet/Mammoth/Mammoth.csproj index 4e945d0..6e2c473 100644 --- a/dotnet/Mammoth/Mammoth.csproj +++ b/dotnet/Mammoth/Mammoth.csproj @@ -81,6 +81,8 @@ + + -- cgit v1.2.3