UNPKG

@catbee/utils

Version:

A modular, production-grade utility toolkit for Node.js and TypeScript, designed for robust, scalable applications (including Express-based services). All utilities are tree-shakable and can be imported independently.

363 lines (358 loc) 12.8 kB
/* * The MIT License * * Copyright (c) 2026 Catbee Technologies. https://catbee.in/license * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ 'use strict'; var httpStatusCodes = require('@catbee/utils/http-status-codes'); var logger = require('@catbee/utils/logger'); var response = require('@catbee/utils/response'); var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var HttpError = class extends response.ErrorResponse { static { __name(this, "HttpError"); } /** * Creates a new HTTP error instance. * @param status - A valid HTTP status code (e.g., 400, 500). * @param message - The error message to return to the client. */ constructor(status, message) { super(message); this.status = status; } }; var InternalServerErrorException = class extends response.ErrorResponse { static { __name(this, "InternalServerErrorException"); } /** * @param message - Optional custom message. Defaults to "Internal server error". */ constructor(message = "Internal server error") { super(message); this.status = httpStatusCodes.HttpStatusCodes.INTERNAL_SERVER_ERROR; } }; var UnauthorizedException = class extends response.ErrorResponse { static { __name(this, "UnauthorizedException"); } /** * @param message - Optional custom message. Defaults to "Unauthorized". */ constructor(message = "Unauthorized") { super(message); this.status = httpStatusCodes.HttpStatusCodes.UNAUTHORIZED; } }; var BadRequestException = class extends response.ErrorResponse { static { __name(this, "BadRequestException"); } /** * @param message - Optional custom message. Defaults to "Bad request". */ constructor(message = "Bad request") { super(message); this.status = httpStatusCodes.HttpStatusCodes.BAD_REQUEST; } }; var NotFoundException = class extends response.ErrorResponse { static { __name(this, "NotFoundException"); } /** * @param message - Optional custom message. Defaults to "Resource not found". */ constructor(message = "Resource not found") { super(message); this.status = httpStatusCodes.HttpStatusCodes.NOT_FOUND; } }; var ForbiddenException = class extends response.ErrorResponse { static { __name(this, "ForbiddenException"); } /** * @param message - Optional custom message. Defaults to "Forbidden". */ constructor(message = "Forbidden") { super(message); this.status = httpStatusCodes.HttpStatusCodes.FORBIDDEN; } }; var ConflictException = class extends response.ErrorResponse { static { __name(this, "ConflictException"); } /** * @param message - Optional custom message. Defaults to "Conflict". */ constructor(message = "Conflict") { super(message); this.status = httpStatusCodes.HttpStatusCodes.CONFLICT; } }; var BadGatewayException = class extends response.ErrorResponse { static { __name(this, "BadGatewayException"); } /** * @param message - Optional custom message. Defaults to "Bad Gateway". */ constructor(message = "Bad Gateway") { super(message); this.status = httpStatusCodes.HttpStatusCodes.BAD_GATEWAY; } }; var TooManyRequestsException = class extends response.ErrorResponse { static { __name(this, "TooManyRequestsException"); } /** * @param message - Optional custom message. Defaults to "Too many requests". */ constructor(message = "Too many requests") { super(message); this.status = httpStatusCodes.HttpStatusCodes.TOO_MANY_REQUESTS; } }; var ServiceUnavailableException = class extends response.ErrorResponse { static { __name(this, "ServiceUnavailableException"); } /** * @param message - Optional custom message. Defaults to "Service Unavailable". */ constructor(message = "Service Unavailable") { super(message); this.status = httpStatusCodes.HttpStatusCodes.SERVICE_UNAVAILABLE; } }; var GatewayTimeoutException = class extends response.ErrorResponse { static { __name(this, "GatewayTimeoutException"); } /** * @param message - Optional custom message. Defaults to "Gateway Timeout". */ constructor(message = "Gateway Timeout") { super(message); this.status = httpStatusCodes.HttpStatusCodes.GATEWAY_TIMEOUT; } }; var UnprocessableEntityException = class extends response.ErrorResponse { static { __name(this, "UnprocessableEntityException"); } details; /** * @param message - Optional custom message. Defaults to "Unprocessable Entity". * @param details - Optional validation details or additional error context. */ constructor(message = "Unprocessable Entity", details) { super(message), this.details = details; this.status = httpStatusCodes.HttpStatusCodes.UNPROCESSABLE_ENTITY; } }; var MethodNotAllowedException = class extends response.ErrorResponse { static { __name(this, "MethodNotAllowedException"); } allowedMethods; /** * @param message - Optional custom message. Defaults to "Method Not Allowed". * @param allowedMethods - Optional array of allowed HTTP methods. */ constructor(message = "Method Not Allowed", allowedMethods) { super(message), this.allowedMethods = allowedMethods; this.status = httpStatusCodes.HttpStatusCodes.METHOD_NOT_ALLOWED; } }; var NotAcceptableException = class extends response.ErrorResponse { static { __name(this, "NotAcceptableException"); } /** * @param message - Optional custom message. Defaults to "Not Acceptable". */ constructor(message = "Not Acceptable") { super(message); this.status = httpStatusCodes.HttpStatusCodes.NOT_ACCEPTABLE; } }; var RequestTimeoutException = class extends response.ErrorResponse { static { __name(this, "RequestTimeoutException"); } /** * @param message - Optional custom message. Defaults to "Request Timeout". */ constructor(message = "Request Timeout") { super(message); this.status = httpStatusCodes.HttpStatusCodes.REQUEST_TIMEOUT; } }; var UnsupportedMediaTypeException = class extends response.ErrorResponse { static { __name(this, "UnsupportedMediaTypeException"); } /** * @param message - Optional custom message. Defaults to "Unsupported Media Type". */ constructor(message = "Unsupported Media Type") { super(message); this.status = httpStatusCodes.HttpStatusCodes.UNSUPPORTED_MEDIA_TYPE; } }; var PayloadTooLargeException = class extends response.ErrorResponse { static { __name(this, "PayloadTooLargeException"); } /** * @param message - Optional custom message. Defaults to "Payload Too Large". */ constructor(message = "Payload Too Large") { super(message); this.status = httpStatusCodes.HttpStatusCodes.PAYLOAD_TOO_LARGE; } }; var InsufficientStorageException = class extends response.ErrorResponse { static { __name(this, "InsufficientStorageException"); } /** * @param message - Optional custom message. Defaults to "Insufficient Storage". */ constructor(message = "Insufficient Storage") { super(message); this.status = httpStatusCodes.HttpStatusCodes.INSUFFICIENT_STORAGE; } }; function isHttpError(error) { return error instanceof response.ErrorResponse; } __name(isHttpError, "isHttpError"); function createHttpError(status, message) { switch (status) { case httpStatusCodes.HttpStatusCodes.BAD_REQUEST: return new BadRequestException(message); case httpStatusCodes.HttpStatusCodes.UNAUTHORIZED: return new UnauthorizedException(message); case httpStatusCodes.HttpStatusCodes.FORBIDDEN: return new ForbiddenException(message); case httpStatusCodes.HttpStatusCodes.NOT_FOUND: return new NotFoundException(message); case httpStatusCodes.HttpStatusCodes.METHOD_NOT_ALLOWED: return new MethodNotAllowedException(message); case httpStatusCodes.HttpStatusCodes.NOT_ACCEPTABLE: return new NotAcceptableException(message); case httpStatusCodes.HttpStatusCodes.REQUEST_TIMEOUT: return new RequestTimeoutException(message); case httpStatusCodes.HttpStatusCodes.CONFLICT: return new ConflictException(message); case httpStatusCodes.HttpStatusCodes.GONE: return new HttpError(httpStatusCodes.HttpStatusCodes.GONE, message || "Gone"); case httpStatusCodes.HttpStatusCodes.PAYLOAD_TOO_LARGE: return new PayloadTooLargeException(message); case httpStatusCodes.HttpStatusCodes.UNSUPPORTED_MEDIA_TYPE: return new UnsupportedMediaTypeException(message); case httpStatusCodes.HttpStatusCodes.UNPROCESSABLE_ENTITY: return new UnprocessableEntityException(message); case httpStatusCodes.HttpStatusCodes.TOO_MANY_REQUESTS: return new TooManyRequestsException(message); case httpStatusCodes.HttpStatusCodes.INTERNAL_SERVER_ERROR: return new InternalServerErrorException(message); case httpStatusCodes.HttpStatusCodes.BAD_GATEWAY: return new BadGatewayException(message); case httpStatusCodes.HttpStatusCodes.SERVICE_UNAVAILABLE: return new ServiceUnavailableException(message); case httpStatusCodes.HttpStatusCodes.GATEWAY_TIMEOUT: return new GatewayTimeoutException(message); case httpStatusCodes.HttpStatusCodes.INSUFFICIENT_STORAGE: return new InsufficientStorageException(message); default: return new HttpError(status, message || `HTTP Error ${status}`); } } __name(createHttpError, "createHttpError"); function hasErrorShape(error) { return typeof error === "object" && error !== null && "message" in error && typeof error.message === "string"; } __name(hasErrorShape, "hasErrorShape"); function getErrorMessage(error) { if (typeof error === "string") { return error; } if (error instanceof Error) { return error.message; } if (hasErrorShape(error)) { return error.message; } return "Unknown error occurred"; } __name(getErrorMessage, "getErrorMessage"); function withErrorHandling(handler) { return async (...args) => { try { return await handler(...args); } catch (err) { if (isHttpError(err)) { throw err; } logger.getLogger().error({ err }, "Caught error in handler"); let status = httpStatusCodes.HttpStatusCodes.INTERNAL_SERVER_ERROR; if (hasErrorShape(err) && typeof err.status === "number") { status = err.status; } throw createHttpError(status, getErrorMessage(err)); } }; } __name(withErrorHandling, "withErrorHandling"); exports.BadGatewayException = BadGatewayException; exports.BadRequestException = BadRequestException; exports.ConflictException = ConflictException; exports.ForbiddenException = ForbiddenException; exports.GatewayTimeoutException = GatewayTimeoutException; exports.HttpError = HttpError; exports.InsufficientStorageException = InsufficientStorageException; exports.InternalServerErrorException = InternalServerErrorException; exports.MethodNotAllowedException = MethodNotAllowedException; exports.NotAcceptableException = NotAcceptableException; exports.NotFoundException = NotFoundException; exports.PayloadTooLargeException = PayloadTooLargeException; exports.RequestTimeoutException = RequestTimeoutException; exports.ServiceUnavailableException = ServiceUnavailableException; exports.TooManyRequestsException = TooManyRequestsException; exports.UnauthorizedException = UnauthorizedException; exports.UnprocessableEntityException = UnprocessableEntityException; exports.UnsupportedMediaTypeException = UnsupportedMediaTypeException; exports.createHttpError = createHttpError; exports.getErrorMessage = getErrorMessage; exports.hasErrorShape = hasErrorShape; exports.isHttpError = isHttpError; exports.withErrorHandling = withErrorHandling;