Skip to main content

29. Custom Exception Hierarchy

Date: 2025-10-20

Status

Accepted

Category

Core Architecture

Context

The MCP server currently uses generic Python exceptions (Exception, ValueError, RuntimeError) throughout the codebase. This leads to: Current Problems:
  1. Poor Error Handling: Cannot distinguish between different error types
  2. Vague Error Messages: Generic exceptions don’t provide context
  3. Difficult Debugging: Hard to trace error source in logs
  4. Lost Context: Stack traces don’t show business logic errors clearly
  5. Poor Observability: Cannot filter errors by type in metrics
  6. Inconsistent HTTP Status Codes: Same exception → different status codes
Analysis:
Impact:
  • Error handling is reactive, not proactive
  • Difficult to implement proper retry logic (which errors to retry?)
  • Metrics are generic (all errors lumped together)
  • User-facing errors lack clarity

Decision

Implement a comprehensive custom exception hierarchy that:
  1. Provides clear error semantics
  2. Includes rich error context (metadata, trace IDs)
  3. Maps to HTTP status codes automatically
  4. Enables fine-grained error handling
  5. Improves observability and debugging

Exception Hierarchy

Architecture

New Module: src/mcp_server_langgraph/core/exceptions.py

FastAPI Integration

Exception Handler

Migration Guide

Before (Generic Exceptions)

After (Custom Exceptions)

Consequences

Positive

  1. Better Error Handling
    • Can catch specific exceptions: except TokenExpiredError:
    • Implement custom retry logic per exception type
    • Different handling for client vs server errors
  2. Improved Observability
    • Error metrics by category/code
    • Trace IDs for debugging
    • Rich metadata in logs
  3. Better User Experience
    • Clear, actionable error messages
    • Appropriate HTTP status codes
    • Retry hints (Retry-After header)
  4. Easier Debugging
    • Stack traces show business logic errors
    • Metadata provides context
    • Trace IDs link to distributed traces
  5. Compliance
    • Specific exceptions for GDPR/HIPAA/SOC2 violations
    • Audit trail of compliance errors

Negative

  1. More Code
    • 300+ lines for exception hierarchy
    • Need to update all exception raises
  2. Learning Curve
    • Developers need to learn exception hierarchy
    • Need to choose correct exception type
  3. Migration Effort
    • Update ~100+ exception raises across codebase
    • Test all error paths

Implementation Plan

Week 1: Foundation

  • Create ADR-0029 (this document)
  • Create core/exceptions.py with hierarchy
  • Add FastAPI exception handlers
  • Write 50+ unit tests for exceptions
  • Update developer documentation

Week 2: Migration - Auth & Core

  • Migrate auth/ module to custom exceptions
  • Migrate core/ module to custom exceptions
  • Update error handling tests
  • Verify metrics are collected correctly

Week 3: Migration - LLM & External Services

  • Migrate llm/ module to custom exceptions
  • Add retry logic based on retry_policy
  • Wrap external service errors properly
  • Test circuit breaker integration

Week 4: Migration - Remaining Modules

  • Migrate api/, mcp/, compliance/ modules
  • Update all integration tests
  • Verify HTTP status codes are correct
  • Performance test (exception overhead)

Week 5: Documentation & Rollout

  • Update API documentation with error codes
  • Create error code reference guide
  • Deploy to staging, test error flows
  • Deploy to production

References


Last Updated: 2025-10-20 Next Review: 2025-11-20