Skip to content

Commit

Permalink
Honor Error.status(Code) in handle-errors middleware (github#17768)
Browse files Browse the repository at this point in the history
* Honor error status codes in handle-errors

* Add a test
  • Loading branch information
JasonEtco authored Feb 9, 2021
1 parent fa6c0f3 commit d98bb56
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 6 additions & 0 deletions middleware/handle-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion tests/rendering/server.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d98bb56

Please sign in to comment.