Improve error handling on SSR

This commit is contained in:
Sébastien Chopin 2017-03-24 01:28:19 +01:00
parent d4db5792a4
commit cfd6b197c4

View File

@ -8,7 +8,7 @@ import { stringify } from 'querystring'
import { omit } from 'lodash'
import middleware from './middleware'
import { app, router<%= (store ? ', store' : '') %>, NuxtError } from './index'
import { getContext, promiseSeries, promisify, urlJoin } from './utils'
import { getMatchedComponents, getContext, promiseSeries, promisify, urlJoin } from './utils'
const isDev = <%= isDev %>
const _app = new Vue(app)
@ -40,8 +40,6 @@ export default context => {
})
context.res.end()
}
// set router's location
router.push(context.url)
// Add route to the context
context.route = router.currentRoute
// Add meta infos
@ -52,12 +50,26 @@ export default context => {
<%= (isDev ? 'const s = isDev && Date.now()' : '') %>
let ctx = getContext(context)
let Components = []
return new Promise((resolve) => {
// Wait for the lazy-routes are loaded
router.onReady(resolve)
let promises = getMatchedComponents(router.match(context.url)).map((Component) => {
return new Promise((resolve, reject) => {
const _resolve = (Component) => {
if (!Component.options) {
Component = Vue.extend(Component) // fix issue #6
Component._Ctor = Component
} else {
Component._Ctor = Component
Component.extendOptions = Component.options
}
resolve(Component)
}
Component().then(_resolve).catch(reject)
})
})
.then(() => {
Components = router.getMatchedComponents()
return Promise.all(promises)
.then((_Components) => {
Components = _Components
// set router's location
router.push(context.url)
// nuxtServerInit
<% if (store) { %>
let promise = (store._actions && store._actions.nuxtServerInit ? store.dispatch('nuxtServerInit', omit(getContext(context), 'redirect', 'error')) : null)
@ -186,8 +198,7 @@ export default context => {
})
.catch(function (error) {
if (error && (error instanceof Error || error.constructor.toString().indexOf('Error()') !== -1)) {
let statusCode = error.statusCode || error.status || (error.response && error.response.status) || 500
error = { statusCode, message: error.message }
return Promise.reject(error)
}
else if (typeof error === 'string') {
error = { statusCode: 500, message: error }