Skip to content

Improve exception handling for JsonXContentParser #123439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Wrap parser creation so that bad char sequences do not cause 500
  • Loading branch information
smalyshev committed Feb 26, 2025
commit 20a0f9e85e3a83c6748ceecc7dbc4aa84d9f4dac
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
import org.elasticsearch.xcontent.XContent;
import org.elasticsearch.xcontent.XContentBuilder;
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.provider.XContentImplUtils;

import java.io.CharConversionException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -96,21 +98,37 @@ private XContentParser createParser(XContentParserConfiguration config, JsonPars

@Override
public XContentParser createParser(XContentParserConfiguration config, String content) throws IOException {
return createParser(config, jsonFactory.createParser(content));
try {
return createParser(config, jsonFactory.createParser(content));
} catch (CharConversionException e) {
throw new XContentParseException(null, e.getMessage(), e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JsonFactory.createParser can throw JsonParseException, so shouldn't this catch also use handleParseException?

}
}

@Override
public XContentParser createParser(XContentParserConfiguration config, InputStream is) throws IOException {
return createParser(config, jsonFactory.createParser(is));
try {
return createParser(config, jsonFactory.createParser(is));
} catch (CharConversionException e) {
throw new XContentParseException(null, e.getMessage(), e);
}
}

@Override
public XContentParser createParser(XContentParserConfiguration config, byte[] data, int offset, int length) throws IOException {
try {
return createParser(config, jsonFactory.createParser(data, offset, length));
} catch (CharConversionException e) {
throw new XContentParseException(null, e.getMessage(), e);
}
}

@Override
public XContentParser createParser(XContentParserConfiguration config, Reader reader) throws IOException {
try {
return createParser(config, jsonFactory.createParser(reader));
} catch (CharConversionException e) {
throw new XContentParseException(null, e.getMessage(), e);
}
}
}