You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 6, 2023. It is now read-only.
You can call this function at any point on client-side, or (on server side) directly within middleware, plugins or `setup()` functions. It will trigger a full-screen error page (as above) which you can clear with `clearError`.
87
+
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.
88
88
89
-
::ReadMore{link="/api/utils/throw-error"}
89
+
If you throw an error created with `createError`:
90
+
91
+
* on server-side, it will trigger a full-screen error page which you can clear with `clearError`.
92
+
* 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`.
93
+
94
+
### Example
95
+
96
+
```vue [pages/movies/[slug].vue]
97
+
<script setup>
98
+
const route = useRoute()
99
+
const { data } = await useFetch(`/api/movies/${route.params.slug}`)
100
+
if (!data.value) {
101
+
throw createError({ statusCode: 404, statusMessage: 'Page Not Found' })
You can call this function at any point on client-side, or (on server side) directly within middleware, plugins or `setup()` functions. It will trigger a full-screen error page which you can clear with `clearError`.
111
+
112
+
It is recommended instead to use `throw createError()`.
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.
* on server-side, it will trigger a full-screen error page which you can clear with `clearError`.
14
+
* 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`.
15
+
16
+
### Example
17
+
18
+
```vue [pages/movies/[slug].vue]
19
+
<script setup>
20
+
const route = useRoute()
21
+
const { data } = await useFetch(`/api/movies/${route.params.slug}`)
22
+
if (!data.value) {
23
+
throw createError({ statusCode: 404, statusMessage: 'Page Not Found' })
24
+
}
25
+
</script>
26
+
```
27
+
28
+
## Throwing errors in API routes
29
+
30
+
You can use `createError` to trigger error handling in server API routes.
showError({ statusCode:404, statusMessage:"Page Not Found" })
14
+
```
15
+
16
+
The error is set in the state using [`useError()`](/api/composables/use-error) to create a reactive and SSR-friendly shared error state across components.
0 commit comments