# Error handling
Strapi is natively handling errors with a standard format.
There are 2 use cases for error handling:
- As a developer querying content through the REST or GraphQL APIs, you might receive errors in response to the requests.
- As a developer customizing the backend of your Strapi application, you could use controllers and services to throw errors.
# Receiving errors
Errors are included in the response object with the error
key and include information such as the HTTP status code, the name of the error, and additional information.
# REST errors
Errors thrown by the REST API are included in the response that has the following format:
{
"data": null,
"error": {
"status": "", // HTTP status
"name": "", // Strapi error name ('ApplicationError' or 'ValidationError')
"message": "", // A human readable error message
"details": {
// error info specific to the error type
}
}
}
2
3
4
5
6
7
8
9
10
11
# GraphQL errors
Errors thrown by the GraphQL API are included in the response that has the following format:
{ "errors": [
{
"message": "", // A human reable error message
"extensions": {
"error": {
"name": "", // Strapi error name ('ApplicationError' or 'ValidationError'),
"message": "", // A human reable error message (same one as above);
"details": {}, // Error info specific to the error type
},
"code": "" // GraphQL error code (ex: BAD_USER_INPUT)
}
}
],
"data": {
"graphQLQueryName": null
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Throwing errors
# Controllers and middlewares
The recommended way to throw errors when developing any custom logic with Strapi is to have the controller or middleware respond with the correct status and body.
This can be done by calling an error function on the context (i.e. ctx
). Available error functions are listed in the http-errors documentation (opens new window) but their name should be lower camel-cased to be used by Strapi (e.g. badRequest
).
Error functions accept 2 parameters that correspond to the error.message
and error.details
attributes received by a developer querying the API:
- the first parameter of the function is the error
message
- and the second one is the object that will be set as
details
in the response received
# Services and models lifecycles
Once you are working at a deeper layer than the controllers or middlewares there are dedicated error classes that can be used to throw errors. These classes are extensions of Node Error
class (opens new window) and are specifically targeted for certain use-cases.
These error classes are imported through the @strapi/utils
package and can be called from several different layers. The following examples use the service layer but error classes are not just limited to services and model lifecycles. When throwing errors in the model lifecycle layer, it's recommended to use the ApplicationError
class so that proper error messages are shown in the admin panel.
✏️ NOTE
See the default error classes section for more information on the error classes provided by Strapi.
Example: Throwing an error in a service
This example shows wrapping a core service and doing a custom validation on the create
method:
Example: Throwing an error in a model lifecycle
This example shows building a custom model lifecyle and being able to throw an error that stops the request and will return proper error messages to the admin panel. Generally you should only throw an error in beforeX
lifecycles, not afterX
lifecycles.
# Policies
Policies are a special type of middleware that are executed before a controller. They are used to check if the user is allowed to perform the action or not. If the user is not allowed to perform the action and a return false
is used then a generic error will be thrown. As an alternative, you can throw a custom error message using a nested class extensions from the Strapi ForbiddenError
class, ApplicationError
class (see Default error classes for both classes), and finally the Node Error
class (opens new window).
The PolicyError
class is available from @strapi/utils
package and accepts 2 parameters:
- the first parameter of the function is the error
message
- (optional) the second parameter is the object that will be set as
details
in the response received; a best practice is to set apolicy
key with the name of the policy that threw the error.
Example: Throwing a PolicyError in a custom policy
This example shows building a custom policy that will throw a custom error message and stop the request.
# Default error classes
The default error classes are available from the @strapi/utils
package and can be imported and used in your code. Any of the default error classes can be extended to create a custom error class. The custom error class can then be used in your code to throw errors.