Make top-level errors consistent and observable #6514
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Previously, errors that weren't specially cased as part of the "request
pipeline" were not observable by plugins. (In some but not all cases,
they were visible to formatError hooks.) Some "HTTP-level" errors were
handled by returning text/plain responses, whereas many other errors
were handled by returning GraphQL-style JSON responses (using
the formatError hook).
This change makes things more consistent by returning all errors as JSON
responses (using the formatError hook). In addition, for better
observability, several new top-level plugin APIs are introduced that are
called when various kinds of errors occur. Specifically:
New plugin hook
startupDidFail
. This is called once if startup fails(eg, if a serverWillStart plugin throws or if gateway fails to load
the schema). (If this hook throws, its error is logged.) Future calls to
executeHTTPGraphQLRequest (if the server was started in the
background) now return a formatted JSON error instead of throwing.
New plugin hook
contextCreationDidFail
. (If this hook throws, itserror is logged.) As before, these errors are sent as formatted JSON
errors.
New plugin hook
invalidRequestWasReceived
. This is called for allthe errors that occur before being able to construct a GraphQLRequest
from an HTTPGraphQLRequest; this includes things like the top-level
fields being of the wrong type, missing body, CSRF failure, etc).
These errors are now sent as formatted JSON instead of as text/plain.
New plugin hook
unexpectedErrorProcessingRequest
. This is called ifprocessGraphQLRequest
throws (typically because a plugin hookthrew). We are making the assumption that this means there is a
programming error, so we are making the behavior change that the
thrown error will be logged and "Internal server error" will be sent
in the HTTP response.
Fix grammar of "GET supports only query operation" and don't return it
if the operation is unknown.
Fixes #6140. Part of #6053.