From 615e4da4da1a6b09fd4e4e42331396f66aad8e1b Mon Sep 17 00:00:00 2001 From: olcbean Date: Sat, 4 Nov 2017 11:50:50 +0100 Subject: [PATCH] replace IllegalStateException with ParsingException --- .../common/xcontent/ObjectParser.java | 14 +++++++------- .../common/xcontent/ObjectParserTests.java | 9 +++------ .../bucket/geogrid/GeoHashGridParserTests.java | 9 +++------ .../search/sort/ScriptSortBuilderTests.java | 3 ++- .../phrase/DirectCandidateGeneratorTests.java | 4 ++-- 5 files changed, 17 insertions(+), 22 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/ObjectParser.java b/core/src/main/java/org/elasticsearch/common/xcontent/ObjectParser.java index ed1d85b5a7644..8ba30178dc945 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/ObjectParser.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/ObjectParser.java @@ -147,7 +147,7 @@ public Value parse(XContentParser parser, Value value, Context context) throws I } else { token = parser.nextToken(); if (token != XContentParser.Token.START_OBJECT) { - throw new IllegalStateException("[" + name + "] Expected START_OBJECT but was: " + token); + throw new ParsingException(parser.getTokenLocation(), "[" + name + "] Expected START_OBJECT but was: " + token); } } @@ -159,13 +159,13 @@ public Value parse(XContentParser parser, Value value, Context context) throws I fieldParser = getParser(currentFieldName); } else { if (currentFieldName == null) { - throw new IllegalStateException("[" + name + "] no field found"); + throw new ParsingException(parser.getTokenLocation(), "[" + name + "] no field found"); } if (fieldParser == null) { assert ignoreUnknownFields : "this should only be possible if configured to ignore known fields"; parser.skipChildren(); // noop if parser points to a value, skips children if parser is start object or start array } else { - fieldParser.assertSupports(name, token, currentFieldName); + fieldParser.assertSupports(name, token, currentFieldName, parser.getTokenLocation()); parseSub(parser, fieldParser, currentFieldName, value, context); } fieldParser = null; @@ -330,7 +330,7 @@ private void parseSub(XContentParser parser, FieldParser fieldParser, String cur case END_OBJECT: case END_ARRAY: case FIELD_NAME: - throw new IllegalStateException("[" + name + "]" + token + " is unexpected"); + throw new ParsingException(parser.getTokenLocation(), "[" + name + "]" + token + " is unexpected"); case VALUE_STRING: case VALUE_NUMBER: case VALUE_BOOLEAN: @@ -361,12 +361,12 @@ private class FieldParser { this.type = type; } - void assertSupports(String parserName, XContentParser.Token token, String currentFieldName) { + void assertSupports(String parserName, XContentParser.Token token, String currentFieldName, XContentLocation location) { if (parseField.match(currentFieldName) == false) { - throw new IllegalStateException("[" + parserName + "] parsefield doesn't accept: " + currentFieldName); + throw new ParsingException(location, "[" + parserName + "] parsefield doesn't accept: " + currentFieldName); } if (supportedTokens.contains(token) == false) { - throw new IllegalArgumentException( + throw new ParsingException(location, "[" + parserName + "] " + currentFieldName + " doesn't support values of type: " + token); } } diff --git a/core/src/test/java/org/elasticsearch/common/xcontent/ObjectParserTests.java b/core/src/test/java/org/elasticsearch/common/xcontent/ObjectParserTests.java index 0d879e4813116..baa2b3bcb36e6 100644 --- a/core/src/test/java/org/elasticsearch/common/xcontent/ObjectParserTests.java +++ b/core/src/test/java/org/elasticsearch/common/xcontent/ObjectParserTests.java @@ -34,6 +34,7 @@ import java.util.Collections; import java.util.List; +import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.hasSize; public class ObjectParserTests extends ESTestCase { @@ -231,12 +232,8 @@ class TestStruct { TestStruct s = new TestStruct(); objectParser.declareField((i, c, x) -> c.test = i.text(), new ParseField("numeric_value"), ObjectParser.ValueType.FLOAT); - try { - objectParser.parse(parser, s, null); - fail("wrong type - must be number"); - } catch (IllegalArgumentException ex) { - assertEquals(ex.getMessage(), "[foo] numeric_value doesn't support values of type: VALUE_BOOLEAN"); - } + Exception e = expectThrows(ParsingException.class, () -> objectParser.parse(parser, s, null)); + assertThat(e.getMessage(), containsString("[foo] numeric_value doesn't support values of type: VALUE_BOOLEAN")); } public void testParseNested() throws IOException { diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridParserTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridParserTests.java index fa9ddd74dde6e..7f46cb9e551a8 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridParserTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridParserTests.java @@ -24,6 +24,7 @@ import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.test.ESTestCase; +import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.greaterThanOrEqualTo; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.lessThanOrEqualTo; @@ -93,12 +94,8 @@ public void testParseErrorOnBooleanPrecision() throws Exception { XContentParser stParser = createParser(JsonXContent.jsonXContent, "{\"field\":\"my_loc\", \"precision\":false}"); XContentParser.Token token = stParser.nextToken(); assertSame(XContentParser.Token.START_OBJECT, token); - try { - GeoGridAggregationBuilder.parse("geohash_grid", stParser); - fail(); - } catch (IllegalArgumentException ex) { - assertEquals("[geohash_grid] precision doesn't support values of type: VALUE_BOOLEAN", ex.getMessage()); - } + Exception e = expectThrows(ParsingException.class, () -> GeoGridAggregationBuilder.parse("geohash_grid", stParser)); + assertThat(e.getMessage(), containsString("[geohash_grid] precision doesn't support values of type: VALUE_BOOLEAN")); } public void testParseErrorOnPrecisionOutOfRange() throws Exception { diff --git a/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java index d5911a41aee56..9a28740d7271f 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java @@ -24,6 +24,7 @@ import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.SortField; import org.apache.lucene.search.TermQuery; +import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource; @@ -245,7 +246,7 @@ public void testParseUnexpectedToken() throws IOException { parser.nextToken(); parser.nextToken(); - Exception e = expectThrows(IllegalArgumentException.class, () -> ScriptSortBuilder.fromXContent(parser, null)); + Exception e = expectThrows(ParsingException.class, () -> ScriptSortBuilder.fromXContent(parser, null)); assertEquals("[_script] script doesn't support values of type: START_ARRAY", e.getMessage()); } diff --git a/core/src/test/java/org/elasticsearch/search/suggest/phrase/DirectCandidateGeneratorTests.java b/core/src/test/java/org/elasticsearch/search/suggest/phrase/DirectCandidateGeneratorTests.java index 10022cc289a71..c92fba09d8cb4 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/phrase/DirectCandidateGeneratorTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/phrase/DirectCandidateGeneratorTests.java @@ -146,7 +146,7 @@ public void testIllegalXContent() throws IOException { logger.info("Skipping test as it uses a custom duplicate check that is obsolete when strict duplicate checks are enabled."); } else { directGenerator = "{ \"field\" : \"f1\", \"field\" : \"f2\" }"; - assertIllegalXContent(directGenerator, ParsingException.class, + assertIllegalXContent(directGenerator, IllegalArgumentException.class, "[direct_generator] failed to parse field [field]"); } @@ -162,7 +162,7 @@ public void testIllegalXContent() throws IOException { // test unexpected token directGenerator = "{ \"size\" : [ \"xxl\" ] }"; - assertIllegalXContent(directGenerator, IllegalArgumentException.class, + assertIllegalXContent(directGenerator, ParsingException.class, "[direct_generator] size doesn't support values of type: START_ARRAY"); }