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

fix(ssr): handle error during ssr render call #12601

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions packages/server-renderer/__tests__/render.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
defineComponent,
getCurrentInstance,
h,
nextTick,
onErrorCaptured,
onServerPrefetch,
reactive,
Expand Down Expand Up @@ -819,6 +820,9 @@ function testRender(type: string, render: typeof renderToString) {
)
} catch {}
expect(getCurrentInstance()).toBe(prev)
expect(
'[Vue warn]: Unhandled error during execution of render function',
).toHaveBeenWarned()
})

// #7733
Expand Down Expand Up @@ -1188,6 +1192,42 @@ function testRender(type: string, render: typeof renderToString) {
expect((capturedError as unknown as Error).message).toBe('An error')
})

test('async setup throwing error', async () => {
let capturedError: string[] = []

const Child = {
async setup() {
await nextTick()
throw new Error('An error')
return { foo: { bar: 1 } }
},
template: `<span>{{ foo.bar }}</span>`,
}

const app = createApp({
components: { Child },
setup() {
onErrorCaptured(e => {
capturedError.push(e.message)
return false
})
},
template: `<Suspense><Child /></Suspense>`,
})

try {
await render(app)
} catch (e: any) {}
expect(capturedError.length).toBe(2)
expect(capturedError).toStrictEqual([
'An error',
"Cannot read properties of undefined (reading 'bar')",
])
expect(
'[Vue warn]: Property "foo" was accessed during render but is not defined on instance',
).toHaveBeenWarned()
})

test('computed reactivity during SSR with onServerPrefetch', async () => {
const store = {
// initial state could be hydrated
Expand Down
4 changes: 4 additions & 0 deletions packages/server-renderer/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import {
type Component,
type ComponentInternalInstance,
type DirectiveBinding,
ErrorCodes,
Fragment,
type FunctionalComponent,
Static,
Text,
type VNode,
type VNodeArrayChildren,
type VNodeProps,
handleError,
mergeProps,
ssrUtils,
warn,
Expand Down Expand Up @@ -200,6 +202,8 @@ function renderComponentSubTree(
instance.data,
instance.ctx,
)
} catch (err) {
handleError(err, instance, ErrorCodes.RENDER_FUNCTION)
} finally {
setCurrentRenderingInstance(prev)
}
Expand Down
Loading