Global Error Handling

Create custom exception classes and centralized NestJS exception filters.

1|Updated Dec 4, 2024
One-click install
npx skills add https://github.com/imkdw/imkdw-dev --skill global-error-handling-imkdw
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Global Error Handling
Source: https://github.com/imkdw/imkdw-dev/tree/main/.claude/skills/global-error-handling
Command: npx skills add https://github.com/imkdw/imkdw-dev --skill global-error-handling-imkdw

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Inconsistent error handling leads to unpredictable application behavior, difficult debugging, and a poor user experience. This Skill provides a standardized approach to error management, ensuring clarity and consistency across your application.

Core Features & Use Cases

  • Consistent Exception Design: Create custom exceptions with defined error codes and appropriate HTTP statuses, making errors predictable and easy to manage.
  • Centralized Error Management: Implement NestJS exception filters for uniform error responses, reducing boilerplate and ensuring a consistent API contract.
  • Fail-Fast Validation: Integrate early error detection in use cases and validators, preventing invalid states and improving application reliability.
  • Use Case: When a user attempts to access a non-existent resource, this Skill ensures the system consistently throws a specific exception (e.g., ArticleNotFoundException) with a 404 Not Found status and a user-friendly message, rather than a generic server error.

Quick Start

Help me create a new custom exception class for a 'ProductNotFound' error, ensuring it extends CustomException and uses HttpStatus.NOT_FOUND with a unique error code.

Frequently Asked Questions about Global Error Handling

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I implement centralized error handling in NestJS applications?

Centralized error handling in NestJS uses custom exception classes that extend a base CustomException, paired with exception filters that intercept and standardize error responses across your API. Define EXCEPTION_CODES for each error type, apply HttpStatus codes, and register filters globally to ensure consistent error formatting and user-facing messages.

What's the best way to structure custom exceptions with error codes?

Structure custom exceptions by extending CustomException, assigning unique EXCEPTION_CODES, mapping appropriate HttpStatus values, and including user-friendly messages. This approach ensures each exception carries metadata needed for logging, debugging, and consistent API responses without boilerplate in individual controllers.

How do I set up fail-fast validation in NestJS to catch errors early?

Fail-fast validation in NestJS integrates validation logic in use cases and custom validators that throw specific exceptions immediately when invalid state is detected, preventing propagation of bad data. Combine this with centralized exception filters to transform validation failures into standardized HTTP responses.

Can I use typed exceptions with TypeScript in NestJS?

Yes. Typed exceptions leverage TypeScript's type system to create strongly-typed custom exception classes that extend CustomException, ensuring compile-time safety and IDE autocomplete. Each exception type maps to a specific HttpStatus and EXCEPTION_CODE, enabling predictable error handling across your backend.

What are the limitations of inconsistent error handling without a framework?

Without standardized error handling, applications suffer from unpredictable exception behavior, scattered error logic across controllers and validators, difficulty debugging root causes, and inconsistent API error responses that confuse clients. A typed exception framework eliminates these issues by centralizing logic and enforcing consistency.

Does this approach work for validation errors in API request bodies?

Yes. This framework integrates with NestJS validators to throw custom exceptions when request validation fails, ensuring validation errors produce consistent API responses with defined HttpStatus codes and user-facing messages rather than generic 400 errors.