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

Treat returned promise like calling next #75

Open
wants to merge 1 commit into
base: 2.0
Choose a base branch
from
Open
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: 6 additions & 2 deletions lib/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ Layer.prototype.handle_error = function handle_error(error, req, res, next) {

// wait for returned promise
if (isPromise(ret)) {
ret.then(null, function (error) {
ret.then(function () {
next()
}, function (error) {
next(error || new Error('Rejected promise'))
})
}
Expand Down Expand Up @@ -103,7 +105,9 @@ Layer.prototype.handle_request = function handle(req, res, next) {

// wait for returned promise
if (isPromise(ret)) {
ret.then(null, function (error) {
ret.then(function () {
next()
}, function (error) {
next(error || new Error('Rejected promise'))
})
}
Expand Down
18 changes: 9 additions & 9 deletions test/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,18 +512,17 @@ describe('Router', function () {
.expect(500, 'caught: Rejected promise', done)
})

it('should ignore resolved promise', function (done) {
it('should treat a resolved promise like calling next', function (done) {
var router = new Router()
var route = router.route('/foo')
var server = createServer(router)

route.all(function createError (req, res, next) {
saw(req, res)
return Promise.resolve('foo')
})

route.all(function () {
done(new Error('Unexpected route invoke'))
route.all(function (req, res) {
saw(req, res)
})

request(server)
Expand Down Expand Up @@ -578,7 +577,7 @@ describe('Router', function () {
.expect(500, 'caught again: Rejected promise', done)
})

it('should ignore resolved promise', function (done) {
it('should treat a resolved promise like calling next', function (done) {
var router = new Router()
var route = router.route('/foo')
var server = createServer(router)
Expand All @@ -587,14 +586,15 @@ describe('Router', function () {
return Promise.reject(new Error('boom!'))
})

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

route.all(function () {
done(new Error('Unexpected route invoke'))
route.all(function (req, res) {
res.statusCode = 500
res.end('caught: ' + seen.message)
})

request(server)
Expand Down
20 changes: 10 additions & 10 deletions test/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -723,17 +723,16 @@ describe('Router', function () {
.expect(200, 'saw Error: Rejected promise', done)
})

it('should ignore resolved promise', function (done) {
it('should treat a resolved promise like calling next', function (done) {
var router = new Router()
var server = createServer(router)

router.use(function createError (req, res, next) {
saw(req, res)
router.use(function (req, res, next) {
return Promise.resolve('foo')
})

router.use(function () {
done(new Error('Unexpected middleware invoke'))
router.use(function (req, res) {
saw(req, res)
})

request(server)
Expand Down Expand Up @@ -780,21 +779,22 @@ describe('Router', function () {
.expect(200, 'saw Error: caught: Rejected promise', done)
})

it('should ignore resolved promise', function (done) {
it('should treat a resolved promise like calling next', function (done) {
var router = new Router()
var server = createServer(router)

router.use(function createError (req, res, next) {
return Promise.reject(new Error('boom!'))
})

var seen = false
router.use(function handleError (err, req, res, next) {
sawError(err, req, res, next)
return Promise.resolve('foo')
seen = err
return Promise.resolve()
})

router.use(function () {
done(new Error('Unexpected middleware invoke'))
router.use(function (req, res, next) {
sawError(seen, req, res, next)
})

request(server)
Expand Down