| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 1× 1× 2× 2× 2× 1× 1× 1× | /**
* A middleware that "catches" non-Errors that are thrown from the downstream
* app and returns them instead. This can be useful for breaking out of a
* nested promise chain, for example.
*
* Example:
*
* mach.catch(function (conn) {
* throw 200;
* });
*/
const {is} = require("ramda");
function catchError(app) {
return function (conn) {
return conn.call(app).then(undefined, function (reason) {
if (is(Error, reason)) {
throw reason;
}
return reason;
});
};
}
module.exports = catchError;
|