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

Catches errors from Promises / Async functions #47

Closed
wants to merge 1 commit 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
28 changes: 26 additions & 2 deletions lib/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ Layer.prototype.handle_error = function handle_error(error, req, res, next) {
}

try {
fn(error, req, res, next)
var result = fn(error, req, res, next)
if (isPromise(result)) {
result.then(null, function (error) {
next(error || new Error('Promise rejected with: ' + error))
})
}
} catch (err) {
next(err)
}
Expand All @@ -90,7 +95,12 @@ Layer.prototype.handle_request = function handle(req, res, next) {
}

try {
fn(req, res, next)
var result = fn(req, res, next)
if (isPromise(result)) {
result.then(null, function (error) {
next(error || new Error('Promise rejected with: ' + error))
})
}
} catch (err) {
next(err)
}
Expand Down Expand Up @@ -173,3 +183,17 @@ function decode_param(val){
throw err
}
}

/**
* Returns true if the val is a Promise.
*
* @param {any} val
* @return {boolean}
* @private
*/

function isPromise(val) {
return val !== null &&
typeof val === 'object' &&
typeof val.then === 'function'
}
68 changes: 68 additions & 0 deletions test/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,74 @@ describe('Router', function () {
.get('/foo')
.expect(500, 'caught: oh, no!', done)
})

it('should handle a returned rejected promise', function(done) {
// Only run this test in environments where Promise is defined.
if (!global.Promise) {
return done();
}

var router = new Router()
var route = router.route('/foo')
var server = createServer(router)

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

route.all(function handleError(err, req, res, next) {
return new Promise(function () {
throw new Error('oh, no!')
})
})

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

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

it('should handle a returned rejected promise with no value', function(done) {
// Only run this test in environments where Promise is defined.
if (!global.Promise) {
return done();
}

var router = new Router()
var route = router.route('/foo')
var server = createServer(router)

route.all(function createError(req, res, next) {
return new Promise(function (_, reject) {
reject()
})
})

route.all(function handleError(err, req, res, next) {
return new Promise(function (_, reject) {
reject()
})
})

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

request(server)
.get('/foo')
.expect(500, 'caught: Promise rejected with: undefined', done)
})
})

describe('path', function () {
Expand Down