mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 07:05:11 +00:00
docs: string vs object errors + accessing data of server-thrown errors (#27398)
This commit is contained in:
parent
1341687320
commit
345a8799f7
@ -150,10 +150,10 @@ Read more about `useError` composable.
|
||||
### `createError`
|
||||
|
||||
```ts [TS Signature]
|
||||
function createError (err: { cause, data, message, name, stack, statusCode, statusMessage, fatal }): Error
|
||||
function createError (err: string | { cause, data, message, name, stack, statusCode, statusMessage, fatal }): Error
|
||||
```
|
||||
|
||||
Create an error object with additional metadata. It is usable in both the Vue and Server portions of your app, and is meant to be thrown.
|
||||
Create an error object with additional metadata. You can pass a string to be set as the error `message` or an object containing error properties. It is usable in both the Vue and Server portions of your app, and is meant to be thrown.
|
||||
|
||||
If you throw an error created with `createError`:
|
||||
- on server-side, it will trigger a full-screen error page which you can clear with [`clearError`](#clearerror).
|
||||
|
@ -12,7 +12,9 @@ You can use this function to create an error object with additional metadata. It
|
||||
|
||||
## Parameters
|
||||
|
||||
- `err`: `{ cause, data, message, name, stack, statusCode, statusMessage, fatal }`
|
||||
- `err`: `string | { cause, data, message, name, stack, statusCode, statusMessage, fatal }`
|
||||
|
||||
You can pass either a string or an object to the `createError` function. If you pass a string, it will be used as the error `message`, and the `statusCode` will default to `500`. If you pass an object, you can set multiple properties of the error, such as `statusCode`, `message`, and other error properties.
|
||||
|
||||
## In Vue App
|
||||
|
||||
@ -48,4 +50,6 @@ export default eventHandler(() => {
|
||||
})
|
||||
```
|
||||
|
||||
In API routes, using `createError` by passing an object with a short `statusMessage` is recommended because it can be accessed on the client side. Otherwise, a `message` passed to `createError` on an API route will not propagate to the client. Alternatively, you can use the `data` property to pass data back to the client. In any case, always consider avoiding to put dynamic user input to the message to avoid potential security issues.
|
||||
|
||||
:read-more{to="/docs/getting-started/error-handling"}
|
||||
|
Loading…
Reference in New Issue
Block a user