Dealing with errors
There are multiple different types of errors in GraphQL and each can be handled differently.
In this guide we will outline the different types of errors that you will encounter when building a GraphQL server.
Note: By default Strawberry will log all execution errors to a
strawberry.execution
logger:
/docs/types/schema#handling-execution-errors .
GraphQL validation errors
GraphQL is strongly typed and so Strawberry validates all queries before
executing them. If a query is invalid it isnβt executed and instead the response
contains an errors
list:
Each error has a message, line, column and path to help you identify what part of the query caused the error.
The validation rules are part of the GraphQL specification and built into Strawberry, so thereβs not really a way to customize this behavior. You can disable all validation by using the DisableValidation extension.
GraphQL type errors
When a query is executed each field must resolve to the correct type. For example non-null fields cannot return None.
Each error has a message, line, column and path to help you identify what part of the query caused the error.
Unhandled execution errors
Sometimes a resolver will throw an unexpected error due to a programming error
or an invalid assumption. When this happens Strawberry catches the error and
exposes it in the top level errors
field in the response.
Expected errors
If an error is expected then it is often best to express it in the schema. This allows the client to deal with the error in a robust way.
This could be achieved by making the field optional when there is a possibility that the data wonβt exist:
When the expected error is more complicated itβs a good pattern to instead return a union of types that either represent an error or a success response. This pattern is often adopted with mutations where itβs important to be able to return more complicated error details to the client.
For example, say you have a registerUser
mutation where you need to deal with
the possibility that a user tries to register with a username that already
exists. You might structure your mutation type like this:
Then your client can look at the __typename
of the result to determine what to
do next:
This approach allows you to express the possible error states in the schema and so provide a robust interface for your client to account for all the potential outcomes from a mutation.
Additional resources:
A Guide to GraphQL Errors | productionreadygraphql.com
200 OK! Error Handling in GraphQL | sachee.medium.com