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

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 {
fetch() {
throw Error('spa test error!')
throw Error('fetch error!')
}
}
</script>

View File

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

View File

@ -43,7 +43,13 @@ describe('spa', () => {
test('/error-handler', async () => {
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 () => {