Skip to content

Commit

Permalink
Wrap jackson exception on malformed json string (#114445)
Browse files Browse the repository at this point in the history
This commit hides the underlying Jackson parse exception when encountered while parsing string tokens.
  • Loading branch information
henriquepaes1 authored Dec 5, 2024
1 parent 21f72f8 commit 4740b02
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
6 changes: 6 additions & 0 deletions docs/changelog/114445.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 114445
summary: Wrap jackson exception on malformed json string
area: Infra/Core
type: bug
issues:
- 114142
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ public String text() throws IOException {
if (currentToken().isValue() == false) {
throwOnNoText();
}
return parser.getText();
try {
return parser.getText();
} catch (JsonParseException e) {
throw newXContentParseException(e);
}
}

private void throwOnNoText() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ public void testBulkInvalidIndexNameString() throws IOException {

ResponseException responseException = expectThrows(ResponseException.class, () -> getRestClient().performRequest(request));
assertThat(responseException.getResponse().getStatusLine().getStatusCode(), equalTo(BAD_REQUEST.getStatus()));
assertThat(responseException.getMessage(), containsString("could not parse bulk request body"));
assertThat(responseException.getMessage(), containsString("json_parse_exception"));
assertThat(responseException.getMessage(), containsString("x_content_parse_exception"));
assertThat(responseException.getMessage(), containsString("Invalid UTF-8"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

import org.elasticsearch.common.xcontent.BaseXContentTestCase;
import org.elasticsearch.xcontent.XContentGenerator;
import org.elasticsearch.xcontent.XContentParseException;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentParserConfiguration;
import org.elasticsearch.xcontent.XContentType;
import org.elasticsearch.xcontent.json.JsonXContent;

Expand All @@ -28,4 +31,14 @@ public void testBigInteger() throws Exception {
XContentGenerator generator = JsonXContent.jsonXContent.createGenerator(os);
doTestBigInteger(generator, os);
}

public void testMalformedJsonFieldThrowsXContentException() throws Exception {
String json = "{\"test\":\"/*/}";
try (XContentParser parser = JsonXContent.jsonXContent.createParser(XContentParserConfiguration.EMPTY, json)) {
parser.nextToken();
parser.nextToken();
parser.nextToken();
assertThrows(XContentParseException.class, () -> parser.text());
}
}
}

0 comments on commit 4740b02

Please sign in to comment.