graylog2-server
graylog2-server copied to clipboard
Return better messages for JSON errors in API.
Description
Motivation and Context
This PR is aiming to improve error messages returned by the API in case of JSON mapping errors. Previously, error messages caused by deserialization failures were of little help, because a) they were using Java fqcn's and b) they were too noisy. This PR is changing this to return a) the location of the error in the JSON input and b) the path to the property that failed to deserialize in a structured and concise way.
How Has This Been Tested?
Screenshots (if appropriate):
For unexpected type:
Before:
{
"type": "ApiError",
"message": "Cannot construct instance of `org.graylog.plugins.views.search.searchtypes.events.EventList$Builder`, problem: Unexpected field name for attribute filter: foo, allowed values: [priority, event_definition_id]\n at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 21, column: 5] (through reference chain: org.graylog.plugins.views.search.rest.AutoValue_SearchDTO$Builder[\"queries\"]->java.util.LinkedHashSet[0]->org.graylog.plugins.views.search.rest.AutoValue_QueryDTO$Builder[\"search_types\"]->java.util.HashSet[0])"
}
After:
{
"type": "RequestError",
"message": "Error at \"queries.[0].timerange.from\" [11, 13]: Must be of type int",
"line": 11,
"column": 13,
"path": "queries.[0].timerange.from"
}
For generic JSON parsing (superfluous ,
):
Before:
{
"type": "ApiError",
"message": "Unexpected character ('}' (code 125)): was expecting double-quote to start field name\n at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 12, column: 5] (through reference chain: org.graylog.plugins.views.search.rest.AutoValue_SearchDTO$Builder[\"queries\"]->java.util.LinkedHashSet[0]->org.graylog.plugins.views.search.rest.AutoValue_QueryDTO$Builder[\"timerange\"])"
}
After:
{
"type": "RequestError",
"message": "Error at \"queries.[0].timerange\" [12, 4]: Unexpected character ('}' (code 125)): was expecting double-quote to start field name\n at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 12, column: 4]",
"line": 12,
"column": 4,
"path": "queries.[0].timerange"
}
Types of changes
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [x] Refactoring (non-breaking change)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
Checklist:
- [x] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [x] I have read the CONTRIBUTING document.
- [ ] I have added tests to cover my changes.