Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(nuxt): replace Nuxt payload's error type with NuxtError #25398

Merged
merged 10 commits into from
Jan 28, 2024
4 changes: 3 additions & 1 deletion docs/1.getting-started/8.error-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ Customize the default error page by adding `~/error.vue` in the source directory

```vue [error.vue]
<script setup lang="ts">
import type { NuxtError } from '#app'

const props = defineProps({
error: Object
error: NuxtError
DamianGlowala marked this conversation as resolved.
Show resolved Hide resolved
})

const handleError = () => clearError({ redirect: '/' })
Expand Down
14 changes: 8 additions & 6 deletions docs/2.guide/2.directory-structure/3.error.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ During the lifespan of your application, some errors may appear unexpectedly at

```vue [error.vue]
<script setup lang="ts">
import type { NuxtError } from '#app'

const props = defineProps({
error: Object
error: NuxtError
})
</script>

Expand All @@ -31,12 +33,12 @@ The error page has a single prop - `error` which contains an error for you to ha
The `error` object provides the following fields:
```ts
{
url: string
statusCode: number
statusMessage: string
message: string
description: string
data: any
fatal: boolean
unhandled: boolean
statusMessage?: string
data?: unknown
cause?: unknown
}
```

Expand Down
14 changes: 8 additions & 6 deletions packages/nuxt/src/app/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import '#build/fetch.mjs'
import { applyPlugins, createNuxtApp } from './nuxt'
import type { CreateOptions } from './nuxt'

import { createError } from './composables/error'

import '#build/css'
// @ts-expect-error virtual file
import plugins from '#build/plugins'
Expand All @@ -29,9 +31,9 @@ if (import.meta.server) {
try {
await applyPlugins(nuxt, plugins)
await nuxt.hooks.callHook('app:created', vueApp)
} catch (err) {
await nuxt.hooks.callHook('app:error', err)
nuxt.payload.error = (nuxt.payload.error || err) as any
} catch (error) {
await nuxt.hooks.callHook('app:error', error)
nuxt.payload.error = nuxt.payload.error || createError(error as any)
}
if (ssrContext?._renderResponse) { throw new Error('skipping render') }

Expand Down Expand Up @@ -59,9 +61,9 @@ if (import.meta.client) {

const nuxt = createNuxtApp({ vueApp })

async function handleVueError(err: any) {
await nuxt.callHook('app:error', err)
nuxt.payload.error = (nuxt.payload.error || err) as any
async function handleVueError(error: any) {
await nuxt.callHook('app:error', error)
nuxt.payload.error = nuxt.payload.error || createError(error as any)
}

vueApp.config.errorHandler = handleVueError
Expand Down
9 changes: 1 addition & 8 deletions packages/nuxt/src/app/nuxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,7 @@ export interface NuxtPayload {
state: Record<string, any>
once: Set<string>
config?: Pick<RuntimeConfig, 'public' | 'app'>
error?: Error | {
url: string
statusCode: number
statusMessage: string
message: string
description: string
data?: any
} | null
error?: NuxtError | null
_errors: Record<string, NuxtError | null>
[key: string]: unknown
}
Expand Down
3 changes: 2 additions & 1 deletion packages/nuxt/src/core/runtime/nitro/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getRequestHeaders, send, setResponseHeader, setResponseStatus } from 'h
import { useRuntimeConfig } from '#internal/nitro'
import { useNitroApp } from '#internal/nitro/app'
import { isJsonRequest, normalizeError } from '#internal/nitro/utils'
import type { NuxtPayload } from '#app'

export default <NitroErrorHandler> async function errorhandler (error: H3Error, event) {
// Parse and normalize error
Expand All @@ -21,7 +22,7 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
: '',
// TODO: check and validate error.data for serialisation into query
data: error.data as any
}
} satisfies Partial<NuxtPayload['error']> & { url: string }

// Console output
if (error.unhandled || error.fatal) {
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/src/core/runtime/nitro/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export default defineRenderHandler(async (event): Promise<Partial<RenderResponse

// Whether we're rendering an error page
const ssrError = event.path.startsWith('/__nuxt_error')
? getQuery(event) as unknown as Exclude<NuxtPayload['error'], Error>
? getQuery(event) as unknown as NuxtPayload['error'] & { url: string }
: null

if (ssrError && ssrError.statusCode) {
Expand Down