This repository was archived by the owner on Apr 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(nuxt): improve error dx for users #4539
Merged
Merged
Changes from all commits
Commits
Show all changes
54 commits
Select commit
Hold shift + click to select a range
6545667
feat(nuxt): support creating errors directly from `throwError`
danielroe ab3bca3
fix(nuxt): clear errors _after_ route resolves
danielroe 1481e00
docs: update documentation
danielroe 8b60613
Merge remote-tracking branch 'origin/main' into fix/error-dx
danielroe 4c899c7
style: spacing
danielroe 3b205ca
fix: don't re-create errors
danielroe 5a8bcb6
Update docs/content/2.guide/2.features/7.error-handling.md
danielroe d224281
Update docs/content/3.api/3.utils/throw-error.md
danielroe 727e585
Update docs/content/2.guide/2.features/7.error-handling.md
danielroe 3983e87
Update packages/nuxt/src/app/composables/error.ts
danielroe 75466e6
docs: check error value
danielroe a4af259
Merge remote-tracking branch 'origin/main' into fix/error-dx
danielroe 358f914
Merge remote-tracking branch 'origin/main' into fix/error-dx
danielroe 5f4a112
fix: restore code missed in merge
danielroe aede51c
fix: support `throw createError()` directly
danielroe 8426fd5
Merge branch 'main' into fix/error-dx
danielroe b751402
Merge remote-tracking branch 'origin/main' into fix/error-dx
danielroe ab4c4d5
Merge branch 'main' into fix/error-dx
danielroe 8c7849d
fix: export/auto-import `createError` from composables
danielroe b0b5a91
fix: implement fuller wrapper
danielroe 5a289b9
Merge remote-tracking branch 'origin/main' into fix/error-dx
danielroe 6ffa080
fix: export `createError`
danielroe 912ab0a
Merge branch 'main' into fix/error-dx
danielroe 6b9131b
Merge branch 'main' into fix/error-dx
danielroe 71fbf23
Merge remote-tracking branch 'origin/main' into fix/error-dx
danielroe d846c00
docs: show underlying options for error
danielroe 2124e3f
fix: move checks into `createError`
danielroe 31a6b06
Merge branch 'main' into fix/error-dx
danielroe bbafd5e
fix: use `isError` from `h3` rather than instanceof
danielroe 09fc189
docs: remove createError import
danielroe 1e75c8c
refactor: isNuxtError
danielroe 8e902b7
Merge remote-tracking branch 'origin/main' into fix/error-dx
danielroe 88a42fa
docs: add blank `createError` page
danielroe 18ebba3
fix: use native createError support for strings
danielroe 711f4ec
Merge remote-tracking branch 'origin/main' into fix/error-dx
danielroe a82450a
fix: set default `statusMessage` if one is not provided
danielroe 8bdcee9
chore: upgrade h3
danielroe 1713806
Merge branch 'main' into fix/error-dx
danielroe 5720255
chore: dedupe h3
danielroe 6ce6521
Merge remote-tracking branch 'origin/main' into fix/error-dx
danielroe 3d66ed7
chore: dedupe
danielroe 95852e5
chore: dedupe for real
danielroe aa8086b
Merge remote-tracking branch 'origin/main' into fix/error-dx
danielroe 0dfe1e5
fix: auto import `isNuxtError`
danielroe bfb4a08
Merge branch 'main' into fix/error-dx
pi0 f72cff3
fix: errors are not fatal by default on client-side unless `fatal: true`
danielroe 2cce81a
Merge remote-tracking branch 'origin/main' into fix/error-dx
danielroe 992a0f7
docs: remove stray wor
danielroe 9bba606
refactor: rename `throwError` -> `showError`, deprecating old name
danielroe 5aee7cf
Merge branch 'main' into fix/error-dx
pi0 f61e13c
upgrade h3 and reuse fatal
pi0 b04dda0
only log unhandled errors
pi0 e69bdf8
update ui templates
pi0 444a59c
Merge branch 'main' into fix/error-dx
pi0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# `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. | ||
|
||
**Parameters:** | ||
|
||
* err: { cause, data, message, name, stack, statusCode, statusMessage, fatal } | ||
|
||
## Throwing errors in your 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> | ||
const route = useRoute() | ||
const { data } = await useFetch(`/api/movies/${route.params.slug}`) | ||
if (!data.value) { | ||
throw createError({ statusCode: 404, statusMessage: 'Page Not Found' }) | ||
} | ||
</script> | ||
``` | ||
|
||
## Throwing errors in API routes | ||
|
||
You can use `createError` to trigger error handling in server API routes. | ||
|
||
### Example | ||
|
||
```js | ||
export default eventHandler(() => { | ||
throw createError({ | ||
statusCode: 404, | ||
statusMessage: 'Page Not Found' | ||
}) | ||
} | ||
``` | ||
|
||
::ReadMore{link="/guide/features/error-handling"} | ||
:: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# `showError` | ||
|
||
Nuxt provides a quick and simple way to show a full screen error page if needed. | ||
|
||
Within your pages, components and plugins you can use `showError` to show an error error. | ||
|
||
**Parameters:** | ||
|
||
- `error`: `string | Error | Partial<{ cause, data, message, name, stack, statusCode, statusMessage }>` | ||
|
||
```js | ||
showError("π± Oh no, an error has been thrown.") | ||
showError({ statusCode: 404, statusMessage: "Page Not Found" }) | ||
``` | ||
|
||
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. | ||
|
||
`showError` calls the `app:error` hook. | ||
|
||
::ReadMore{link="/guide/features/error-handling"} | ||
:: |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export default defineNuxtRouteMiddleware((to) => { | ||
if ('middleware' in to.query) { | ||
return throwError('error in middleware') | ||
return showError('error in middleware') | ||
} | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.