Skip to content

Commit

Permalink
fix(runtime-core): handle error in async setup (#2881)
Browse files Browse the repository at this point in the history
  • Loading branch information
edison1105 authored Mar 26, 2021
1 parent abd129d commit d668d48
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from './componentProps'
import { Slots, initSlots, InternalSlots } from './componentSlots'
import { warn } from './warning'
import { ErrorCodes, callWithErrorHandling } from './errorHandling'
import { ErrorCodes, callWithAsyncErrorHandling } from './errorHandling'
import { AppContext, createAppContext, AppConfig } from './apiCreateApp'
import { Directive, validateDirectiveName } from './directives'
import {
Expand Down Expand Up @@ -579,7 +579,7 @@ function setupStatefulComponent(

currentInstance = instance
pauseTracking()
const setupResult = callWithErrorHandling(
const setupResult = callWithAsyncErrorHandling(
setup,
instance,
ErrorCodes.SETUP_FUNCTION,
Expand Down
38 changes: 38 additions & 0 deletions packages/server-renderer/__tests__/render.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,44 @@ function testRender(type: string, render: typeof renderToString) {
expect(html).toBe(`<div>hello</div>`)
})

// #2763
test('error handling w/ async setup', async () => {
const fn = jest.fn()
const fn2 = jest.fn()

const asyncChildren = defineComponent({
async setup() {
return Promise.reject('async child error')
},
template: `<div>asyncChildren</div>`
})
const app = createApp({
name: 'App',
components: {
asyncChildren
},
template: `<div class="app"><async-children /></div>`,
errorCaptured(error) {
fn(error)
}
})

app.config.errorHandler = error => {
fn2(error)
}

const html = await renderToString(app)
expect(html).toBe(`<div class="app"><div>asyncChildren</div></div>`)

expect(fn).toHaveBeenCalledTimes(1)
expect(fn).toBeCalledWith('async child error')

expect(fn2).toHaveBeenCalledTimes(1)
expect(fn2).toBeCalledWith('async child error')

expect('Uncaught error in async setup').toHaveBeenWarned()
})

// https://github.com/vuejs/vue-next/issues/3322
test('effect onInvalidate does not error', async () => {
const noop = () => {}
Expand Down
12 changes: 12 additions & 0 deletions packages/server-renderer/__tests__/ssrSuspense.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ describe('SSR Suspense', () => {

expect(await renderToString(createApp(Comp))).toBe(`<!---->`)
expect('Uncaught error in async setup').toHaveBeenWarned()
expect(
'Unhandled error during execution of setup function'
).toHaveBeenWarned()
expect('missing template').toHaveBeenWarned()
})

Expand Down Expand Up @@ -71,6 +74,9 @@ describe('SSR Suspense', () => {
`<div><div>async</div><!----></div>`
)
expect('Uncaught error in async setup').toHaveBeenWarned()
expect(
'Unhandled error during execution of setup function'
).toHaveBeenWarned()
expect('missing template or render function').toHaveBeenWarned()
})

Expand All @@ -94,6 +100,9 @@ describe('SSR Suspense', () => {
`<div><div>async</div><div><!----></div></div>`
)
expect('Uncaught error in async setup').toHaveBeenWarned()
expect(
'Unhandled error during execution of setup function'
).toHaveBeenWarned()
expect('missing template').toHaveBeenWarned()
})

Expand All @@ -117,6 +126,9 @@ describe('SSR Suspense', () => {
`<div><!----><div><div>async</div></div></div>`
)
expect('Uncaught error in async setup').toHaveBeenWarned()
expect(
'Unhandled error during execution of setup function'
).toHaveBeenWarned()
expect('missing template').toHaveBeenWarned()
})
})

0 comments on commit d668d48

Please sign in to comment.