diff --git a/middleware/handle-errors.js b/middleware/handle-errors.js index bc2ef80260a0..0170a59ca303 100644 --- a/middleware/handle-errors.js +++ b/middleware/handle-errors.js @@ -37,6 +37,12 @@ module.exports = async function handleError (error, req, res, next) { .send(await liquid.parseAndRender(layouts['error-404'], req.context)) } + // If the error contains a status code, just send that back. This is usually + // from a middleware like `express.json()` or `csrf`. + if (error.statusCode || error.status) { + return res.sendStatus(error.statusCode || error.status) + } + if (process.env.NODE_ENV !== 'test') { console.error('500 error!', req.path) console.error(error) diff --git a/tests/rendering/server.js b/tests/rendering/server.js index 9910e12c3213..db4aa54d5dac 100644 --- a/tests/rendering/server.js +++ b/tests/rendering/server.js @@ -1,6 +1,6 @@ const lodash = require('lodash') const enterpriseServerReleases = require('../../lib/enterprise-server-releases') -const { get, getDOM, head } = require('../helpers/supertest') +const { get, getDOM, head, post } = require('../helpers/supertest') const { describeViaActionsOnly } = require('../helpers/conditional-runs') const path = require('path') const { loadPages } = require('../../lib/pages') @@ -114,6 +114,13 @@ describe('server', () => { expect($.res.statusCode).toBe(500) }) + test('returns a 400 when POST-ed invalid JSON', async () => { + const res = await post('/') + .send('not real JSON') + .set('Content-Type', 'application/json') + expect(res.statusCode).toBe(400) + }) + test('converts Markdown in intros', async () => { // example from markdown source in intro: // The `git rebase` command