mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 16:12:12 +00:00
renderer: errorMiddleware
Catch all middleware errors
This commit is contained in:
parent
daa50e4efb
commit
b6544637e0
@ -22,7 +22,7 @@ export default class Nuxt extends Tapable {
|
||||
this.renderer = new Renderer(this)
|
||||
|
||||
// Backward compatibility
|
||||
this.render = (...args) => this.renderer.app(...args)
|
||||
this.render = this.renderer.app
|
||||
this.renderRoute = this.renderer.renderRoute.bind(this.renderer)
|
||||
this.renderAndGetWindow = this.renderer.renderAndGetWindow.bind(this.renderer)
|
||||
|
||||
|
@ -45,6 +45,10 @@ export default class Renderer extends Tapable {
|
||||
errorTemplate: parseTemplate('<pre>{{ stack }}</pre>') // Will be loaded on ready
|
||||
}
|
||||
|
||||
// Bind middleware to this context
|
||||
this.nuxtMiddleware = this.nuxtMiddleware.bind(this)
|
||||
this.errorMiddleware = this.errorMiddleware.bind(this)
|
||||
|
||||
// Initialize
|
||||
/* istanbul ignore if */
|
||||
if (nuxt.initialized) {
|
||||
@ -220,10 +224,13 @@ export default class Renderer extends Tapable {
|
||||
}
|
||||
|
||||
// Finally use nuxtMiddleware
|
||||
this.useMiddleware(this.nuxtMiddleware.bind(this))
|
||||
this.useMiddleware(this.nuxtMiddleware)
|
||||
|
||||
// Error middleware for errors that occurred in middleware that declared above
|
||||
this.useMiddleware(this.errorMiddleware)
|
||||
}
|
||||
|
||||
async nuxtMiddleware (req, res) {
|
||||
async nuxtMiddleware (req, res, next) {
|
||||
// Get context
|
||||
const context = getContext(req, res)
|
||||
res.statusCode = 200
|
||||
@ -271,23 +278,28 @@ export default class Renderer extends Tapable {
|
||||
res.end(html, 'utf8')
|
||||
return html
|
||||
} catch (err) {
|
||||
/* istanbul ignore if */
|
||||
if (context.redirected) {
|
||||
console.error(err) // eslint-disable-line no-console
|
||||
return err
|
||||
}
|
||||
// Render error template
|
||||
const html = this.resources.errorTemplate({
|
||||
error: err,
|
||||
stack: ansiHTML(encodeHtml(err.stack))
|
||||
})
|
||||
// Send response
|
||||
res.statusCode = 500
|
||||
res.setHeader('Content-Type', 'text/html; charset=utf-8')
|
||||
res.setHeader('Content-Length', Buffer.byteLength(html))
|
||||
res.end(html, 'utf8')
|
||||
next(this.errorMiddleware(err, req, res, next, context))
|
||||
}
|
||||
}
|
||||
|
||||
async errorMiddleware (err, req, res, next, context) {
|
||||
/* istanbul ignore if */
|
||||
if (context && context.redirected) {
|
||||
console.error(err) // eslint-disable-line no-console
|
||||
return err
|
||||
}
|
||||
|
||||
// Render error template
|
||||
const html = this.resources.errorTemplate({
|
||||
error: err,
|
||||
stack: ansiHTML(encodeHtml(err.stack))
|
||||
})
|
||||
// Send response
|
||||
res.statusCode = 500
|
||||
res.setHeader('Content-Type', 'text/html; charset=utf-8')
|
||||
res.setHeader('Content-Length', Buffer.byteLength(html))
|
||||
res.end(html, 'utf8')
|
||||
return err
|
||||
}
|
||||
|
||||
async renderRoute (url, context = {}) {
|
||||
|
Loading…
Reference in New Issue
Block a user