mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 15:15:19 +00:00
fix(nuxt): allow customising status code in validate
method (#28612)
This commit is contained in:
parent
eed8730688
commit
fb7c2f79f8
@ -12,13 +12,10 @@ export default defineNuxtRouteMiddleware(async (to) => {
|
|||||||
if (result === true) {
|
if (result === true) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (import.meta.server) {
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
const error = createError({
|
const error = createError({
|
||||||
statusCode: 404,
|
statusCode: (result && result.statusCode) || 404,
|
||||||
statusMessage: `Page Not Found: ${to.fullPath}`,
|
statusMessage: (result && result.statusMessage) || `Page Not Found: ${to.fullPath}`,
|
||||||
data: {
|
data: {
|
||||||
path: to.fullPath,
|
path: to.fullPath,
|
||||||
},
|
},
|
||||||
@ -32,7 +29,7 @@ export default defineNuxtRouteMiddleware(async (to) => {
|
|||||||
// We pretend to have navigated to the invalid route so
|
// We pretend to have navigated to the invalid route so
|
||||||
// that the user can return to the previous page with
|
// that the user can return to the previous page with
|
||||||
// the back button.
|
// the back button.
|
||||||
window.history.pushState({}, '', to.fullPath)
|
window?.history.pushState({}, '', to.fullPath)
|
||||||
})
|
})
|
||||||
// We stop the navigation immediately before it resolves
|
// We stop the navigation immediately before it resolves
|
||||||
// if there is no other route matching it.
|
// if there is no other route matching it.
|
||||||
|
@ -207,6 +207,45 @@ describe('pages', () => {
|
|||||||
await page.close()
|
await page.close()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('validates routes with custom statusCode and statusMessage', async () => {
|
||||||
|
const CUSTOM_ERROR_CODE = 401
|
||||||
|
const CUSTOM_ERROR_MESSAGE = 'Custom error message'
|
||||||
|
const ERROR_PAGE_TEXT = 'This is the error page'
|
||||||
|
const PAGE_TEXT = 'You should not see this'
|
||||||
|
|
||||||
|
// Check status code and message on fetch
|
||||||
|
const res = await fetch('/validate-custom-error')
|
||||||
|
const serverText = await res.text()
|
||||||
|
|
||||||
|
expect(res.status).toEqual(CUSTOM_ERROR_CODE)
|
||||||
|
expect(serverText).toContain(CUSTOM_ERROR_MESSAGE)
|
||||||
|
expect(serverText).not.toContain(PAGE_TEXT)
|
||||||
|
|
||||||
|
// Client-side navigation
|
||||||
|
const { page } = await renderPage('/navigate-to-validate-custom-error')
|
||||||
|
await page.getByText('should throw a 401 error with custom message').click()
|
||||||
|
// error.vue has an h1 tag
|
||||||
|
await page.waitForSelector('h1')
|
||||||
|
|
||||||
|
const clientText = await page.innerText('body')
|
||||||
|
|
||||||
|
expect(clientText).toContain(CUSTOM_ERROR_MESSAGE)
|
||||||
|
expect(clientText).toContain(ERROR_PAGE_TEXT)
|
||||||
|
expect(clientText).not.toContain(PAGE_TEXT)
|
||||||
|
|
||||||
|
await page.close()
|
||||||
|
|
||||||
|
// Server-side navigation
|
||||||
|
const { page: serverPage } = await renderPage('/validate-custom-error')
|
||||||
|
const serverPageText = await serverPage.innerText('body')
|
||||||
|
|
||||||
|
expect(serverPageText).toContain(CUSTOM_ERROR_MESSAGE)
|
||||||
|
expect(serverPageText).toContain(ERROR_PAGE_TEXT)
|
||||||
|
expect(serverPageText).not.toContain(PAGE_TEXT)
|
||||||
|
|
||||||
|
await serverPage.close()
|
||||||
|
})
|
||||||
|
|
||||||
it('returns 500 when there is an infinite redirect', async () => {
|
it('returns 500 when there is an infinite redirect', async () => {
|
||||||
const { status } = await fetch('/redirect-infinite', { redirect: 'manual' })
|
const { status } = await fetch('/redirect-infinite', { redirect: 'manual' })
|
||||||
expect(status).toEqual(500)
|
expect(status).toEqual(500)
|
||||||
|
14
test/fixtures/basic/pages/navigate-to-validate-custom-error.vue
vendored
Normal file
14
test/fixtures/basic/pages/navigate-to-validate-custom-error.vue
vendored
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
<div>navigate-to-validate-custom-error.vue</div>
|
||||||
|
<NuxtLink to="/validate-custom-error">
|
||||||
|
should throw a 401 error with custom message
|
||||||
|
</NuxtLink>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
definePageMeta({
|
||||||
|
middleware: ['override'],
|
||||||
|
})
|
||||||
|
</script>
|
12
test/fixtures/basic/pages/validate-custom-error.vue
vendored
Normal file
12
test/fixtures/basic/pages/validate-custom-error.vue
vendored
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<template>
|
||||||
|
<div>You should not see this</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
definePageMeta({
|
||||||
|
validate: to => ({
|
||||||
|
statusCode: 401,
|
||||||
|
statusMessage: 'Custom error message',
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
</script>
|
Loading…
Reference in New Issue
Block a user