mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
fix: async fetch and asyncData not trigger ErrorHandler (#3781)
This commit is contained in:
parent
b35126d68c
commit
3f7c5f64ed
@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
9
test/fixtures/spa/pages/error-handler-async.vue
vendored
Normal file
9
test/fixtures/spa/pages/error-handler-async.vue
vendored
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<script>
|
||||||
|
|
||||||
|
export default {
|
||||||
|
async asyncData() {
|
||||||
|
await Promise.resolve()
|
||||||
|
throw Error('asyncData error!')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
2
test/fixtures/spa/pages/error-handler.vue
vendored
2
test/fixtures/spa/pages/error-handler.vue
vendored
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
fetch() {
|
fetch() {
|
||||||
throw Error('spa test error!')
|
throw Error('fetch error!')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
4
test/fixtures/spa/plugins/error.js
vendored
4
test/fixtures/spa/plugins/error.js
vendored
@ -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}`))
|
||||||
}
|
}
|
||||||
|
@ -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 () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user