ignore-errors
Version:
Easily ignore specific promise errors
44 lines • 1.6 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ignoreAny = exports.ignoreCode = exports.ignoreReason = exports.ignoreStatus = exports.ignoreName = exports.ignoreMessage = void 0;
const shallow_contains_1 = __importDefault(require("shallow-contains"));
const isRegExp = (val) => Object.prototype.toString.call(val) === '[object RegExp]';
const shallowContainsComparator = (a, b) => {
return isRegExp(b) ? b.test(a) : a === b;
};
function ignore(...args) {
return (err) => {
const match = args.length === 2 ? { [args[0]]: args[1] } : args[0];
if (shallow_contains_1.default(err, match, shallowContainsComparator)) {
// ignore
return null;
}
throw err;
};
}
exports.default = ignore;
exports.ignoreMessage = (val) => ignore('message', val);
exports.ignoreName = (val) => ignore('name', val);
exports.ignoreStatus = (val) => ignore('status', val);
exports.ignoreReason = (val) => ignore('reason', val);
exports.ignoreCode = (val) => ignore('code', val);
exports.ignoreAny = (...ignoreFns) => {
return (err) => {
const somePassed = ignoreFns.some((ignoreFn) => {
try {
ignoreFn(err);
return true;
}
catch (err) {
return false;
}
});
if (somePassed)
return null;
throw err;
};
};
//# sourceMappingURL=index.js.map