Skip to content

[Backend] Consolidate Error Handling with Custom Error Classes #109

Description

@devJaja

Problem

Error handling across the backend is inconsistent:

  • Some routes throw generic Error objects
  • Others return res.status(500).json({ error: 'message' }) directly
  • The error handler in backend/src/api/middleware/errorHandler.ts only handles known error shapes
  • No distinction between client errors (4xx) and server errors (5xx)

Current State

Looking at error handling in routes and coordinator:

// Some places:
throw new Error('Task not found');

// Others:
return res.status(500).json({ error: 'Internal error' });

// Error handler assumes:
if (err instanceof AppError) { ... }

Proposed Solution

  1. Create backend/src/errors/ directory with:
    • AppError.ts — base class with statusCode, isOperational, code
    • ValidationError.ts — 400 with field details
    • NotFoundError.ts — 404 with resource type/id
    • UnauthorizedError.ts — 401/403
    • RateLimitError.ts — 429
  2. Update error handler to recognize all custom error types
  3. Add error.code for machine-readable error identification
  4. Ensure all errors are operational (no stack traces in production responses)

Acceptance Criteria

  • Custom error classes in backend/src/errors/
  • Error handler handles all custom types
  • No raw Error objects thrown in routes
  • Error responses have consistent shape: { error: { code, message, details? } }
  • Stack traces hidden in production
  • All existing error tests pass

Difficulty

High — touches every route and middleware

Labels

backend enhancement complex

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions