From 2cbb4869f34563ea686a3275ba7b2afce99efa14 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 30 Apr 2016 16:02:45 +0100 Subject: Use specialised exception type for parse errors --- .../internal/styles/parsing/DocumentMatcherParser.java | 4 ++-- .../mammoth/internal/styles/parsing/ParseException.java | 7 +++++++ .../mammoth/internal/styles/parsing/TokenIterator.java | 6 +++--- src/test/java/org/zwobble/mammoth/tests/MammothTests.java | 12 ++++++++++++ 4 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 src/main/java/org/zwobble/mammoth/internal/styles/parsing/ParseException.java 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 @@ -189,6 +191,16 @@ public class MammothTests { isSuccess("

The Sunset Tree

")); } + @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( -- cgit v1.2.3