15
15
import com .fasterxml .jackson .core .JsonProcessingException ;
16
16
import com .fasterxml .jackson .core .JsonToken ;
17
17
import com .fasterxml .jackson .core .exc .InputCoercionException ;
18
+ import com .fasterxml .jackson .core .exc .StreamConstraintsException ;
18
19
import com .fasterxml .jackson .core .io .JsonEOFException ;
19
20
20
21
import org .elasticsearch .core .IOUtils ;
28
29
import org .elasticsearch .xcontent .provider .XContentParserConfigurationImpl ;
29
30
import org .elasticsearch .xcontent .support .AbstractXContentParser ;
30
31
32
+ import java .io .CharConversionException ;
31
33
import java .io .IOException ;
32
34
import java .nio .CharBuffer ;
33
35
@@ -50,29 +52,54 @@ public void allowDuplicateKeys(boolean allowDuplicateKeys) {
50
52
parser .configure (JsonParser .Feature .STRICT_DUPLICATE_DETECTION , allowDuplicateKeys == false );
51
53
}
52
54
53
- private static XContentParseException newXContentParseException (JsonProcessingException e ) {
55
+ private static XContentLocation getLocation (JsonProcessingException e ) {
54
56
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
+ if (e instanceof JsonEOFException eof ) {
74
+ throw new XContentEOFException (getLocation (eof ), "Unexpected end of file" , e );
75
+ } else if (e instanceof JsonParseException pe ) {
76
+ throw newXContentParseException (pe );
77
+ } else if (e instanceof InputCoercionException ice ) {
78
+ throw newXContentParseException (ice );
79
+ } else if (e instanceof CharConversionException cce ) {
80
+ throw new XContentParseException (null , cce .getMessage (), cce );
81
+ } else if (e instanceof StreamConstraintsException sce ) {
82
+ throw newXContentParseException (sce );
83
+ } else {
84
+ return e ;
85
+ }
56
86
}
57
87
58
88
@ Override
59
89
public Token nextToken () throws IOException {
60
90
try {
61
91
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 );
92
+ } catch (IOException e ) {
93
+ throw handleParserException (e );
67
94
}
68
95
}
69
96
70
97
@ Override
71
98
public String nextFieldName () throws IOException {
72
99
try {
73
100
return parser .nextFieldName ();
74
- } catch (JsonParseException e ) {
75
- throw newXContentParseException (e );
101
+ } catch (IOException e ) {
102
+ throw handleParserException (e );
76
103
}
77
104
}
78
105
@@ -100,8 +127,8 @@ public String currentName() throws IOException {
100
127
protected boolean doBooleanValue () throws IOException {
101
128
try {
102
129
return parser .getBooleanValue ();
103
- } catch (JsonParseException e ) {
104
- throw newXContentParseException (e );
130
+ } catch (IOException e ) {
131
+ throw handleParserException (e );
105
132
}
106
133
}
107
134
@@ -112,8 +139,8 @@ public String text() throws IOException {
112
139
}
113
140
try {
114
141
return parser .getText ();
115
- } catch (JsonParseException e ) {
116
- throw newXContentParseException (e );
142
+ } catch (IOException e ) {
143
+ throw handleParserException (e );
117
144
}
118
145
}
119
146
@@ -139,8 +166,8 @@ private void throwOnNoText() {
139
166
public CharBuffer charBuffer () throws IOException {
140
167
try {
141
168
return CharBuffer .wrap (parser .getTextCharacters (), parser .getTextOffset (), parser .getTextLength ());
142
- } catch (JsonParseException e ) {
143
- throw newXContentParseException (e );
169
+ } catch (IOException e ) {
170
+ throw handleParserException (e );
144
171
}
145
172
}
146
173
@@ -189,89 +216,89 @@ public boolean hasTextCharacters() {
189
216
public char [] textCharacters () throws IOException {
190
217
try {
191
218
return parser .getTextCharacters ();
192
- } catch (JsonParseException e ) {
193
- throw newXContentParseException (e );
219
+ } catch (IOException e ) {
220
+ throw handleParserException (e );
194
221
}
195
222
}
196
223
197
224
@ Override
198
225
public int textLength () throws IOException {
199
226
try {
200
227
return parser .getTextLength ();
201
- } catch (JsonParseException e ) {
202
- throw newXContentParseException (e );
228
+ } catch (IOException e ) {
229
+ throw handleParserException (e );
203
230
}
204
231
}
205
232
206
233
@ Override
207
234
public int textOffset () throws IOException {
208
235
try {
209
236
return parser .getTextOffset ();
210
- } catch (JsonParseException e ) {
211
- throw newXContentParseException (e );
237
+ } catch (IOException e ) {
238
+ throw handleParserException (e );
212
239
}
213
240
}
214
241
215
242
@ Override
216
243
public Number numberValue () throws IOException {
217
244
try {
218
245
return parser .getNumberValue ();
219
- } catch (InputCoercionException | JsonParseException e ) {
220
- throw newXContentParseException (e );
246
+ } catch (IOException e ) {
247
+ throw handleParserException (e );
221
248
}
222
249
}
223
250
224
251
@ Override
225
252
public short doShortValue () throws IOException {
226
253
try {
227
254
return parser .getShortValue ();
228
- } catch (InputCoercionException | JsonParseException e ) {
229
- throw newXContentParseException (e );
255
+ } catch (IOException e ) {
256
+ throw handleParserException (e );
230
257
}
231
258
}
232
259
233
260
@ Override
234
261
public int doIntValue () throws IOException {
235
262
try {
236
263
return parser .getIntValue ();
237
- } catch (InputCoercionException | JsonParseException e ) {
238
- throw newXContentParseException (e );
264
+ } catch (IOException e ) {
265
+ throw handleParserException (e );
239
266
}
240
267
}
241
268
242
269
@ Override
243
270
public long doLongValue () throws IOException {
244
271
try {
245
272
return parser .getLongValue ();
246
- } catch (InputCoercionException | JsonParseException e ) {
247
- throw newXContentParseException (e );
273
+ } catch (IOException e ) {
274
+ throw handleParserException (e );
248
275
}
249
276
}
250
277
251
278
@ Override
252
279
public float doFloatValue () throws IOException {
253
280
try {
254
281
return parser .getFloatValue ();
255
- } catch (InputCoercionException | JsonParseException e ) {
256
- throw newXContentParseException (e );
282
+ } catch (IOException e ) {
283
+ throw handleParserException (e );
257
284
}
258
285
}
259
286
260
287
@ Override
261
288
public double doDoubleValue () throws IOException {
262
289
try {
263
290
return parser .getDoubleValue ();
264
- } catch (InputCoercionException | JsonParseException e ) {
265
- throw newXContentParseException (e );
291
+ } catch (IOException e ) {
292
+ throw handleParserException (e );
266
293
}
267
294
}
268
295
269
296
@ Override
270
297
public byte [] binaryValue () throws IOException {
271
298
try {
272
299
return parser .getBinaryValue ();
273
- } catch (JsonParseException e ) {
274
- throw newXContentParseException (e );
300
+ } catch (IOException e ) {
301
+ throw handleParserException (e );
275
302
}
276
303
}
277
304
0 commit comments