WayrApp Backend & Ecosystem Documentation - v1.0.0
    Preparing search index...

    Class AppError

    Custom Application Error Class for WayrApp Backend

    A specialized error class that extends the native JavaScript Error to provide structured error handling throughout the WayrApp language learning platform. This class standardizes error representation by including HTTP status codes, application-specific error codes, and operational flags that help distinguish between expected application errors and unexpected system failures.

    The AppError class is designed to work seamlessly with the errorHandler middleware, providing a consistent error format across all API endpoints. It supports the application's evolution toward a distributed architecture where standardized error handling becomes critical for inter-node communication and debugging across multiple services.

    This error class is used throughout the application for throwing predictable, well-structured errors that can be properly handled by the error middleware and returned to clients in a consistent format. It's particularly useful for validation errors, authentication failures, authorization issues, and business logic violations.

    Key Features:

    • Extends native Error class for proper inheritance
    • Includes HTTP status codes for proper response handling
    • Contains application-specific error codes for categorization
    • Marks errors as operational to distinguish from system failures
    • Automatically captures stack traces for debugging
    • Integrates seamlessly with the global error handler

    Usage Patterns:

    • Validation errors: Use with BAD_REQUEST status and VALIDATION_ERROR code
    • Authentication errors: Use with UNAUTHORIZED status and AUTHENTICATION_ERROR code
    • Authorization errors: Use with FORBIDDEN status and AUTHORIZATION_ERROR code
    • Resource not found: Use with NOT_FOUND status and NOT_FOUND code
    • Conflict errors: Use with CONFLICT status and CONFLICT code
    // Throwing a validation error in a service
    import { AppError } from '@/shared/middleware/errorHandler';
    import { ErrorCodes, HttpStatus } from '@/shared/types';

    if (!user.email) {
    throw new AppError(
    'Email is required for user registration',
    HttpStatus.BAD_REQUEST,
    ErrorCodes.VALIDATION_ERROR
    );
    }
    // Throwing an authorization error in a controller
    if (user.role !== 'admin') {
    throw new AppError(
    'Admin access required for this operation',
    HttpStatus.FORBIDDEN,
    ErrorCodes.AUTHORIZATION_ERROR
    );
    }
    // Throwing a not found error in a service
    const course = await prisma.course.findUnique({ where: { id } });
    if (!course) {
    throw new AppError(
    'Course not found',
    HttpStatus.NOT_FOUND,
    ErrorCodes.NOT_FOUND
    );
    }
    // Throwing a conflict error for duplicate resources
    const existingUser = await prisma.user.findUnique({ where: { email } });
    if (existingUser) {
    throw new AppError(
    'User with this email already exists',
    HttpStatus.CONFLICT,
    ErrorCodes.CONFLICT
    );
    }

    @class AppError

    Hierarchy

    • Error
      • AppError
    Index

    Constructors

    Properties

    Constructors

    • Creates a new AppError instance with structured error information

      This constructor automatically sets the error as operational and captures the stack trace for debugging purposes. The error will be properly handled by the global error handler middleware.

      Parameters

      • message: string

        Human-readable error message describing what went wrong

      • statusCode: number

        HTTP status code (e.g., 400, 401, 403, 404, 500)

      • code: string

        Application-specific error code from ErrorCodes enum

      Returns AppError

      The constructed error instance

      // Create a validation error
      const error = new AppError(
      'Invalid email format',
      HttpStatus.BAD_REQUEST,
      ErrorCodes.VALIDATION_ERROR
      );
      // Create a not found error
      const error = new AppError(
      'User not found',
      HttpStatus.NOT_FOUND,
      ErrorCodes.NOT_FOUND
      );

    Properties

    statusCode: number

    HTTP status code to be returned to the client

    400, 401, 403, 404, 409, 500
    
    code: string

    Application-specific error code for categorization and client-side handling

    'VALIDATION_ERROR', 'NOT_FOUND', 'AUTHORIZATION_ERROR'
    
    isOperational: boolean

    Flag indicating this is an expected operational error, not a system failure Operational errors are expected and should be handled gracefully by the application

    true