Skip to content

Introduce DocumentParsingException#92646

Merged
romseygeek merged 27 commits intoelastic:mainfrom
romseygeek:exceptions/mapper-parsing
Mar 31, 2023
Merged

Introduce DocumentParsingException#92646
romseygeek merged 27 commits intoelastic:mainfrom
romseygeek:exceptions/mapper-parsing

Conversation

@romseygeek
Copy link
Copy Markdown
Contributor

@romseygeek romseygeek commented Jan 3, 2023

Document parsing methods currently throw MapperParsingException. This
isn't very helpful, as it doesn't contain any information about where the parse
error happened - it is designed for parsing mappings, which are realised into
java maps before being examined. This commit introduces a new exception
specifically for document parsing that extends XContentException, so that
it reports the current position of the parser as part of its error message.

Fixes #85083

@romseygeek romseygeek added >enhancement :Search/Search Search-related issues that do not fall into other categories v8.7.0 labels Jan 3, 2023
@romseygeek romseygeek requested a review from javanna January 3, 2023 16:11
@romseygeek romseygeek self-assigned this Jan 3, 2023
@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

Pinging @elastic/es-search (Team:Search)

@elasticsearchmachine elasticsearchmachine added the Team:Search Meta label for search team label Jan 3, 2023
@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

Hi @romseygeek, I've created a changelog YAML for you.

@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

Hi @romseygeek, I've updated the changelog YAML for you.

Copy link
Copy Markdown
Contributor

@javanna javanna left a comment

Choose a reason for hiding this comment

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

Left a couple of comments, nothing major. One more thing I could use is a test where we check the full toXContent representation of the query, if we don't have it yet (I may have missed it)

- match: { items.0.index.error.type: mapper_parsing_exception }
- match: { items.0.index.error.reason: "Can't find dynamic template for dynamic template name [bar_foo] of field [foo_location]"}
- match: { items.0.index.error.type: document_parsing_exception }
- match: { items.0.index.error.reason: "[-1:-1] failed to parse: Can't find dynamic template for dynamic template name [bar_foo] of field [foo_location]"}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That -1:-1 isn't great? Shall we omit printing out the location when it's not set? Or should it be set to the location where the field appears first ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've changed this so that it doesn't output the location when it's UNKNOWN

}

return new MapperParsingException("failed to parse", e);
return new DocumentParsingException(XContentLocation.UNKNOWN, "failed to parse: " + e.getMessage(), e);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We can't really provide a more useful location in these cases?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In some cases there's no information, but I've reworked things a bit so that we can catch jackson errors that do include location data and include that in the error message.


private static MapperParsingException wrapInMapperParsingException(SourceToParse source, Exception e) {
private static DocumentParsingException wrapInDocumentParsingException(SourceToParse source, Exception e) {
// if its already a mapper parsing exception, no need to wrap it...
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

either remove the comment or update it?

documentParserContext.addIgnoredField(name());
} else {
throw new MapperParsingException("Error executing script on field [" + name() + "]", e);
throw new DocumentParsingException(XContentLocation.UNKNOWN, "Error executing script on field [" + name() + "]", e);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this makes me wonder if we should use this exception when we don't have a location. Is this a document parsing error?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think this still counts as an error in document parsing? It's still definitely not a mapper parsing exception...

@rjernst rjernst added v8.8.0 and removed v8.7.0 labels Feb 8, 2023
@romseygeek
Copy link
Copy Markdown
Contributor Author

I've updated this in response to your feedback @javanna. CI will not pass for a while as I need to chase down a bunch of error message changes in yaml tests, but I think it's ready for review otherwise.

@javanna
Copy link
Copy Markdown
Contributor

javanna commented Feb 24, 2023

@romseygeek this was already approved, feel free to merge when ready.

@romseygeek romseygeek merged commit 093e36c into elastic:main Mar 31, 2023
romseygeek added a commit that referenced this pull request Apr 21, 2023
This was incorrectly changed in #92646 but never caught as it's a super
rare path for the test to take.

Fixes #95047
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

>enhancement :Search/Search Search-related issues that do not fall into other categories Team:Search Meta label for search team v8.8.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add token location to exceptions thrown when parsing documents

4 participants