mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-24 14:45:15 +00:00
f26a801775
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Daniel Roe <daniel@roe.dev>
52 lines
1.4 KiB
Markdown
52 lines
1.4 KiB
Markdown
---
|
|
title: 'createError'
|
|
description: Create an error object with additional metadata.
|
|
links:
|
|
- label: Source
|
|
icon: i-simple-icons-github
|
|
to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/error.ts
|
|
size: xs
|
|
---
|
|
|
|
You can use this function to create an error object with additional metadata. It is usable in both the Vue and Nitro portions of your app, and is meant to be thrown.
|
|
|
|
## Parameters
|
|
|
|
- `err`: `{ cause, data, message, name, stack, statusCode, statusMessage, fatal }`
|
|
|
|
## In Vue App
|
|
|
|
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`.
|
|
- on client-side, it will throw a non-fatal error for you to handle. If you need to trigger a full-screen error page, then you can do this by setting `fatal: true`.
|
|
|
|
### Example
|
|
|
|
```vue [pages/movies/[slug\\].vue]
|
|
<script setup lang="ts">
|
|
const route = useRoute()
|
|
const { data } = await useFetch(`/api/movies/${route.params.slug}`)
|
|
if (!data.value) {
|
|
throw createError({ statusCode: 404, statusMessage: 'Page Not Found' })
|
|
}
|
|
</script>
|
|
```
|
|
|
|
## In API Routes
|
|
|
|
Use `createError` to trigger error handling in server API routes.
|
|
|
|
### Example
|
|
|
|
```js
|
|
export default eventHandler(() => {
|
|
throw createError({
|
|
statusCode: 404,
|
|
statusMessage: 'Page Not Found'
|
|
})
|
|
})
|
|
```
|
|
|
|
:read-more{to="/docs/getting-started/error-handling"}
|