martinms

Untitled

Oct 5th, 2025
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.59 KB | None | 0 0
  1. /**
  2.  * Error Utility Functions
  3.  *
  4.  * This file provides custom error classes and error handling utilities
  5.  * for consistent error management throughout the application.
  6.  *
  7.  * @author Sahabat Ibu Hamil Team
  8.  * @version 1.0.0
  9.  */
  10.  
  11. /**
  12.  * Custom Application Error class
  13.  * Extends the built-in Error class with additional properties for HTTP status codes
  14.  */
  15. export class AppError extends Error {
  16.   public statusCode: number;
  17.   public isOperational: boolean;
  18.  
  19.   constructor(message: string, statusCode: number = 500, isOperational: boolean = true) {
  20.     super(message);
  21.  
  22.     this.statusCode = statusCode;
  23.     this.isOperational = isOperational;
  24.  
  25.     // Maintains proper stack trace for where our error was thrown (only available on V8)
  26.     if (Error.captureStackTrace) {
  27.       Error.captureStackTrace(this, AppError);
  28.     }
  29.  
  30.     this.name = this.constructor.name;
  31.   }
  32. }
  33.  
  34. /**
  35.  * Validation Error class
  36.  * Specific error type for validation failures
  37.  */
  38. export class ValidationFailedError<TDetails = unknown> extends AppError {
  39.   public readonly details?: TDetails;
  40.  
  41.   constructor(details?: TDetails, message = 'Validation failed') {
  42.     super(message, 422);
  43.     if (details !== undefined) {
  44.       this.details = details;
  45.     }
  46.   }
  47. }
  48.  
  49. /**
  50.  * Not Found Error class
  51.  * Specific error type for resource not found scenarios
  52.  */
  53. export class NotFoundError extends AppError {
  54.   constructor(message: string = 'Resource not found') {
  55.     super(message, 404);
  56.   }
  57. }
  58.  
  59. /**
  60.  * Unauthorized Error class
  61.  * Specific error type for authentication failures
  62.  */
  63. export class UnauthorizedError extends AppError {
  64.   constructor(message: string = 'Unauthorized access') {
  65.     super(message, 401);
  66.   }
  67. }
  68.  
  69. /**
  70.  * Forbidden Error class
  71.  * Specific error type for authorization failures
  72.  */
  73. export class ForbiddenError extends AppError {
  74.   constructor(message: string = 'Forbidden access') {
  75.     super(message, 403);
  76.   }
  77. }
  78.  
  79. /**
  80.  * Conflict Error class
  81.  * Specific error type for resource conflicts
  82.  */
  83. export class ConflictError extends AppError {
  84.   constructor(message: string = 'Resource conflict') {
  85.     super(message, 409);
  86.   }
  87. }
  88.  
  89. /**
  90.  * Bad Request Error class
  91.  * Specific error type for bad request scenarios
  92.  */
  93. export class BadRequestError extends AppError {
  94.   constructor(message: string = 'Bad request') {
  95.     super(message, 400);
  96.   }
  97. }
  98.  
  99. /**
  100.  * Internal Server Error class
  101.  * Specific error type for internal server errors
  102.  */
  103. export class InternalServerError extends AppError {
  104.   constructor(message: string = 'Internal server error') {
  105.     super(message, 500);
  106.   }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment