mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 15:15:19 +00:00
feat(nuxt): add path to error.data
when throwing 404 errors (#24674)
This commit is contained in:
parent
10c69aa74e
commit
70dea8b83c
@ -246,7 +246,10 @@ export default defineNuxtPlugin<{ route: Route, router: Router }>({
|
|||||||
if (result === false || result instanceof Error) {
|
if (result === false || result instanceof Error) {
|
||||||
const error = result || createError({
|
const error = result || createError({
|
||||||
statusCode: 404,
|
statusCode: 404,
|
||||||
statusMessage: `Page Not Found: ${initialURL}`
|
statusMessage: `Page Not Found: ${initialURL}`,
|
||||||
|
data: {
|
||||||
|
path: initialURL
|
||||||
|
}
|
||||||
})
|
})
|
||||||
delete nuxtApp._processingMiddleware
|
delete nuxtApp._processingMiddleware
|
||||||
return nuxtApp.runWithContext(() => showError(error))
|
return nuxtApp.runWithContext(() => showError(error))
|
||||||
|
@ -209,7 +209,10 @@ const plugin: Plugin<{ router: Router }> = defineNuxtPlugin({
|
|||||||
await nuxtApp.runWithContext(() => showError(createError({
|
await nuxtApp.runWithContext(() => showError(createError({
|
||||||
statusCode: 404,
|
statusCode: 404,
|
||||||
fatal: false,
|
fatal: false,
|
||||||
statusMessage: `Page not found: ${to.fullPath}`
|
statusMessage: `Page not found: ${to.fullPath}`,
|
||||||
|
data: {
|
||||||
|
path: to.fullPath
|
||||||
|
}
|
||||||
})))
|
})))
|
||||||
} else if (import.meta.server && to.redirectedFrom && to.fullPath !== initialURL) {
|
} else if (import.meta.server && to.redirectedFrom && to.fullPath !== initialURL) {
|
||||||
await nuxtApp.runWithContext(() => navigateTo(to.fullPath || '/'))
|
await nuxtApp.runWithContext(() => navigateTo(to.fullPath || '/'))
|
||||||
|
@ -15,9 +15,13 @@ export default defineNuxtRouteMiddleware(async (to) => {
|
|||||||
if (import.meta.server) {
|
if (import.meta.server) {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
const error = createError({
|
const error = createError({
|
||||||
statusCode: 404,
|
statusCode: 404,
|
||||||
statusMessage: `Page Not Found: ${to.fullPath}`
|
statusMessage: `Page Not Found: ${to.fullPath}`,
|
||||||
|
data: {
|
||||||
|
path: to.fullPath
|
||||||
|
}
|
||||||
})
|
})
|
||||||
const unsub = router.beforeResolve((final) => {
|
const unsub = router.beforeResolve((final) => {
|
||||||
unsub()
|
unsub()
|
||||||
|
@ -155,6 +155,7 @@ describe('pages', () => {
|
|||||||
|
|
||||||
await page.getByText('should throw a 404 error').click()
|
await page.getByText('should throw a 404 error').click()
|
||||||
expect(await page.getByRole('heading').textContent()).toMatchInlineSnapshot('"Page Not Found: /forbidden"')
|
expect(await page.getByRole('heading').textContent()).toMatchInlineSnapshot('"Page Not Found: /forbidden"')
|
||||||
|
expect(await page.getByTestId('path').textContent()).toMatchInlineSnapshot('" Path: /forbidden"')
|
||||||
|
|
||||||
await gotoPath(page, '/navigate-to-forbidden')
|
await gotoPath(page, '/navigate-to-forbidden')
|
||||||
await page.getByText('should be caught by catchall').click()
|
await page.getByText('should be caught by catchall').click()
|
||||||
|
6
test/fixtures/basic/error.vue
vendored
6
test/fixtures/basic/error.vue
vendored
@ -3,6 +3,12 @@
|
|||||||
<div>
|
<div>
|
||||||
<h1>{{ error?.message }}</h1>
|
<h1>{{ error?.message }}</h1>
|
||||||
This is the error page 😱
|
This is the error page 😱
|
||||||
|
<div
|
||||||
|
v-if="error?.data?.path"
|
||||||
|
data-testid="path"
|
||||||
|
>
|
||||||
|
Path: {{ error.data.path }}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
Loading…
Reference in New Issue
Block a user