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

Return better messages for JSON errors in API. #18367

Merged
merged 17 commits into from
Jun 3, 2024

Conversation

dennisoelkers
Copy link
Member

@dennisoelkers dennisoelkers commented Feb 23, 2024

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 deserialize value of type `int` from String \"foo\": not a valid `int` value\n at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 11, column: 13] (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\"]->org.graylog2.plugin.indexer.searches.timeranges.AutoValue_RelativeRange$Builder[\"from\"])"
}

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",
	"reference_path": "org.graylog.plugins.views.search.rest.AutoValue_SearchDTO$Builder[\"queries\"]->java.util.LinkedHashSet[0]->org.graylog.plugins.views.search.rest.AutoValue_QueryDTO$Builder[\"timerange\"]->org.graylog2.plugin.indexer.searches.timeranges.AutoValue_RelativeRange$Builder[\"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",
	"reference_path": "org.graylog.plugins.views.search.rest.AutoValue_SearchDTO$Builder[\"queries\"]->java.util.LinkedHashSet[0]->org.graylog.plugins.views.search.rest.AutoValue_QueryDTO$Builder[\"timerange\"]"
}

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Refactoring (non-breaking change)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.

@dennisoelkers dennisoelkers marked this pull request as ready for review May 23, 2024 06:52
@dennisoelkers dennisoelkers requested a review from a team May 23, 2024 06:59
Copy link
Member

@bernd bernd left a comment

Choose a reason for hiding this comment

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

Thank you! I have some comments.

The new error message no longer includes any type information about which class the issue is about.

Example:

Cannot construct instance of `org.graylog.plugins.views.search.searchtypes.events.EventList$Builder`

That information doesn't seem to be part of the response anymore. I was using the information as a starting point for debugging.

Can we bring that information back?

@@ -0,0 +1,4 @@
type = "a"
message = "Improving API error responses for JSON parsing failures.."
Copy link
Member

Choose a reason for hiding this comment

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

Double full stop.

Suggested change
message = "Improving API error responses for JSON parsing failures.."
message = "Improving API error responses for JSON parsing failures."

Copy link
Member Author

@dennisoelkers dennisoelkers May 29, 2024

Choose a reason for hiding this comment

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

@@ -0,0 +1,4 @@
type = "a"
Copy link
Member

Choose a reason for hiding this comment

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

Let's make it a c because it's a change in the API error responses.

Copy link
Member Author

Choose a reason for hiding this comment

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

@dennisoelkers dennisoelkers requested a review from bernd May 29, 2024 10:18
Copy link
Member

@bernd bernd left a comment

Choose a reason for hiding this comment

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

Thank you!

@dennisoelkers dennisoelkers merged commit 9830372 into master Jun 3, 2024
5 checks passed
@dennisoelkers dennisoelkers deleted the feat/improve-api-responses-for-json-errors branch June 3, 2024 06:32
dennisoelkers added a commit that referenced this pull request Jun 7, 2024
* Return better messages for JSON errors in API.

* Improving structure once again.

* Include more attributes in specialized error class.

* Adding license header.

* Adding changelog snippet.

* Handling missing target type.

* Including reference path if present.

* Fixing up changelog, adding upgrade notes.

* Adding test for JSON parsing error responses.

* Constructing reference path for missing required property.

* Handling JSON parsing error on root level.

* Consolidating `JacksonPropertyExceptionMapper` to handle invalid properties in a structured way.

* Adjusting test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants