Error Handling Pattern

Consistent, type-safe API error responses with custom error classes, centralized error handlers, structured response formats, and client-side error parsing.

What's Included#

  • Custom error class hierarchy (AppError, NotFoundError, UnauthorizedError, ForbiddenError, ValidationError, ConflictError, RateLimitError)
  • Structured error response format with message, code, and details
  • Error handler wrapper for route handlers that catches Zod, app, and unknown errors
  • Error logging utility that distinguishes operational errors from system errors
  • Client-side API request helper with typed error parsing
  • Zod validation error formatting into field-level error maps

Usage#

Via CLI#

Loading code block...

Via AI Assistant#

Ask your AI assistant:

  • "Use the error handling pattern from Bootspring"
  • "Apply the Bootspring error handling pattern to my project"

Key Considerations#

  • Create specific error classes for each failure type with appropriate HTTP status codes
  • Include machine-readable error codes alongside messages for programmatic client handling
  • Never expose stack traces or internal details in API error responses
  • Distinguish operational errors (expected, like 404) from system errors (bugs, like unhandled exceptions)
  • Log system errors for debugging but only return generic messages to clients
  • Use a consistent error response structure across all API endpoints