fix: async fetch and asyncData not trigger ErrorHandler (#3781)

This commit is contained in:
Clark Du 2018-08-22 14:10:43 +01:00 committed by GitHub
parent b35126d68c
commit 3f7c5f64ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 12 deletions

View File

@ -14,8 +14,7 @@ import {
getLocation, getLocation,
compile, compile,
getQueryDiff, getQueryDiff,
globalHandleError, globalHandleError
empty
} from './utils' } from './utils'
const noopData = () => { return {} } const noopData = () => { return {} }
@ -35,7 +34,7 @@ Object.assign(Vue.config, <%= serialize(vue.config) %>)
<% if (debug || mode === 'spa') { %> <% if (debug || mode === 'spa') { %>
// Setup global Vue error handler // Setup global Vue error handler
const defaultErrorHandler = Vue.config.errorHandler const defaultErrorHandler = Vue.config.errorHandler
Vue.config.errorHandler = (err, vm, info) => { Vue.config.errorHandler = (err, vm, info, ...rest) => {
const nuxtError = { const nuxtError = {
statusCode: err.statusCode || err.name || 'Whoops!', statusCode: err.statusCode || err.name || 'Whoops!',
message: err.message || err.toString() message: err.message || err.toString()
@ -44,7 +43,7 @@ Vue.config.errorHandler = (err, vm, info) => {
// Call other handler if exist // Call other handler if exist
let handled = null let handled = null
if (typeof defaultErrorHandler === 'function') { if (typeof defaultErrorHandler === 'function') {
handled = defaultErrorHandler(...arguments) handled = defaultErrorHandler(err, vm, info, ...rest)
} }
if (handled === true){ if (handled === true){
return handled return handled
@ -347,8 +346,6 @@ async function render (to, from, next) {
} }
<% } %> <% } %>
}) })
// error will be handled in try catch
.catch(empty)
promises.push(promise) promises.push(promise)
} }
@ -368,8 +365,6 @@ async function render (to, from, next) {
} }
<% } %> <% } %>
}) })
// error will be handled in try catch
.catch(empty)
promises.push(p) promises.push(p)
} }

View File

@ -0,0 +1,9 @@
<script>
export default {
async asyncData() {
await Promise.resolve()
throw Error('asyncData error!')
}
}
</script>

View File

@ -2,7 +2,7 @@
export default { export default {
fetch() { fetch() {
throw Error('spa test error!') throw Error('fetch error!')
} }
} }
</script> </script>

View File

@ -1,5 +1,5 @@
import Vue from 'vue' import Vue from 'vue'
Vue.config.errorHandler = function () { Vue.config.errorHandler = function (err) {
document.body.appendChild(document.createTextNode('error handler triggered')) document.body.appendChild(document.createTextNode(`error handler triggered: ${err.message}`))
} }

View File

@ -43,7 +43,13 @@ describe('spa', () => {
test('/error-handler', async () => { test('/error-handler', async () => {
await renderRoute('/error-handler') await renderRoute('/error-handler')
const { html } = await renderRoute('/error-handler') const { html } = await renderRoute('/error-handler')
expect(html).toMatch('error handler triggered') expect(html).toMatch('error handler triggered: fetch error!')
})
test('/error-handler-async', async () => {
await renderRoute('/error-handler-async')
const { html } = await renderRoute('/error-handler-async')
expect(html).toMatch('error handler triggered: asyncData error!')
}) })
test('/_nuxt/ (access publicPath in spa mode)', async () => { test('/_nuxt/ (access publicPath in spa mode)', async () => {