UNPKG

@heleneb1/ts-errors

Version:

Lightweight TypeScript library to create, manage and log typed, structured errors for Node.js, Express, and JavaScript apps.

37 lines (36 loc) 2.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GatewayTimeoutError = exports.ServiceUnavailableError = exports.ConflictError = exports.InternalServerError = exports.TooManyRequestsError = exports.UnprocessableEntityError = exports.ForbiddenError = exports.BadRequestError = exports.UnauthorizedError = exports.NotFoundError = exports.createErrorFunction = exports.errorMap = void 0; const CustomError_1 = require("./CustomError"); // Mapping : code => emoji + default message exports.errorMap = { 400: { defaultMsg: "Invalid request", emoji: '❗', category: 'Client Error' }, 401: { defaultMsg: "Unauthorized", emoji: '⛔', category: 'Client Error' }, 403: { defaultMsg: "Forbidden", emoji: '⛔', category: 'Client Error' }, 404: { defaultMsg: "Resource not found", emoji: '⁉️', category: 'Client Error' }, 409: { defaultMsg: "Conflict", emoji: '⚔️', category: 'Client Error' }, 422: { defaultMsg: "Unprocessable entity", emoji: '❌', category: 'Client Error' }, 429: { defaultMsg: "Too many requests", emoji: '⏸️', category: 'Client Error' }, 500: { defaultMsg: "Internal server error", emoji: '⚙️', category: 'Server Error' }, 503: { defaultMsg: "Service unavailable", emoji: '⚙️', category: 'Server Error' }, 504: { defaultMsg: "Gateway timeout", emoji: '⏱️', category: 'Server Error' }, }; // Génère dynamiquement les fonctions d’erreurs const createErrorFunction = (code) => { const { defaultMsg } = exports.errorMap[code] || { defaultMsg: "Error" }; return (message, details = {}) => { return new CustomError_1.CustomError(message ?? defaultMsg, code, details); }; }; exports.createErrorFunction = createErrorFunction; // Export des erreurs spécifiques exports.NotFoundError = (0, exports.createErrorFunction)(404); exports.UnauthorizedError = (0, exports.createErrorFunction)(401); exports.BadRequestError = (0, exports.createErrorFunction)(400); exports.ForbiddenError = (0, exports.createErrorFunction)(403); exports.UnprocessableEntityError = (0, exports.createErrorFunction)(422); exports.TooManyRequestsError = (0, exports.createErrorFunction)(429); exports.InternalServerError = (0, exports.createErrorFunction)(500); exports.ConflictError = (0, exports.createErrorFunction)(409); exports.ServiceUnavailableError = (0, exports.createErrorFunction)(503); exports.GatewayTimeoutError = (0, exports.createErrorFunction)(504);