Skip to content

feat: add centralized error handling with error codes and validation …#116

Merged
memplethee-lab merged 1 commit into
SourceXXL:mainfrom
Tobi-8:feat/centralized-error-handling
Jul 22, 2026
Merged

feat: add centralized error handling with error codes and validation …#116
memplethee-lab merged 1 commit into
SourceXXL:mainfrom
Tobi-8:feat/centralized-error-handling

Conversation

@Tobi-8

@Tobi-8 Tobi-8 commented Jul 21, 2026

Copy link
Copy Markdown

Closes #107

What

Adds a centralized error handling module with custom exception classes, error codes, and field-level validation error responses.

Changes

  • src/common/errors/error-codes.tsErrorCode enum for consistent programmatic error identification (NOT_FOUND, UNAUTHORIZED, VALIDATION_ERROR, CONFLICT, etc.)
  • src/common/errors/app.exception.ts — Base AppException class extending HttpException with an errorCode field, plus convenience subclasses: NotFoundException, UnauthorizedException, ForbiddenException, BadRequestException, ConflictException, RateLimitException
  • src/common/filters/global-exception.filter.ts — Updated to:
    • Include errorCode in all error responses
    • Extract and return field-level validation errors for 400 responses (class-validator)
    • Reuse x-request-id header as correlation ID when present
    • Extract Sentry reporting and logging into helper methods
  • src/common/index.ts — Re-exports new exception classes and error codes

Error Response Format

All errors now return:

{
  "statusCode": 400,
  "errorCode": "VALIDATION_ERROR",
  "message": "Validation failed",
  "errors": { "email": ["email must be an email"] },
  "correlationId": "...",
  "timestamp": "2026-07-21T...",
  "path": "/api/v1/auth/register"
}
Validation errors, 401, 403, 404, and 409 all map to their correct HTTP status codes with matching errorCode values. Stack traces remain hidden in production, visible in development.
Why
Standardizes error responses across the API so clients can handle errors programmatically using errorCode instead of parsing message strings. Existing services using NestJS built-in exceptions continue to work unchanged — the filter maps their status codes to error codes automatically.

closes #107

…details

- Add ErrorCode enum for consistent error identification
- Add custom exception classes (AppException, NotFoundException, etc.) with error codes
- Update GlobalExceptionFilter to include error codes in responses
- Add field-level validation error extraction for 400 responses
- Extract Sentry reporting and logging into private helper methods
- Use x-request-id header as correlation ID when available
@memplethee-lab
memplethee-lab merged commit 7870278 into SourceXXL:main Jul 22, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Global Error Handling and Exception Module

2 participants