thrilled-be-core
Version:
Core Express backend package with middleware, logging, security, and base application setup
182 lines • 6.43 kB
TypeScript
/**
* Custom Error Classes for Enterprise Applications
*
* This file defines a comprehensive set of custom error classes that provide
* better error handling, logging, and debugging capabilities throughout applications.
* These errors follow HTTP status codes and provide structured error information
* for both development and production environments.
*
* @package be-core
* @since 1.0.0
*/
import { HttpException } from 'thrilled-be-types';
/**
* Base application error class
* All custom errors should extend this class
*/
export declare abstract class AppError extends HttpException {
readonly timestamp: Date;
readonly context?: Record<string, unknown>;
code: string;
readonly isOperational: boolean;
status: number;
constructor(message: string, status: number, code: string, context?: Record<string, unknown>, isOperational?: boolean);
/**
* Convert error to JSON representation
*/
toJSON(): {
name: string;
message: string;
status: number;
code: string;
timestamp: string;
context: Record<string, unknown> | undefined;
isOperational: boolean;
};
}
/**
* Database-related errors
*/
export declare class DatabaseError extends AppError {
constructor(message: string, context?: Record<string, unknown>, originalError?: Error);
}
export declare class DatabaseConnectionError extends DatabaseError {
constructor(message?: string, context?: Record<string, unknown>);
}
export declare class QueryTimeoutError extends DatabaseError {
constructor(message?: string, context?: Record<string, unknown>);
}
export declare class DatabaseConstraintError extends DatabaseError {
constructor(message: string, constraint: string, context?: Record<string, unknown>);
}
export declare class TransactionError extends DatabaseError {
constructor(message?: string, context?: Record<string, unknown>);
}
/**
* Authentication and Authorization errors
*/
export declare class AuthenticationError extends AppError {
constructor(message?: string, context?: Record<string, unknown>);
}
export declare class AuthorizationError extends AppError {
constructor(message?: string, context?: Record<string, unknown>);
}
export declare class TokenExpiredError extends AuthenticationError {
constructor(message?: string, context?: Record<string, unknown>);
}
export declare class InvalidTokenError extends AuthenticationError {
constructor(message?: string, context?: Record<string, unknown>);
}
export declare class SessionExpiredError extends AuthenticationError {
constructor(message?: string, context?: Record<string, unknown>);
}
/**
* Validation errors
*/
export declare class ValidationError extends AppError {
readonly fields?: Array<{
field: string;
message: string;
value?: unknown;
}>;
constructor(message: string, fields?: Array<{
field: string;
message: string;
value?: unknown;
}>, context?: Record<string, unknown>);
}
export declare class SchemaValidationError extends ValidationError {
constructor(message: string, fields?: Array<{
field: string;
message: string;
value?: unknown;
}>, context?: Record<string, unknown>);
}
export declare class BusinessRuleValidationError extends ValidationError {
constructor(message: string, rule: string, context?: Record<string, unknown>);
}
/**
* Resource errors
*/
export declare class ResourceError extends AppError {
constructor(message: string, status: number, code: string, context?: Record<string, unknown>);
}
export declare class NotFoundError extends ResourceError {
constructor(resource: string, identifier?: string | number, context?: Record<string, unknown>);
}
export declare class ConflictError extends ResourceError {
constructor(message: string, resource?: string, context?: Record<string, unknown>);
}
export declare class DuplicateResourceError extends ConflictError {
constructor(resource: string, field: string, value: unknown, context?: Record<string, unknown>);
}
/**
* External service errors
*/
export declare class ExternalServiceError extends AppError {
constructor(message: string, service: string, context?: Record<string, unknown>);
}
export declare class ServiceUnavailableError extends ExternalServiceError {
constructor(message?: string, service?: string, context?: Record<string, unknown>);
}
export declare class ThirdPartyAPIError extends ExternalServiceError {
constructor(message: string, apiName: string, statusCode?: number, context?: Record<string, unknown>);
}
/**
* Rate limiting errors
*/
export declare class RateLimitError extends AppError {
constructor(message?: string, context?: Record<string, unknown>);
}
/**
* Configuration errors
*/
export declare class ConfigurationError extends AppError {
constructor(message: string, configKey?: string, context?: Record<string, unknown>);
}
/**
* System errors
*/
export declare class SystemError extends AppError {
constructor(message: string, context?: Record<string, unknown>);
}
export declare class InternalServerError extends SystemError {
constructor(message?: string, context?: Record<string, unknown>);
}
export declare class RequestTimeoutError extends AppError {
constructor(message?: string, context?: Record<string, unknown>);
}
/**
* Utility functions for error handling
*/
export declare class ErrorUtils {
/**
* Check if error is operational (expected) or programming error
*/
static isOperationalError(error: Error): boolean;
/**
* Convert any error to AppError instance
*/
static toAppError(error: unknown): AppError;
/**
* Check if error should be logged
*/
static shouldLog(error: Error): boolean;
/**
* Get error message safe for production
*/
static getPublicMessage(error: Error): string;
/**
* Extract error context for logging
*/
static getErrorContext(error: Error): Record<string, unknown>;
}
/**
* Error handler decorator for async functions
*/
export declare function handleErrors(target: unknown, propertyKey: string, descriptor: PropertyDescriptor): PropertyDescriptor;
/**
* Async wrapper for error handling
*/
export declare function asyncHandler<T extends unknown[], R>(fn: (...args: T) => Promise<R>): (...args: T) => Promise<R>;
//# sourceMappingURL=errors.d.ts.map