Skip to content

Commit 875c9f3

Browse files
olcbeancbuescher
authored andcommitted
ObjectParser: Replace IllegalStateException with ParsingException (#27302)
Relates to #27147
1 parent f321c6d commit 875c9f3

File tree

5 files changed

+17
-22
lines changed

5 files changed

+17
-22
lines changed

core/src/main/java/org/elasticsearch/common/xcontent/ObjectParser.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public Value parse(XContentParser parser, Value value, Context context) throws I
147147
} else {
148148
token = parser.nextToken();
149149
if (token != XContentParser.Token.START_OBJECT) {
150-
throw new IllegalStateException("[" + name + "] Expected START_OBJECT but was: " + token);
150+
throw new ParsingException(parser.getTokenLocation(), "[" + name + "] Expected START_OBJECT but was: " + token);
151151
}
152152
}
153153

@@ -159,13 +159,13 @@ public Value parse(XContentParser parser, Value value, Context context) throws I
159159
fieldParser = getParser(currentFieldName);
160160
} else {
161161
if (currentFieldName == null) {
162-
throw new IllegalStateException("[" + name + "] no field found");
162+
throw new ParsingException(parser.getTokenLocation(), "[" + name + "] no field found");
163163
}
164164
if (fieldParser == null) {
165165
assert ignoreUnknownFields : "this should only be possible if configured to ignore known fields";
166166
parser.skipChildren(); // noop if parser points to a value, skips children if parser is start object or start array
167167
} else {
168-
fieldParser.assertSupports(name, token, currentFieldName);
168+
fieldParser.assertSupports(name, token, currentFieldName, parser.getTokenLocation());
169169
parseSub(parser, fieldParser, currentFieldName, value, context);
170170
}
171171
fieldParser = null;
@@ -330,7 +330,7 @@ private void parseSub(XContentParser parser, FieldParser fieldParser, String cur
330330
case END_OBJECT:
331331
case END_ARRAY:
332332
case FIELD_NAME:
333-
throw new IllegalStateException("[" + name + "]" + token + " is unexpected");
333+
throw new ParsingException(parser.getTokenLocation(), "[" + name + "]" + token + " is unexpected");
334334
case VALUE_STRING:
335335
case VALUE_NUMBER:
336336
case VALUE_BOOLEAN:
@@ -361,12 +361,12 @@ private class FieldParser {
361361
this.type = type;
362362
}
363363

364-
void assertSupports(String parserName, XContentParser.Token token, String currentFieldName) {
364+
void assertSupports(String parserName, XContentParser.Token token, String currentFieldName, XContentLocation location) {
365365
if (parseField.match(currentFieldName) == false) {
366-
throw new IllegalStateException("[" + parserName + "] parsefield doesn't accept: " + currentFieldName);
366+
throw new ParsingException(location, "[" + parserName + "] parsefield doesn't accept: " + currentFieldName);
367367
}
368368
if (supportedTokens.contains(token) == false) {
369-
throw new IllegalArgumentException(
369+
throw new ParsingException(location,
370370
"[" + parserName + "] " + currentFieldName + " doesn't support values of type: " + token);
371371
}
372372
}

core/src/test/java/org/elasticsearch/common/xcontent/ObjectParserTests.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.Collections;
3535
import java.util.List;
3636

37+
import static org.hamcrest.Matchers.containsString;
3738
import static org.hamcrest.Matchers.hasSize;
3839

3940
public class ObjectParserTests extends ESTestCase {
@@ -231,12 +232,8 @@ class TestStruct {
231232
TestStruct s = new TestStruct();
232233

233234
objectParser.declareField((i, c, x) -> c.test = i.text(), new ParseField("numeric_value"), ObjectParser.ValueType.FLOAT);
234-
try {
235-
objectParser.parse(parser, s, null);
236-
fail("wrong type - must be number");
237-
} catch (IllegalArgumentException ex) {
238-
assertEquals(ex.getMessage(), "[foo] numeric_value doesn't support values of type: VALUE_BOOLEAN");
239-
}
235+
Exception e = expectThrows(ParsingException.class, () -> objectParser.parse(parser, s, null));
236+
assertThat(e.getMessage(), containsString("[foo] numeric_value doesn't support values of type: VALUE_BOOLEAN"));
240237
}
241238

242239
public void testParseNested() throws IOException {

core/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridParserTests.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.elasticsearch.common.xcontent.json.JsonXContent;
2525
import org.elasticsearch.test.ESTestCase;
2626

27+
import static org.hamcrest.Matchers.containsString;
2728
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
2829
import static org.hamcrest.Matchers.instanceOf;
2930
import static org.hamcrest.Matchers.lessThanOrEqualTo;
@@ -93,12 +94,8 @@ public void testParseErrorOnBooleanPrecision() throws Exception {
9394
XContentParser stParser = createParser(JsonXContent.jsonXContent, "{\"field\":\"my_loc\", \"precision\":false}");
9495
XContentParser.Token token = stParser.nextToken();
9596
assertSame(XContentParser.Token.START_OBJECT, token);
96-
try {
97-
GeoGridAggregationBuilder.parse("geohash_grid", stParser);
98-
fail();
99-
} catch (IllegalArgumentException ex) {
100-
assertEquals("[geohash_grid] precision doesn't support values of type: VALUE_BOOLEAN", ex.getMessage());
101-
}
97+
Exception e = expectThrows(ParsingException.class, () -> GeoGridAggregationBuilder.parse("geohash_grid", stParser));
98+
assertThat(e.getMessage(), containsString("[geohash_grid] precision doesn't support values of type: VALUE_BOOLEAN"));
10299
}
103100

104101
public void testParseErrorOnPrecisionOutOfRange() throws Exception {

core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.lucene.search.MatchAllDocsQuery;
2525
import org.apache.lucene.search.SortField;
2626
import org.apache.lucene.search.TermQuery;
27+
import org.elasticsearch.common.ParsingException;
2728
import org.elasticsearch.common.xcontent.XContentParser;
2829
import org.elasticsearch.common.xcontent.json.JsonXContent;
2930
import org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource;
@@ -245,7 +246,7 @@ public void testParseUnexpectedToken() throws IOException {
245246
parser.nextToken();
246247
parser.nextToken();
247248

248-
Exception e = expectThrows(IllegalArgumentException.class, () -> ScriptSortBuilder.fromXContent(parser, null));
249+
Exception e = expectThrows(ParsingException.class, () -> ScriptSortBuilder.fromXContent(parser, null));
249250
assertEquals("[_script] script doesn't support values of type: START_ARRAY", e.getMessage());
250251
}
251252

core/src/test/java/org/elasticsearch/search/suggest/phrase/DirectCandidateGeneratorTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public void testIllegalXContent() throws IOException {
146146
logger.info("Skipping test as it uses a custom duplicate check that is obsolete when strict duplicate checks are enabled.");
147147
} else {
148148
directGenerator = "{ \"field\" : \"f1\", \"field\" : \"f2\" }";
149-
assertIllegalXContent(directGenerator, ParsingException.class,
149+
assertIllegalXContent(directGenerator, IllegalArgumentException.class,
150150
"[direct_generator] failed to parse field [field]");
151151
}
152152

@@ -162,7 +162,7 @@ public void testIllegalXContent() throws IOException {
162162

163163
// test unexpected token
164164
directGenerator = "{ \"size\" : [ \"xxl\" ] }";
165-
assertIllegalXContent(directGenerator, IllegalArgumentException.class,
165+
assertIllegalXContent(directGenerator, ParsingException.class,
166166
"[direct_generator] size doesn't support values of type: START_ARRAY");
167167
}
168168

0 commit comments

Comments
 (0)