Engineering
error-handling avatar

error-handling

Implement robust backend error handling with custom classes, middleware, structured logging, and recovery patterns.

Introduction

This skill provides a standardized architectural framework for managing backend application errors, focusing on observability, debuggability, and graceful service recovery. Designed for professional development teams building Node.js/TypeScript services, it establishes clear patterns for distinguishing between operational errors and system failures. By using a centralized AppError base class, developers can enforce consistent HTTP status codes, structured error responses, and meaningful error identifiers. This approach ensures that client-side integrations receive predictable error shapes while internal systems maintain high-fidelity logs for monitoring and incident response.

  • Implements a hierarchical custom error class structure including common types like ValidationError, NotFoundError, UnauthorizedError, ForbiddenError, ConflictError, and RateLimitError.

  • Provides production-ready error handling middleware for Express.js that intercepts errors, logs contextual metadata, and formats responses based on the environment (development vs. production).

  • Includes asynchronous error handling wrappers to ensure promise rejections are correctly captured and passed to the global error middleware.

  • Integrates Zod schema validation to transform complex input parsing errors into user-friendly validation reports.

  • Offers database error mapping strategies to sanitize low-level SQL/ORM errors (like UniqueConstraintError) into meaningful application-level domain errors.

  • Facilitates structured logging of request context, stack traces, and relevant metadata for debugging distributed systems.

  • Input: Typically invoked when implementing or refactoring API routes, service-layer logic, or database repository operations.

  • Output: Produces clean, typed TypeScript error classes and modular middleware functions integrated into your application pipeline.

  • Use this skill to avoid inconsistent error shapes across your API and to reduce the time spent debugging unexpected server crashes.

  • When implementing, ensure that sensitive stack trace information is restricted to non-production environments to maintain security best practices.

  • Align your application's error response interface with the provided standard ErrorResponse pattern for easier frontend integration.

Repository Stats

Stars
255
Forks
31
Open Issues
7
Language
TypeScript
Default Branch
main
Sync Status
Idle
Last Synced
Apr 30, 2026, 08:52 AM
View on GitHub