Skip to content

Implement Centralized API Error Handling #638

Description

@A6dulmalik

Implement Centralized API Error Handling

Priority

P0 — Critical

Labels

backend architecture security priority: p0

Description

Mind Block requires a consistent error-handling strategy across all backend modules. Currently, different services may return errors in different formats, making it difficult for the frontend to reliably display meaningful feedback.

A centralized exception-handling system should provide predictable API error responses while preventing sensitive internal information from being exposed to users.

Objective

Implement a global NestJS exception filter and standardized API error response format.

Standard Error Response

All API errors should follow a consistent structure:

{
  "success": false,
  "statusCode": 400,
  "error": "Bad Request",
  "message": "A human-readable error message",
  "code": "VALIDATION_ERROR",
  "timestamp": "2026-08-20T12:00:00.000Z",
  "path": "/api/game-sessions"
}

Error Categories

Support standardized error codes for:

VALIDATION_ERROR
UNAUTHORIZED
FORBIDDEN
NOT_FOUND
CONFLICT
RATE_LIMITED
SESSION_EXPIRED
SESSION_INVALID
CHALLENGE_UNAVAILABLE
INVALID_ANSWER
DUPLICATE_SUBMISSION
REWARD_NOT_ELIGIBLE
BLOCKCHAIN_ERROR
INTERNAL_SERVER_ERROR

Requirements

  • Create a global exception filter.
  • Standardize error response structures.
  • Map NestJS exceptions to application error codes.
  • Handle validation errors.
  • Handle database errors safely.
  • Handle authentication errors.
  • Handle authorization errors.
  • Handle unexpected server errors.
  • Prevent stack traces from being exposed in production.
  • Log unexpected server errors.
  • Include a unique request ID where applicable.

Acceptance Criteria

  • All API errors follow a consistent response structure.
  • Validation errors return meaningful field-level information.
  • Authentication errors return standardized responses.
  • Authorization errors return standardized responses.
  • Database errors do not expose sensitive information.
  • Stack traces are hidden in production.
  • Unexpected errors are logged.
  • Error codes are documented.
  • Unit tests cover common error scenarios.

Dependencies

None.

Metadata

Metadata

Assignees

Labels

GrantFox OSSIssue tracked in GrantFox OSSMaybe RewardedIssue may be eligible for a GrantFox rewardThird CampaignCampaign: Third Campaign

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions