UNPKG

throwlhos

Version:

Thorw error objects directly from express response

54 lines (53 loc) 2.06 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var http_status_codes_1 = __importDefault(require("http-status-codes")); var camelCase = function (str) { return str.toLowerCase().replace(/(\_\w)/g, function (c) { return c[1].toUpperCase(); }); }; var addThrowlhosToObject = function (object) { var _loop_1 = function (httpStatus, httpCode) { if (!httpStatus.startsWith('get') && typeof httpCode !== 'function' && !['1', '2'].includes(String(httpCode).charAt(0))) { object['err_' + camelCase(httpStatus)] = function (message, errors, i18n) { var result = { code: httpCode, status: httpStatus, message: message !== null && message !== void 0 ? message : http_status_codes_1.default.getStatusText(String(httpCode)) }; if (errors && Object.keys(errors).length > 0) { result.errors = errors; } if (i18n) { result.i18n = i18n; } return result; }; } }; for (var _i = 0, _a = Object.entries(http_status_codes_1.default); _i < _a.length; _i++) { var _b = _a[_i], httpStatus = _b[0], httpCode = _b[1]; _loop_1(httpStatus, httpCode); } object['err_custom'] = function (message, code, errors, i18n) { var result = { message: message, code: code, status: 'CUSTOM_ERROR' }; if (errors && Object.keys(errors).length > 0) { result.errors = errors; } if (i18n) { result.i18n = i18n; } return result; }; }; var throwlhos = { middleware: function (request, response, next) { addThrowlhosToObject(response); next(); } }; addThrowlhosToObject(throwlhos); exports.default = throwlhos;