1515import com .fasterxml .jackson .core .JsonProcessingException ;
1616import com .fasterxml .jackson .core .JsonToken ;
1717import com .fasterxml .jackson .core .exc .InputCoercionException ;
18+ import com .fasterxml .jackson .core .exc .StreamConstraintsException ;
1819import com .fasterxml .jackson .core .io .JsonEOFException ;
1920
2021import org .elasticsearch .core .IOUtils ;
2829import org .elasticsearch .xcontent .provider .XContentParserConfigurationImpl ;
2930import org .elasticsearch .xcontent .support .AbstractXContentParser ;
3031
32+ import java .io .CharConversionException ;
3133import java .io .IOException ;
3234import java .nio .CharBuffer ;
3335
@@ -50,29 +52,51 @@ public void allowDuplicateKeys(boolean allowDuplicateKeys) {
5052 parser .configure (JsonParser .Feature .STRICT_DUPLICATE_DETECTION , allowDuplicateKeys == false );
5153 }
5254
53- private static XContentParseException newXContentParseException (JsonProcessingException e ) {
55+ private static XContentLocation getLocation (JsonProcessingException e ) {
5456 JsonLocation loc = e .getLocation ();
55- throw new XContentParseException (new XContentLocation (loc .getLineNr (), loc .getColumnNr ()), e .getMessage (), e );
57+ if (loc != null ) {
58+ return new XContentLocation (loc .getLineNr (), loc .getColumnNr ());
59+ } else {
60+ return null ;
61+ }
62+ }
63+
64+ private static XContentParseException newXContentParseException (JsonProcessingException e ) {
65+ return new XContentParseException (getLocation (e ), e .getMessage (), e );
66+ }
67+
68+ /**
69+ * Handle parser exception depending on type.
70+ * This converts known exceptions to XContentParseException and rethrows them.
71+ */
72+ static IOException handleParserException (IOException e ) throws IOException {
73+ switch (e ) {
74+ case JsonEOFException eof -> throw new XContentEOFException (getLocation (eof ), "Unexpected end of file" , e );
75+ case JsonParseException pe -> throw newXContentParseException (pe );
76+ case InputCoercionException ice -> throw newXContentParseException (ice );
77+ case CharConversionException cce -> throw new XContentParseException (null , cce .getMessage (), cce );
78+ case StreamConstraintsException sce -> throw newXContentParseException (sce );
79+ default -> {
80+ return e ;
81+ }
82+ }
5683 }
5784
5885 @ Override
5986 public Token nextToken () throws IOException {
6087 try {
6188 return convertToken (parser .nextToken ());
62- } catch (JsonEOFException e ) {
63- JsonLocation location = e .getLocation ();
64- throw new XContentEOFException (new XContentLocation (location .getLineNr (), location .getColumnNr ()), "Unexpected end of file" , e );
65- } catch (JsonParseException e ) {
66- throw newXContentParseException (e );
89+ } catch (IOException e ) {
90+ throw handleParserException (e );
6791 }
6892 }
6993
7094 @ Override
7195 public String nextFieldName () throws IOException {
7296 try {
7397 return parser .nextFieldName ();
74- } catch (JsonParseException e ) {
75- throw newXContentParseException (e );
98+ } catch (IOException e ) {
99+ throw handleParserException (e );
76100 }
77101 }
78102
@@ -100,8 +124,8 @@ public String currentName() throws IOException {
100124 protected boolean doBooleanValue () throws IOException {
101125 try {
102126 return parser .getBooleanValue ();
103- } catch (JsonParseException e ) {
104- throw newXContentParseException (e );
127+ } catch (IOException e ) {
128+ throw handleParserException (e );
105129 }
106130 }
107131
@@ -112,8 +136,8 @@ public String text() throws IOException {
112136 }
113137 try {
114138 return parser .getText ();
115- } catch (JsonParseException e ) {
116- throw newXContentParseException (e );
139+ } catch (IOException e ) {
140+ throw handleParserException (e );
117141 }
118142 }
119143
@@ -139,8 +163,8 @@ private void throwOnNoText() {
139163 public CharBuffer charBuffer () throws IOException {
140164 try {
141165 return CharBuffer .wrap (parser .getTextCharacters (), parser .getTextOffset (), parser .getTextLength ());
142- } catch (JsonParseException e ) {
143- throw newXContentParseException (e );
166+ } catch (IOException e ) {
167+ throw handleParserException (e );
144168 }
145169 }
146170
@@ -189,89 +213,89 @@ public boolean hasTextCharacters() {
189213 public char [] textCharacters () throws IOException {
190214 try {
191215 return parser .getTextCharacters ();
192- } catch (JsonParseException e ) {
193- throw newXContentParseException (e );
216+ } catch (IOException e ) {
217+ throw handleParserException (e );
194218 }
195219 }
196220
197221 @ Override
198222 public int textLength () throws IOException {
199223 try {
200224 return parser .getTextLength ();
201- } catch (JsonParseException e ) {
202- throw newXContentParseException (e );
225+ } catch (IOException e ) {
226+ throw handleParserException (e );
203227 }
204228 }
205229
206230 @ Override
207231 public int textOffset () throws IOException {
208232 try {
209233 return parser .getTextOffset ();
210- } catch (JsonParseException e ) {
211- throw newXContentParseException (e );
234+ } catch (IOException e ) {
235+ throw handleParserException (e );
212236 }
213237 }
214238
215239 @ Override
216240 public Number numberValue () throws IOException {
217241 try {
218242 return parser .getNumberValue ();
219- } catch (InputCoercionException | JsonParseException e ) {
220- throw newXContentParseException (e );
243+ } catch (IOException e ) {
244+ throw handleParserException (e );
221245 }
222246 }
223247
224248 @ Override
225249 public short doShortValue () throws IOException {
226250 try {
227251 return parser .getShortValue ();
228- } catch (InputCoercionException | JsonParseException e ) {
229- throw newXContentParseException (e );
252+ } catch (IOException e ) {
253+ throw handleParserException (e );
230254 }
231255 }
232256
233257 @ Override
234258 public int doIntValue () throws IOException {
235259 try {
236260 return parser .getIntValue ();
237- } catch (InputCoercionException | JsonParseException e ) {
238- throw newXContentParseException (e );
261+ } catch (IOException e ) {
262+ throw handleParserException (e );
239263 }
240264 }
241265
242266 @ Override
243267 public long doLongValue () throws IOException {
244268 try {
245269 return parser .getLongValue ();
246- } catch (InputCoercionException | JsonParseException e ) {
247- throw newXContentParseException (e );
270+ } catch (IOException e ) {
271+ throw handleParserException (e );
248272 }
249273 }
250274
251275 @ Override
252276 public float doFloatValue () throws IOException {
253277 try {
254278 return parser .getFloatValue ();
255- } catch (InputCoercionException | JsonParseException e ) {
256- throw newXContentParseException (e );
279+ } catch (IOException e ) {
280+ throw handleParserException (e );
257281 }
258282 }
259283
260284 @ Override
261285 public double doDoubleValue () throws IOException {
262286 try {
263287 return parser .getDoubleValue ();
264- } catch (InputCoercionException | JsonParseException e ) {
265- throw newXContentParseException (e );
288+ } catch (IOException e ) {
289+ throw handleParserException (e );
266290 }
267291 }
268292
269293 @ Override
270294 public byte [] binaryValue () throws IOException {
271295 try {
272296 return parser .getBinaryValue ();
273- } catch (JsonParseException e ) {
274- throw newXContentParseException (e );
297+ } catch (IOException e ) {
298+ throw handleParserException (e );
275299 }
276300 }
277301
0 commit comments