mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-29 09:02:03 +00:00
docs: add separate docs page for error.vue
(#25320)
This commit is contained in:
parent
ae8bbe0b04
commit
979ee466c1
@ -101,36 +101,10 @@ const handleError = () => clearError({ redirect: '/' })
|
|||||||
</template>
|
</template>
|
||||||
```
|
```
|
||||||
|
|
||||||
::callout
|
::read-more{to="/docs/guide/directory-structure/error"}
|
||||||
Although it is called an 'error page' it's not a route and shouldn't be placed in your `~/pages` directory. For the same reason, you shouldn't use `definePageMeta` within this page.
|
Read more about `error.vue` and its uses.
|
||||||
::
|
::
|
||||||
|
|
||||||
The error page has a single prop - `error` which contains an error for you to handle.
|
|
||||||
|
|
||||||
The `error` object provides the fields:
|
|
||||||
```ts
|
|
||||||
{
|
|
||||||
url: string
|
|
||||||
statusCode: number
|
|
||||||
statusMessage: string
|
|
||||||
message: string
|
|
||||||
description: string
|
|
||||||
data: any
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
If you have an error with custom fields they will be lost; you should assign them to `data` instead:
|
|
||||||
|
|
||||||
```ts
|
|
||||||
throw createError({
|
|
||||||
statusCode: 404,
|
|
||||||
statusMessage: 'Page Not Found',
|
|
||||||
data: {
|
|
||||||
myCustomField: true
|
|
||||||
}
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
For custom errors we highly recommend to use `onErrorCaptured` composable that can be called in a page/component setup function or `vue:error` runtime nuxt hook that can be configured in a nuxt plugin.
|
For custom errors we highly recommend to use `onErrorCaptured` composable that can be called in a page/component setup function or `vue:error` runtime nuxt hook that can be configured in a nuxt plugin.
|
||||||
|
|
||||||
```ts [plugins/error-handler.ts]
|
```ts [plugins/error-handler.ts]
|
||||||
|
53
docs/2.guide/2.directory-structure/3.error.md
Normal file
53
docs/2.guide/2.directory-structure/3.error.md
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
---
|
||||||
|
title: "error.vue"
|
||||||
|
description: "The error.vue file is the error page in your Nuxt application."
|
||||||
|
head.title: "error.vue"
|
||||||
|
navigation.icon: i-ph-file-duotone
|
||||||
|
---
|
||||||
|
|
||||||
|
During the lifespan of your application, some errors may appear unexpectedly at runtime. In such case, we can use the `error.vue` file to override the default error files and display the error nicely.
|
||||||
|
|
||||||
|
```vue [error.vue]
|
||||||
|
<script setup lang="ts">
|
||||||
|
const props = defineProps({
|
||||||
|
error: Object
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
<h1>{{ error.statusCode }}</h1>
|
||||||
|
<NuxtLink to="/">Go back home</NuxtLink>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
```
|
||||||
|
|
||||||
|
::callout
|
||||||
|
Although it is called an 'error page' it's not a route and shouldn't be placed in your `~/pages` directory. For the same reason, you shouldn't use `definePageMeta` within this page.
|
||||||
|
::
|
||||||
|
|
||||||
|
The error page has a single prop - `error` which contains an error for you to handle.
|
||||||
|
|
||||||
|
The `error` object provides the following fields:
|
||||||
|
```ts
|
||||||
|
{
|
||||||
|
url: string
|
||||||
|
statusCode: number
|
||||||
|
statusMessage: string
|
||||||
|
message: string
|
||||||
|
description: string
|
||||||
|
data: any
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
If you have an error with custom fields they will be lost; you should assign them to `data` instead:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
throw createError({
|
||||||
|
statusCode: 404,
|
||||||
|
statusMessage: 'Page Not Found',
|
||||||
|
data: {
|
||||||
|
myCustomField: true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
```
|
Loading…
Reference in New Issue
Block a user