Skip to content

Commit

Permalink
better testing of error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémi Becheras committed Jan 9, 2017
1 parent d4dc68d commit 4ffb52b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 23 deletions.
19 changes: 18 additions & 1 deletion lib/ConnectSequence.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,38 @@ function run () {
var initialNext = this.next.bind()
var req = this.req
var res = this.res
var nestedCallSequence = midSequence.reduce(middlewareReducer, initialNext)
var nestedCallSequence

// create the call sequence
nestedCallSequence = midSequence.reduce(middlewareReducer, initialNext)
// call it
nestedCallSequence.call()

/**
* Reduce the middleware sequence to a nested middleware handler sequence
* @function
* @param {Function} callSequence intermediate resulting call sequence
* @param {Function} middleware the current middleware
* @returns {Function} the new intermediate resulting call sequence
*/
function middlewareReducer (callSequence, middleware) {
return function nextHandler (err) {
// if the previous middleware passed an error argument
if (err !== undefined) {
if (isErrorHandler(middleware)) {
// call the current middleware if it is an error handler middleware
middleware(err, req, res, callSequence)
} else {
// else skip the current middleware and call the intermediate sequence
callSequence(err)
}
} else {
// if no error argument is passed
if (isErrorHandler(middleware)) {
// skip the current middleware if it is an errorHandler
callSequence()
} else {
// else call it
middleware(req, res, callSequence)
}
}
Expand Down
59 changes: 37 additions & 22 deletions tests/ConnectSequence.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,34 +679,49 @@ describe('ConnectSequence', function () {
expect(runSeq).to.not.throw(Error)
})

it('should skip all middlewares after the middleware throwing and error', function (done) {
next = function next () {
if (!req.isDone) {
req.isDone = true
describe('when a middleware pass an error in its next middleware', function () {
it('should skip all middlewares after the middleware throwing and error', function (done) {
var midBetween = function (req, res, next) {
req.midBetween = true
}
errorHandler = function errorHandler (err, req, res, next) {
if (err) {
// the error is handled ...
}
next()
}
next = function next () {
expect(req.midBetween).to.be.undefined
done()
}
}
errorHandler = function errorHandler (err, req, res, next) {
// the error is handled ...
if (err) {
// ...
seq = new ConnectSequence(req, res, next)
seq.append(midBefore0, nextErr, midBetween, errorHandler)
seq.run()
})
})

describe('when any middleware pass any error in its next middleware', function () {
it('should skip an error handler middleware if no error is passed', function (done) {
errorHandler = function errorHandler (err, req, res, next) {
if (err) {
// the error is handled ...
}
req.errorHandler = true
next()
}
if (!req.isDone) {
req.isDone = true
midAfter0 = function midAfter0 (req, res, next) {
req.midAfter0 = true
next()
}
next = function next () {
expect(req.errorHandler).to.be.undefined
expect(req.midAfter0).to.equal(true)
done()
}
}
midAfter0 = function midAfter0 (req, res, next) {
ensureReqIdsDefined(req)
req.ids.push('midAfter0')
// next()
throw new Error('Forbidden middleware')
}
seq = new ConnectSequence(req, res, next)
seq.append(midBefore0, nextErr, midBefore1, errorHandler, midAfter0)
expect(function () {
seq = new ConnectSequence(req, res, next)
seq.append(midBefore0, errorHandler, midAfter0)
seq.run()
}).to.not.throw(Error)
})
})
})
})
Expand Down

0 comments on commit 4ffb52b

Please sign in to comment.