Skip to content

Commit

Permalink
Fix for #164 – status should not be ignored (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
floatdrop authored and timneutkens committed Mar 14, 2017
1 parent 40304d3 commit eb7e248
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@ function send(res, code, obj = null) {
}

function sendError(req, res, {statusCode, status, message, stack}) {
statusCode = statusCode || status

if (statusCode) {
send(res, statusCode || status, DEV ? stack : message)
send(res, statusCode, DEV ? stack : message)
} else {
send(res, 500, DEV ? stack : 'Internal Server Error')
}
Expand Down
17 changes: 16 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const resumer = require('resumer')
const listen = require('test-listen')
const micro = require('../lib/server')

const {send, json} = micro
const {send, sendError, json} = micro

const getUrl = fn => {
const srv = micro(fn)
Expand Down Expand Up @@ -419,3 +419,18 @@ test('limit included in error', async t => {

await getUrl(fn)
})

test('support for status fallback in errors', async t => {
const fn = (req, res) => {
const err = new Error('Custom')
err.status = 403
sendError(req, res, err)
}

const url = await getUrl(fn)
try {
await request(url)
} catch (err) {
t.deepEqual(err.statusCode, 403)
}
})

0 comments on commit eb7e248

Please sign in to comment.