mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 13:45:18 +00:00
docs: add description of returning different status codes (#10059)
This commit is contained in:
parent
cb607b84ad
commit
49a2dd5d5a
@ -163,6 +163,37 @@ export default defineEventHandler((event) => {
|
||||
})
|
||||
```
|
||||
|
||||
### Error handling
|
||||
|
||||
If no errors are thrown, a status code of `200 OK` will be returned. Any uncaught errors will return a `500 Internal Server Error` HTTP Error.
|
||||
|
||||
To return other error codes, throw an exception with `createError`
|
||||
|
||||
```ts [server/api/validation/[id].ts]
|
||||
export default defineEventHandler((event) => {
|
||||
const id = parseInt(event.context.params.id) as number
|
||||
if (!Number.isInteger(id)) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'ID should be an integer',
|
||||
})
|
||||
}
|
||||
return 'All good'
|
||||
})
|
||||
```
|
||||
|
||||
### Returning other status codes
|
||||
|
||||
To return other status codes, you can use the `setResponseStatus` utility.
|
||||
|
||||
For example, to return `202 Accepted`
|
||||
|
||||
```ts [server/api/validation/[id].ts]
|
||||
export default defineEventHandler((event) => {
|
||||
setResponseStatus(event, 202)
|
||||
})
|
||||
```
|
||||
|
||||
### Accessing Runtime Config
|
||||
|
||||
```ts [server/api/foo.ts]
|
||||
|
Loading…
Reference in New Issue
Block a user