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,51 @@ 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
+ 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
+ }
56
83
}
57
84
58
85
@ Override
59
86
public Token nextToken () throws IOException {
60
87
try {
61
88
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 );
67
91
}
68
92
}
69
93
70
94
@ Override
71
95
public String nextFieldName () throws IOException {
72
96
try {
73
97
return parser .nextFieldName ();
74
- } catch (JsonParseException e ) {
75
- throw newXContentParseException (e );
98
+ } catch (IOException e ) {
99
+ throw handleParserException (e );
76
100
}
77
101
}
78
102
@@ -100,8 +124,8 @@ public String currentName() throws IOException {
100
124
protected boolean doBooleanValue () throws IOException {
101
125
try {
102
126
return parser .getBooleanValue ();
103
- } catch (JsonParseException e ) {
104
- throw newXContentParseException (e );
127
+ } catch (IOException e ) {
128
+ throw handleParserException (e );
105
129
}
106
130
}
107
131
@@ -112,8 +136,8 @@ public String text() throws IOException {
112
136
}
113
137
try {
114
138
return parser .getText ();
115
- } catch (JsonParseException e ) {
116
- throw newXContentParseException (e );
139
+ } catch (IOException e ) {
140
+ throw handleParserException (e );
117
141
}
118
142
}
119
143
@@ -139,8 +163,8 @@ private void throwOnNoText() {
139
163
public CharBuffer charBuffer () throws IOException {
140
164
try {
141
165
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 );
144
168
}
145
169
}
146
170
@@ -189,89 +213,89 @@ public boolean hasTextCharacters() {
189
213
public char [] textCharacters () throws IOException {
190
214
try {
191
215
return parser .getTextCharacters ();
192
- } catch (JsonParseException e ) {
193
- throw newXContentParseException (e );
216
+ } catch (IOException e ) {
217
+ throw handleParserException (e );
194
218
}
195
219
}
196
220
197
221
@ Override
198
222
public int textLength () throws IOException {
199
223
try {
200
224
return parser .getTextLength ();
201
- } catch (JsonParseException e ) {
202
- throw newXContentParseException (e );
225
+ } catch (IOException e ) {
226
+ throw handleParserException (e );
203
227
}
204
228
}
205
229
206
230
@ Override
207
231
public int textOffset () throws IOException {
208
232
try {
209
233
return parser .getTextOffset ();
210
- } catch (JsonParseException e ) {
211
- throw newXContentParseException (e );
234
+ } catch (IOException e ) {
235
+ throw handleParserException (e );
212
236
}
213
237
}
214
238
215
239
@ Override
216
240
public Number numberValue () throws IOException {
217
241
try {
218
242
return parser .getNumberValue ();
219
- } catch (InputCoercionException | JsonParseException e ) {
220
- throw newXContentParseException (e );
243
+ } catch (IOException e ) {
244
+ throw handleParserException (e );
221
245
}
222
246
}
223
247
224
248
@ Override
225
249
public short doShortValue () throws IOException {
226
250
try {
227
251
return parser .getShortValue ();
228
- } catch (InputCoercionException | JsonParseException e ) {
229
- throw newXContentParseException (e );
252
+ } catch (IOException e ) {
253
+ throw handleParserException (e );
230
254
}
231
255
}
232
256
233
257
@ Override
234
258
public int doIntValue () throws IOException {
235
259
try {
236
260
return parser .getIntValue ();
237
- } catch (InputCoercionException | JsonParseException e ) {
238
- throw newXContentParseException (e );
261
+ } catch (IOException e ) {
262
+ throw handleParserException (e );
239
263
}
240
264
}
241
265
242
266
@ Override
243
267
public long doLongValue () throws IOException {
244
268
try {
245
269
return parser .getLongValue ();
246
- } catch (InputCoercionException | JsonParseException e ) {
247
- throw newXContentParseException (e );
270
+ } catch (IOException e ) {
271
+ throw handleParserException (e );
248
272
}
249
273
}
250
274
251
275
@ Override
252
276
public float doFloatValue () throws IOException {
253
277
try {
254
278
return parser .getFloatValue ();
255
- } catch (InputCoercionException | JsonParseException e ) {
256
- throw newXContentParseException (e );
279
+ } catch (IOException e ) {
280
+ throw handleParserException (e );
257
281
}
258
282
}
259
283
260
284
@ Override
261
285
public double doDoubleValue () throws IOException {
262
286
try {
263
287
return parser .getDoubleValue ();
264
- } catch (InputCoercionException | JsonParseException e ) {
265
- throw newXContentParseException (e );
288
+ } catch (IOException e ) {
289
+ throw handleParserException (e );
266
290
}
267
291
}
268
292
269
293
@ Override
270
294
public byte [] binaryValue () throws IOException {
271
295
try {
272
296
return parser .getBinaryValue ();
273
- } catch (JsonParseException e ) {
274
- throw newXContentParseException (e );
297
+ } catch (IOException e ) {
298
+ throw handleParserException (e );
275
299
}
276
300
}
277
301
0 commit comments