all files / modules/middleware/ catch.js

100% Statements 8/8
100% Branches 2/2
100% Functions 3/3
100% Lines 8/8
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                                   
/**
 * 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;