UNPKG

@ticatec/express-exception

Version:

A comprehensive set of reusable error classes and middleware for Node.js Express applications with standardized error handling and consistent response formats.

110 lines (109 loc) 4.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ActionNotFoundError = exports.IllegalParameterError = exports.InsufficientPermissionError = exports.UnauthenticatedError = exports.AppError = void 0; if (!global.AppError) { /** * Generic application error with a custom error code. * Used for application-specific errors that need to be handled differently from standard errors. */ class AppError extends Error { /** * Gets the error code associated with this application error. * @returns The numeric error code */ get code() { return this._code; } /** * Creates a new AppError instance. * @param code - The numeric error code to associate with this error * @param message - Optional error message. If null, uses the default error message */ constructor(code, message = null) { super(message); //@ts-ignore Error.captureStackTrace(this, this.constructor); this.name = this.constructor.name; this._code = code; } } global.AppError = AppError; } if (!global.UnauthenticatedError) { /** * Error thrown when an unauthenticated user attempts to access a protected resource. * This error indicates that the user needs to authenticate before proceeding. */ class UnauthenticatedError extends Error { /** * Creates a new UnauthenticatedError instance with a default message. */ constructor() { super('Unauthenticated user is accessing the system.'); //@ts-ignore Error.captureStackTrace(this, this.constructor); this.name = this.constructor.name; } } global.UnauthenticatedError = UnauthenticatedError; } if (!global.InsufficientPermissionError) { /** * Error thrown when an authenticated user lacks sufficient permissions to perform a specific action. * This error indicates that the user is authenticated but doesn't have the required authorization level. */ class InsufficientPermissionError extends Error { /** * Creates a new InsufficientPermissionError instance with a default message. */ constructor() { super('User doesn\'t has permission to access this function.'); //@ts-ignore Error.captureStackTrace(this, this.constructor); this.name = this.constructor.name; } } global.InsufficientPermissionError = InsufficientPermissionError; } if (!global.IllegalParameterError) { /** * Error thrown when invalid or illegal parameters are provided to a function or API endpoint. * This error indicates that the input parameters don't meet the expected format or validation rules. */ class IllegalParameterError extends Error { /** * Creates a new IllegalParameterError instance. * @param message - The error message describing which parameter is invalid or why it's illegal */ constructor(message) { super(message); //@ts-ignore Error.captureStackTrace(this, this.constructor); this.name = this.constructor.name; } } global.IllegalParameterError = IllegalParameterError; } if (!global.ActionNotFoundError) { /** * Error thrown when a requested web action or route cannot be found. * This error indicates that the requested endpoint or action doesn't exist in the application. */ class ActionNotFoundError extends Error { /** * Creates a new ActionNotFoundError instance with a default message. */ constructor() { super('Web action not found.'); //@ts-ignore Error.captureStackTrace(this, this.constructor); this.name = this.constructor.name; } } global.ActionNotFoundError = ActionNotFoundError; } exports.AppError = global.AppError; exports.UnauthenticatedError = global.UnauthenticatedError; exports.InsufficientPermissionError = global.InsufficientPermissionError; exports.IllegalParameterError = global.IllegalParameterError; exports.ActionNotFoundError = global.ActionNotFoundError;