Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for throwing in async handlers. #69

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ Layer.prototype.handle_error = function handle_error(error, req, res, next) {
return next(error)
}

if (fn.constructor.name === 'AsyncFunction') {
return fn(error, req, res, next).catch(next)
}

try {
fn(error, req, res, next)
} catch (err) {
Expand All @@ -89,6 +93,10 @@ Layer.prototype.handle_request = function handle(req, res, next) {
return next()
}

if (fn.constructor.name === 'AsyncFunction') {
return fn(req, res, next).catch(next)
}

try {
fn(req, res, next)
} catch (err) {
Expand Down
54 changes: 54 additions & 0 deletions test/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var createServer = utils.createServer
var request = utils.request
var shouldHitHandle = utils.shouldHitHandle
var shouldNotHitHandle = utils.shouldNotHitHandle
var asyncFunctionsSupported = utils.asyncFunctionsSupported

describe('Router', function () {
describe('.route(path)', function () {
Expand Down Expand Up @@ -335,6 +336,59 @@ describe('Router', function () {
.get('/foo')
.expect(500, 'caught: oh, no!', done)
})

if (asyncFunctionsSupported()) {

it('should handle errors thrown', function (done) {
var router = new Router()
var route = router.route('/foo')
var server = createServer(router)

route.all(new Function([],
"return async function createError(req, res, next) { throw new Error('boom!') }"
)())

route.all(helloWorld)

route.all(function handleError(err, req, res, next) {
res.statusCode = 500
res.end('caught: ' + err.message)
})

request(server)
.get('/foo')
.expect(500, 'caught: boom!', done)
})

it('should handle errors thrown in error handlers', function (done) {
var router = new Router()
var route = router.route('/foo')
var server = createServer(router)

route.all(function createError(req, res, next) {
throw new Error('boom!')
})

route.all(new Function([],
"return async function handleError(err, req, res, next) { throw new Error('oh, no!') }"
)())

route.all(function handleError(err, req, res, next) {
res.statusCode = 500
res.end('caught: ' + err.message)
})

request(server)
.get('/foo')
.expect(500, 'caught: oh, no!', done)
})



}



})

describe('next("route")', function () {
Expand Down
10 changes: 10 additions & 0 deletions test/support/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ exports.rawrequest = rawrequest
exports.request = request
exports.shouldHitHandle = shouldHitHandle
exports.shouldNotHitHandle = shouldNotHitHandle
exports.asyncFunctionsSupported = asyncFunctionsSupported

function createHitHandle(num) {
var name = 'x-fn-' + String(num)
Expand Down Expand Up @@ -120,3 +121,12 @@ function shouldNotHaveHeader(header) {
assert.ok(!(header.toLowerCase() in res.headers), 'should not have header ' + header)
}
}

function asyncFunctionsSupported() {
try {
new Function([],"return async function(){}")
return true
} catch(e) {
return false
}
}