Skip to content

Commit 25dbb58

Browse files
JSON parse failures should be 4xx codes (elastic#112703) (elastic#112751)
It seemed if there wasn't any text to parse, this is not an internal issue but instead an argument issue. I simply changed the exception thrown. If we don't agree with this, I can adjust `query` parsing directly, but this seemed like the better choice. closes: elastic#112296 Co-authored-by: Elastic Machine <[email protected]>
1 parent a2338d7 commit 25dbb58

File tree

5 files changed

+10
-13
lines changed

5 files changed

+10
-13
lines changed

docs/changelog/112703.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 112703
2+
summary: JSON parse failures should be 4xx codes
3+
area: Infra/Core
4+
type: bug
5+
issues: []

libs/x-content/impl/src/main/java/org/elasticsearch/xcontent/provider/json/JsonXContentParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public String text() throws IOException {
111111
}
112112

113113
private void throwOnNoText() {
114-
throw new IllegalStateException("Can't get text on a " + currentToken() + " at " + getTokenLocation());
114+
throw new IllegalArgumentException("Expected text at " + getTokenLocation() + " but found " + currentToken());
115115
}
116116

117117
@Override

server/src/main/java/org/elasticsearch/index/mapper/IpFieldMapper.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
4343
import org.elasticsearch.search.lookup.FieldValues;
4444
import org.elasticsearch.search.lookup.SearchLookup;
45-
import org.elasticsearch.xcontent.XContentParser;
4645

4746
import java.io.IOException;
4847
import java.net.InetAddress;
@@ -533,8 +532,9 @@ protected String contentType() {
533532
@Override
534533
protected void parseCreateField(DocumentParserContext context) throws IOException {
535534
InetAddress address;
535+
String value = context.parser().textOrNull();
536536
try {
537-
address = value(context.parser(), nullValue);
537+
address = value == null ? nullValue : InetAddresses.forString(value);
538538
} catch (IllegalArgumentException e) {
539539
if (ignoreMalformed) {
540540
context.addIgnoredField(fieldType().name());
@@ -552,14 +552,6 @@ protected void parseCreateField(DocumentParserContext context) throws IOExceptio
552552
}
553553
}
554554

555-
private static InetAddress value(XContentParser parser, InetAddress nullValue) throws IOException {
556-
String value = parser.textOrNull();
557-
if (value == null) {
558-
return nullValue;
559-
}
560-
return InetAddresses.forString(value);
561-
}
562-
563555
private void indexValue(DocumentParserContext context, InetAddress address) {
564556
if (dimension) {
565557
context.getDimensions().addIp(fieldType().name(), address).validate(context.indexSettings());

server/src/test/java/org/elasticsearch/common/ReferenceDocsTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void testResourceValidation() throws Exception {
6666
builder.startObject("UNEXPECTED").endObject().endObject();
6767

6868
try (var stream = BytesReference.bytes(builder).streamInput()) {
69-
expectThrows(IllegalStateException.class, () -> ReferenceDocs.readLinksBySymbol(stream));
69+
expectThrows(IllegalArgumentException.class, () -> ReferenceDocs.readLinksBySymbol(stream));
7070
}
7171
}
7272

server/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ public void testParseFailsWithTermsArray() {
373373
"message1" : ["term1", "term2"]
374374
}
375375
}""";
376-
expectThrows(IllegalStateException.class, () -> parseQuery(json2));
376+
expectThrows(IllegalArgumentException.class, () -> parseQuery(json2));
377377
}
378378

379379
public void testExceptionUsingAnalyzerOnNumericField() {

0 commit comments

Comments
 (0)