UNPKG

stringiful

Version:

Easy to use stringify function with built-in configurable formatters

38 lines 1.88 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getErrorFormatter = void 0; const lodash_1 = __importDefault(require("lodash")); const config_1 = __importDefault(require("../../config")); const axiosError_1 = require("./axiosError"); const getErrorFormatter = (formatterConfig) => { var _a; const { formattersDefaultParams: { error: { maxMessageLength, allowedProperties }, }, } = config_1.default; const baseErrorFormatter = { matches: (obj) => { return !axiosError_1.isAxiosError(obj) && obj instanceof Error; }, }; const defaultFormatFunction = (error) => { var _a, _b, _c, _d; const selectedMaxMessageLength = (_b = (_a = formatterConfig.params) === null || _a === void 0 ? void 0 : _a.maxMessageLength) !== null && _b !== void 0 ? _b : maxMessageLength; const formattedMessage = (_d = (_c = lodash_1.default.get(error, 'message')) === null || _c === void 0 ? void 0 : _c.substring) === null || _d === void 0 ? void 0 : _d.call(_c, 0, selectedMaxMessageLength); if (formattedMessage) { lodash_1.default.set(error, 'message', formattedMessage); } return error; }; baseErrorFormatter.format = (_a = formatterConfig.format) !== null && _a !== void 0 ? _a : defaultFormatFunction; const { fieldsBlacklist, fieldsWhitelist } = formatterConfig; if (fieldsWhitelist) { return { ...baseErrorFormatter, fieldsWhitelist }; } if (fieldsBlacklist) { return { ...baseErrorFormatter, fieldsBlacklist }; } return { ...baseErrorFormatter, fieldsWhitelist: allowedProperties }; }; exports.getErrorFormatter = getErrorFormatter; //# sourceMappingURL=error.js.map