Skip to content
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

Improve error message for parse failures of completion fields #27297

Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ private void parse(ParseContext parseContext, Token token, XContentParser parser
}
}
} else {
throw new ElasticsearchParseException("failed to parse expected text or object got" + token.name());
throw new ElasticsearchParseException("failed to parse [" + parser.currentName() + "][" + parser.getTokenLocation() + "]: expected text or object, but got " + token.name());
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if it would make sense to use ParsingException otherwise, which supports token location natively.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yes, that's nicer than putting it in the string. Thanks.

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,28 @@ public void testParsingMinimal() throws Exception {
assertSuggestFields(fields, 1);
}

public void testParsingFailure() throws Exception {
String mapping = jsonBuilder().startObject().startObject("type1")
.startObject("properties").startObject("completion")
.field("type", "completion")
.endObject().endObject()
.endObject().endObject().string();

DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type1", new CompressedXContent(mapping));

try {
defaultMapper.parse(SourceToParse.source("test", "type1", "1", XContentFactory.jsonBuilder()
Copy link
Member

Choose a reason for hiding this comment

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

you could use expectThrows here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah thanks, I was just about to ask for a better way to do this :)

.startObject()
.field("completion", 1.0)
.endObject()
.bytes(),
XContentType.JSON));
fail("Expected parse() to throw an exception, but it did not.");
} catch (MapperParsingException e) {
assertEquals("failed to parse [completion][1:15]: expected text or object, but got VALUE_NUMBER", e.getCause().getMessage());
}
}

public void testParsingMultiValued() throws Exception {
String mapping = jsonBuilder().startObject().startObject("type1")
.startObject("properties").startObject("completion")
Expand Down