feat(server): add custom HTTP headers into error response (#7214)

This commit is contained in:
Nicolas PENNEC 2020-04-13 20:58:53 +02:00 committed by GitHub
parent 73541a576d
commit 1002d3f456
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 2 deletions

View File

@ -10,7 +10,8 @@ export default ({ resources, options }) => async function errorMiddleware (err,
const error = { const error = {
statusCode: err.statusCode || 500, statusCode: err.statusCode || 500,
message: err.message || 'Nuxt Server Error', message: err.message || 'Nuxt Server Error',
name: !err.name || err.name === 'Error' ? 'NuxtServerError' : err.name name: !err.name || err.name === 'Error' ? 'NuxtServerError' : err.name,
headers: err.headers
} }
const sendResponse = (content, type = 'text/html') => { const sendResponse = (content, type = 'text/html') => {
@ -21,6 +22,13 @@ export default ({ resources, options }) => async function errorMiddleware (err,
res.setHeader('Content-Length', Buffer.byteLength(content)) res.setHeader('Content-Length', Buffer.byteLength(content))
res.setHeader('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate') res.setHeader('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate')
// Error headers
if (error.headers) {
for (const name in error.headers) {
res.setHeader(name, error.headers[name])
}
}
// Send Response // Send Response
res.end(content, 'utf-8') res.end(content, 'utf-8')
} }

View File

@ -49,6 +49,7 @@ describe('server: errorMiddleware', () => {
const params = createParams() const params = createParams()
const errorMiddleware = createErrorMiddleware(params) const errorMiddleware = createErrorMiddleware(params)
const error = new Error() const error = new Error()
error.headers = { 'Custom-Header': 'test' }
const ctx = createServerContext() const ctx = createServerContext()
await errorMiddleware(error, ctx.req, ctx.res, ctx.next) await errorMiddleware(error, ctx.req, ctx.res, ctx.next)
@ -56,10 +57,11 @@ describe('server: errorMiddleware', () => {
expect(consola.error).toBeCalledWith(error) expect(consola.error).toBeCalledWith(error)
expect(ctx.res.statusCode).toEqual(500) expect(ctx.res.statusCode).toEqual(500)
expect(ctx.res.statusMessage).toEqual('NuxtServerError') expect(ctx.res.statusMessage).toEqual('NuxtServerError')
expect(ctx.res.setHeader).toBeCalledTimes(3) expect(ctx.res.setHeader).toBeCalledTimes(4)
expect(ctx.res.setHeader).nthCalledWith(1, 'Content-Type', 'text/html; charset=utf-8') expect(ctx.res.setHeader).nthCalledWith(1, 'Content-Type', 'text/html; charset=utf-8')
expect(ctx.res.setHeader).nthCalledWith(2, 'Content-Length', Buffer.byteLength('error template')) expect(ctx.res.setHeader).nthCalledWith(2, 'Content-Length', Buffer.byteLength('error template'))
expect(ctx.res.setHeader).nthCalledWith(3, 'Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate') expect(ctx.res.setHeader).nthCalledWith(3, 'Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate')
expect(ctx.res.setHeader).nthCalledWith(4, 'Custom-Header', 'test')
expect(params.resources.errorTemplate).toBeCalledTimes(1) expect(params.resources.errorTemplate).toBeCalledTimes(1)
expect(params.resources.errorTemplate).toBeCalledWith({ expect(params.resources.errorTemplate).toBeCalledWith({
status: 500, status: 500,