-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[v4] Dev docs: Add error handling #460
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3217c7a
Create error handling documentation
pwizla ece38db
Add links in REST and GraphQL API docs
pwizla 74a0c51
Add entry in sidebar TOC
pwizla ab13ec0
Make h3 titles consistent
pwizla 0416149
Improve wording
pwizla 2af5547
Update docs/developer-docs/latest/developer-resources/error-handling.md
pwizla c5f4f96
Update JSONs
pwizla eb8e55e
Improve based on Mégane's and Pierre N's feedback
pwizla 25d98c4
Update with Pierre N feedback
pwizla File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
docs/developer-docs/latest/developer-resources/error-handling.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
--- | ||
title: Error handling - Strapi Developer Documentation | ||
description: … | ||
--- | ||
|
||
<!-- TODO: update SEO --> | ||
|
||
# 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](/developer-docs/latest/developer-resources/database-apis-reference/rest-api.md) or [GraphQL](/developer-docs/latest/developer-resources/database-apis-reference/graphql-api.md) APIs, you might [receive errors](#receiving-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](#throwing-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](/developer-docs/latest/developer-resources/database-apis-reference/rest-api.html#unified-response-format) that has the following format: | ||
|
||
```json | ||
{ | ||
"data": null, | ||
"error": { | ||
"status": "", // HTTP status | ||
"name": "", // Strapi error name ('ApplicationError' or 'ValidationError') | ||
"message": "", // A human reable error message | ||
"details": { | ||
// error info specific to the error type | ||
} | ||
} | ||
} | ||
``` | ||
|
||
<!-- TODO: add "types" (aka error `name`) list once settled --> | ||
|
||
### GraphQL errors | ||
|
||
Errors thrown by the GraphQL API are included in the [response](/developer-docs/latest/developer-resources/database-apis-reference/graphql-api.html#unified-response-format) that has the following format: | ||
|
||
```json | ||
{ "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 | ||
} | ||
} | ||
``` | ||
|
||
## Throwing errors | ||
|
||
The recommended way to throw errors when developing any custom logic with Strapi is to have the [controller](/developer-docs/latest/development/backend-customization/controllers.md) 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](https://github.com/jshttp/http-errors#list-of-all-constructors) but their name should be 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](#receiving-errors) 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 | ||
|
||
```js | ||
|
||
// path: ./src/api/[api-name]/controllers/my-controller.js | ||
|
||
module.exports = { | ||
renameDog: async (ctx, next) => { | ||
const newName = ctx.request.body.name; | ||
if (!newName) { | ||
return ctx.badRequest('name is missing', { foo: 'bar' }) | ||
} | ||
ctx.body = strapi.service('api::dog.dog').rename(newName); | ||
} | ||
} | ||
pwizla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
``` | ||
|
||
:::note | ||
[Services](/developer-docs/latest/development/backend-customization/services.md) don't have access to the controller's `ctx` object. If services need to throw errors, these need to be caught by the controller, that in turn is in charge of calling the proper error function. | ||
::: |
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.
Uh oh!
There was an error while loading. Please reload this page.