all files / blackbird/modules/middleware/ catch.js

100% Statements 6/6
100% Branches 2/2
100% Functions 2/2
100% Lines 6/6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23                                 
/*   */
/**
 * 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, curry} = require("ramda");
module.exports = curry(function catchError(app, conn) {
    return conn.run(app).then(null, function (reason) {
        if (is(Error, reason)) {
            throw reason;
        }
 
        return reason;
    });
});