First off, excellent module! This thing is well built and extremely useful. Thank you for making it more enjoyable to build http servers in node.
I have a small nit that I think may be worth pointing out in the README to possibly save future devs some pain, and that's this:
99% of the time, devs can assume that this module will create a single piece of middleware that behaves equivalent to all composed middleware.
There is at least one case though where this is not true:
// without-compose.js
app.use(
(req, res, next) => { res.write('a'); next() }
)
app.use(
(req, res, next) => { next(new Error('bummer!')) }
)
// here's our chain of error-handling middleware
app.use([
(err, req, res, next) => { res.write('b'); next(err) },
(err, req, res, next) => { res.write('c'); next(err) },
])
app.use(
(err, req, res, next) => { res.write('d'); next(err) }
)
In this example, the caller will receive a response of abcd
.
If however, we use compose
in the above example to compose the array of error handling middleware, the result is different:
// with-compose.js
app.use(
(req, res, next) => { res.write('a'); next() }
)
app.use(
(req, res, next) => { next(new Error('bummer!')) }
)
// this chain acts differently when used with compose
app.use(compose([
(err, req, res, next) => { res.write('b'); next(err) },
(err, req, res, next) => { res.write('c'); next(err) },
]))
app.use(
(err, req, res, next) => { res.write('d'); next(err) }
)
This will result in a response of ad
.
As far as I'm aware, there's nothing we can really do about this. The reason for this behavior is probably very obvious to anyone who has seen the source code: compose
always returns a function with a signature of (req, res, next)
, which, requiring 3 arguments and not 4, will result in express only registering the composed function for success handlers and not error handlers.
The TL;DR that I think warrants noting in the README is this: if devs wish to produce a composed function specifically for handling errors, they will have to do it some other way as the signature of the composed function that they end up registering will need to require 4 arguments and not 3.
Thanks for taking the time to give this a read! And thanks for building a great module!