2022-10-06 09:15:30 +00:00
---
2023-10-18 10:59:43 +00:00
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
2022-10-06 09:15:30 +00:00
---
2022-07-21 14:29:03 +00:00
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.
2023-10-18 10:59:43 +00:00
## Parameters
2022-07-21 14:29:03 +00:00
2023-10-18 10:59:43 +00:00
- `err` : `{ cause, data, message, name, stack, statusCode, statusMessage, fatal }`
2022-07-21 14:29:03 +00:00
2023-10-18 10:59:43 +00:00
## In Vue App
2022-07-21 14:29:03 +00:00
If you throw an error created with `createError` :
2023-10-18 10:59:43 +00:00
- 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` .
2022-07-21 14:29:03 +00:00
### Example
2023-08-03 11:05:29 +00:00
```vue [pages/movies/[slug\\].vue]
2023-07-18 10:31:45 +00:00
< script setup lang = "ts" >
2022-07-21 14:29:03 +00:00
const route = useRoute()
const { data } = await useFetch(`/api/movies/${route.params.slug}`)
if (!data.value) {
throw createError({ statusCode: 404, statusMessage: 'Page Not Found' })
}
< / script >
```
2023-10-18 10:59:43 +00:00
## In API Routes
2022-07-21 14:29:03 +00:00
2022-10-06 09:15:30 +00:00
Use `createError` to trigger error handling in server API routes.
2022-07-21 14:29:03 +00:00
### Example
```js
export default eventHandler(() => {
throw createError({
statusCode: 404,
statusMessage: 'Page Not Found'
})
2023-07-02 15:03:05 +00:00
})
2022-07-21 14:29:03 +00:00
```
2023-10-18 10:59:43 +00:00
:read-more{to="/docs/getting-started/error-handling"}