Skip to content

Commit 4c839b9

Browse files
committed
Change error message to report the same XContentLocation as before
1 parent f41655c commit 4c839b9

File tree

11 files changed

+28
-32
lines changed

11 files changed

+28
-32
lines changed

libs/x-content/src/main/java/org/elasticsearch/common/xcontent/ObjectParser.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,17 @@ public static <Value, ElementValue> BiConsumer<Value, List<ElementValue>> fromLi
8080

8181
private interface UnknownFieldParser<Value, Context> {
8282

83-
/**
84-
* Called when an unknown field is encountered
85-
* @param parserName the parent ObjectParser name
86-
* @param field the name of the unknown field
87-
* @param parser the parser to build values from
88-
*/
89-
void acceptUnknownField(String parserName, String field, XContentParser parser, Value value, Context context) throws IOException;
83+
void acceptUnknownField(String parserName, String field, XContentLocation location, XContentParser parser,
84+
Value value, Context context) throws IOException;
9085
}
9186

9287
private static <Value, Context> UnknownFieldParser<Value, Context> ignoreUnknown() {
93-
return (n, f, x, v, c) -> x.skipChildren();
88+
return (n, f, p, x, v, c) -> x.skipChildren();
9489
}
95-
90+
9691
private static <Value, Context> UnknownFieldParser<Value, Context> errorOnUnknown() {
97-
return (n, f, x, v, c) -> {
98-
throw new XContentParseException(x.getTokenLocation(),
99-
"[" + n + "] unknown field [" + f + "], parser not found");
92+
return (n, f, p, x, v, c) -> {
93+
throw new XContentParseException(p, "[" + n + "] unknown field [" + f + "], parser not found");
10094
};
10195
}
10296

@@ -108,7 +102,7 @@ public interface UnknownFieldConsumer<Value> {
108102
}
109103

110104
private static <Value, Context> UnknownFieldParser<Value, Context> consumeUnknownField(UnknownFieldConsumer<Value> consumer) {
111-
return (parserName, field, parser, value, context) -> {
105+
return (parserName, field, location, parser, value, context) -> {
112106
XContentParser.Token t = parser.currentToken();
113107
switch (t) {
114108
case VALUE_STRING:
@@ -227,16 +221,18 @@ public Value parse(XContentParser parser, Value value, Context context) throws I
227221

228222
FieldParser fieldParser = null;
229223
String currentFieldName = null;
224+
XContentLocation currentPosition = null;
230225
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
231226
if (token == XContentParser.Token.FIELD_NAME) {
232227
currentFieldName = parser.currentName();
228+
currentPosition = parser.getTokenLocation();
233229
fieldParser = fieldParserMap.get(currentFieldName);
234230
} else {
235231
if (currentFieldName == null) {
236232
throw new XContentParseException(parser.getTokenLocation(), "[" + name + "] no field found");
237233
}
238234
if (fieldParser == null) {
239-
unknownFieldParser.acceptUnknownField(name, currentFieldName, parser, value, context);
235+
unknownFieldParser.acceptUnknownField(name, currentFieldName, currentPosition, parser, value, context);
240236
} else {
241237
fieldParser.assertSupports(name, parser, currentFieldName);
242238
parseSub(parser, fieldParser, currentFieldName, value, context);

libs/x-content/src/test/java/org/elasticsearch/common/xcontent/ObjectParserTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public void setTest(int test) {
205205
{
206206
XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"not_supported_field\" : \"foo\"}");
207207
XContentParseException ex = expectThrows(XContentParseException.class, () -> objectParser.parse(parser, s, null));
208-
assertEquals(ex.getMessage(), "[1:26] [the_parser] unknown field [not_supported_field], parser not found");
208+
assertEquals(ex.getMessage(), "[1:2] [the_parser] unknown field [not_supported_field], parser not found");
209209
}
210210
}
211211

server/src/test/java/org/elasticsearch/action/update/UpdateRequestTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public void testUnknownFieldParsing() throws Exception {
288288
.endObject());
289289

290290
XContentParseException ex = expectThrows(XContentParseException.class, () -> request.fromXContent(contentParser));
291-
assertEquals("[1:2] [UpdateRequest] unknown field [unknown_field], parser not found", ex.getMessage());
291+
assertEquals("[1:18] [UpdateRequest] unknown field [unknown_field], parser not found", ex.getMessage());
292292

293293
UpdateRequest request2 = new UpdateRequest("test", "type", "1");
294294
XContentParser unknownObject = createParser(XContentFactory.jsonBuilder()
@@ -299,7 +299,7 @@ public void testUnknownFieldParsing() throws Exception {
299299
.endObject()
300300
.endObject());
301301
ex = expectThrows(XContentParseException.class, () -> request2.fromXContent(unknownObject));
302-
assertEquals("[1:76] [UpdateRequest] unknown field [params], parser not found", ex.getMessage());
302+
assertEquals("[1:85] [UpdateRequest] unknown field [params], parser not found", ex.getMessage());
303303
}
304304

305305
public void testFetchSourceParsing() throws Exception {

server/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightBuilderTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public void testUnknownArrayNameExpection() throws IOException {
162162
XContentParseException e = expectParseThrows(XContentParseException.class, "{\n" +
163163
" \"bad_fieldname\" : [ \"field1\" 1 \"field2\" ]\n" +
164164
"}\n");
165-
assertEquals("[2:5] [highlight] unknown field [bad_fieldname], parser not found", e.getMessage());
165+
assertEquals("[2:23] [highlight] unknown field [bad_fieldname], parser not found", e.getMessage());
166166
}
167167

168168
{
@@ -175,7 +175,7 @@ public void testUnknownArrayNameExpection() throws IOException {
175175
"}\n");
176176
assertThat(e.getMessage(), containsString("[highlight] failed to parse field [fields]"));
177177
assertThat(e.getCause().getMessage(), containsString("[fields] failed to parse field [body]"));
178-
assertEquals("[4:9] [highlight_field] unknown field [bad_fieldname], parser not found", e.getCause().getCause().getMessage());
178+
assertEquals("[4:27] [highlight_field] unknown field [bad_fieldname], parser not found", e.getCause().getCause().getMessage());
179179
}
180180
}
181181

@@ -193,7 +193,7 @@ public void testUnknownFieldnameExpection() throws IOException {
193193
XContentParseException e = expectParseThrows(XContentParseException.class, "{\n" +
194194
" \"bad_fieldname\" : \"value\"\n" +
195195
"}\n");
196-
assertEquals("[2:5] [highlight] unknown field [bad_fieldname], parser not found", e.getMessage());
196+
assertEquals("[2:23] [highlight] unknown field [bad_fieldname], parser not found", e.getMessage());
197197
}
198198

199199
{
@@ -206,7 +206,7 @@ public void testUnknownFieldnameExpection() throws IOException {
206206
"}\n");
207207
assertThat(e.getMessage(), containsString("[highlight] failed to parse field [fields]"));
208208
assertThat(e.getCause().getMessage(), containsString("[fields] failed to parse field [body]"));
209-
assertEquals("[4:9] [highlight_field] unknown field [bad_fieldname], parser not found", e.getCause().getCause().getMessage());
209+
assertEquals("[4:27] [highlight_field] unknown field [bad_fieldname], parser not found", e.getCause().getCause().getMessage());
210210
}
211211
}
212212

@@ -218,7 +218,7 @@ public void testUnknownObjectFieldnameExpection() throws IOException {
218218
XContentParseException e = expectParseThrows(XContentParseException.class, "{\n" +
219219
" \"bad_fieldname\" : { \"field\" : \"value\" }\n \n" +
220220
"}\n");
221-
assertEquals("[2:5] [highlight] unknown field [bad_fieldname], parser not found", e.getMessage());
221+
assertEquals("[2:24] [highlight] unknown field [bad_fieldname], parser not found", e.getMessage());
222222
}
223223

224224
{
@@ -231,7 +231,7 @@ public void testUnknownObjectFieldnameExpection() throws IOException {
231231
"}\n");
232232
assertThat(e.getMessage(), containsString("[highlight] failed to parse field [fields]"));
233233
assertThat(e.getCause().getMessage(), containsString("[fields] failed to parse field [body]"));
234-
assertEquals("[4:9] [highlight_field] unknown field [bad_fieldname], parser not found", e.getCause().getCause().getMessage());
234+
assertEquals("[4:27] [highlight_field] unknown field [bad_fieldname], parser not found", e.getCause().getCause().getMessage());
235235
}
236236
}
237237

server/src/test/java/org/elasticsearch/search/rescore/QueryRescorerBuilderTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public void testUnknownFieldsExpection() throws IOException {
251251
"}\n";
252252
try (XContentParser parser = createParser(rescoreElement)) {
253253
XContentParseException e = expectThrows(XContentParseException.class, () -> RescorerBuilder.parseFromXContent(parser));
254-
assertEquals("[3:17] [query] unknown field [bad_fieldname], parser not found", e.getMessage());
254+
assertEquals("[3:35] [query] unknown field [bad_fieldname], parser not found", e.getMessage());
255255
}
256256

257257
rescoreElement = "{\n" +

server/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ public void testUnknownOptionFails() throws IOException {
318318
parser.nextToken();
319319

320320
XContentParseException e = expectThrows(XContentParseException.class, () -> FieldSortBuilder.fromXContent(parser, ""));
321-
assertEquals("[1:18] [field_sort] unknown field [reverse], parser not found", e.getMessage());
321+
assertEquals("[1:30] [field_sort] unknown field [reverse], parser not found", e.getMessage());
322322
}
323323
}
324324

server/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public void testParseBadFieldNameExceptions() throws IOException {
227227
parser.nextToken();
228228

229229
XContentParseException e = expectThrows(XContentParseException.class, () -> ScriptSortBuilder.fromXContent(parser, null));
230-
assertEquals("[1:15] [_script] unknown field [bad_field], parser not found", e.getMessage());
230+
assertEquals("[1:29] [_script] unknown field [bad_field], parser not found", e.getMessage());
231231
}
232232
}
233233

@@ -240,7 +240,7 @@ public void testParseBadFieldNameExceptionsOnStartObject() throws IOException {
240240
parser.nextToken();
241241

242242
XContentParseException e = expectThrows(XContentParseException.class, () -> ScriptSortBuilder.fromXContent(parser, null));
243-
assertEquals("[1:15] [_script] unknown field [bad_field], parser not found", e.getMessage());
243+
assertEquals("[1:29] [_script] unknown field [bad_field], parser not found", e.getMessage());
244244
}
245245
}
246246

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedConfigTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public void testFutureConfigParse() throws IOException {
268268
.createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, FUTURE_DATAFEED);
269269
XContentParseException e = expectThrows(XContentParseException.class,
270270
() -> DatafeedConfig.STRICT_PARSER.apply(parser, null).build());
271-
assertEquals("[6:5] [datafeed_config] unknown field [tomorrows_technology_today], parser not found", e.getMessage());
271+
assertEquals("[6:35] [datafeed_config] unknown field [tomorrows_technology_today], parser not found", e.getMessage());
272272
}
273273

274274
public void testPastQueryConfigParse() throws IOException {

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/JobTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void testFutureConfigParse() throws IOException {
8181
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, FUTURE_JOB);
8282
XContentParseException e = expectThrows(XContentParseException.class,
8383
() -> Job.STRICT_PARSER.apply(parser, null).build());
84-
assertEquals("[4:5] [job_details] unknown field [tomorrows_technology_today], parser not found", e.getMessage());
84+
assertEquals("[4:35] [job_details] unknown field [tomorrows_technology_today], parser not found", e.getMessage());
8585
}
8686

8787
public void testFutureMetadataParse() throws IOException {

x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/output/AutodetectResultsParserTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ public void testParse_GivenUnknownObject() throws ElasticsearchParseException, I
390390
XContentParseException e = expectThrows(XContentParseException.class,
391391
() -> parser.parseResults(inputStream).forEachRemaining(a -> {
392392
}));
393-
assertEquals("[1:3] [autodetect_result] unknown field [unknown], parser not found", e.getMessage());
393+
assertEquals("[1:13] [autodetect_result] unknown field [unknown], parser not found", e.getMessage());
394394
}
395395
}
396396

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/text/TextTemplateTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public void testParserInvalidUnknownScriptType() throws Exception {
210210
XContentParser parser = createParser(JsonXContent.jsonXContent, bytes);
211211
parser.nextToken();
212212
XContentParseException ex = expectThrows(XContentParseException.class, () -> TextTemplate.parse(parser));
213-
assertEquals("[1:2] [script] unknown field [template], parser not found", ex.getMessage());
213+
assertEquals("[1:13] [script] unknown field [template], parser not found", ex.getMessage());
214214
}
215215

216216
public void testParserInvalidMissingText() throws Exception {
@@ -222,7 +222,7 @@ public void testParserInvalidMissingText() throws Exception {
222222
XContentParser parser = createParser(JsonXContent.jsonXContent, bytes);
223223
parser.nextToken();
224224
XContentParseException ex = expectThrows(XContentParseException.class, () -> TextTemplate.parse(parser));
225-
assertEquals("[1:2] [script] unknown field [type], parser not found", ex.getMessage());
225+
assertEquals("[1:9] [script] unknown field [type], parser not found", ex.getMessage());
226226
}
227227

228228
public void testNullObject() throws Exception {

0 commit comments

Comments
 (0)