diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-04-30 16:02:45 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-04-30 16:02:45 +0100 |
| commit | 2cbb4869f34563ea686a3275ba7b2afce99efa14 (patch) | |
| tree | 94d6df4ae3b1bd983e810973baa0a64e0b611412 | |
| parent | cd0383a5cdb303a552ef13fc3c70261d36823ba8 (diff) | |
Use specialised exception type for parse errors
4 files changed, 24 insertions, 5 deletions
diff --git a/src/main/java/org/zwobble/mammoth/internal/styles/parsing/DocumentMatcherParser.java b/src/main/java/org/zwobble/mammoth/internal/styles/parsing/DocumentMatcherParser.java index 0998f8b..368bc81 100644 --- a/src/main/java/org/zwobble/mammoth/internal/styles/parsing/DocumentMatcherParser.java +++ b/src/main/java/org/zwobble/mammoth/internal/styles/parsing/DocumentMatcherParser.java @@ -29,7 +29,7 @@ public class DocumentMatcherParser { case "strike": return StyleMapBuilder::strikethrough; default: - throw new IllegalArgumentException("Unrecognised document element: " + identifier); + throw new ParseException("Unrecognised document element: " + identifier); } } @@ -88,7 +88,7 @@ public class DocumentMatcherParser { } else if (listType.equals("unordered-list")) { return false; } else { - throw new IllegalArgumentException("Unrecognised list type: " + listType); + throw new ParseException("Unrecognised list type: " + listType); } } } diff --git a/src/main/java/org/zwobble/mammoth/internal/styles/parsing/ParseException.java b/src/main/java/org/zwobble/mammoth/internal/styles/parsing/ParseException.java new file mode 100644 index 0000000..718bb30 --- /dev/null +++ b/src/main/java/org/zwobble/mammoth/internal/styles/parsing/ParseException.java @@ -0,0 +1,7 @@ +package org.zwobble.mammoth.internal.styles.parsing; + +public class ParseException extends RuntimeException { + public ParseException(String message) { + super(message); + } +} diff --git a/src/main/java/org/zwobble/mammoth/internal/styles/parsing/TokenIterator.java b/src/main/java/org/zwobble/mammoth/internal/styles/parsing/TokenIterator.java index df22b5d..f006007 100644 --- a/src/main/java/org/zwobble/mammoth/internal/styles/parsing/TokenIterator.java +++ b/src/main/java/org/zwobble/mammoth/internal/styles/parsing/TokenIterator.java @@ -46,12 +46,12 @@ public class TokenIterator { } String actualValue = tokens.get(index).getValue(); if (!actualValue.equals(tokenValue)) { - throw new IllegalArgumentException("expected " + tokenType + " token with value " + tokenValue + " but value was " + actualValue); + throw new ParseException("expected " + tokenType + " token with value " + tokenValue + " but value was " + actualValue); } index += 1; } - private IllegalArgumentException unexpectedTokenType(TokenType expected, TokenType actual) { - return new IllegalArgumentException("expected token of type " + expected + " but was of type " + actual); + private ParseException unexpectedTokenType(TokenType expected, TokenType actual) { + return new ParseException("expected token of type " + expected + " but was of type " + actual); } } diff --git a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java index b2661b0..4f4c9fd 100644 --- a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java @@ -3,6 +3,7 @@ package org.zwobble.mammoth.tests; import org.junit.Test; import org.zwobble.mammoth.DocumentConverter; import org.zwobble.mammoth.Result; +import org.zwobble.mammoth.internal.styles.parsing.ParseException; import java.io.File; import java.io.IOException; @@ -15,6 +16,7 @@ import static org.junit.Assert.assertThat; import static org.zwobble.mammoth.internal.util.Lists.list; import static org.zwobble.mammoth.tests.ResultMatchers.isResult; import static org.zwobble.mammoth.tests.ResultMatchers.isSuccess; +import static org.zwobble.mammoth.tests.util.MammothAsserts.assertThrows; public class MammothTests { @Test @@ -190,6 +192,16 @@ public class MammothTests { } @Test + public void errorIsRaisedIfStyleMapCannotBeParsed() throws IOException { + RuntimeException exception = assertThrows( + ParseException.class, + () -> new DocumentConverter().addStyleMap("p =>\np[style-name=] =>")); + assertThat( + exception.getMessage(), + equalTo("expected token of type STRING but was of type CLOSE_SQUARE_BRACKET")); + } + + @Test public void canDisableDefaultStyleMap() throws IOException { assertThat( convertToHtml("simple-list.docx", mammoth -> mammoth.disableDefaultStyleMap()), |
