Skip to content

ExceptionHandler

reference
1 min readUpdated

Kind: Class

Source: packages/core/errors/exception-handler.ts

Part of: Core

ExceptionHandler centralizes application error handling within the core error layer. Its handle() method receives an exception, applies the project's error-handling policy, and translates it into the appropriate logging, response, or propagation behavior for the calling layer.

Methods

MethodSignatureReturns
handlehandle(exception: Error)void

Diagram

mermaid
graph LR
  A[Application Code] -->|throws or catches error| B[ExceptionHandler]
  B -->|handle(error)| C{Classify Exception}
  C --> D[Log / Report Error]
  C --> E[Create Standardized Error Result]
  E --> F[API, Worker, or Caller]

Usage

ts
import { ExceptionHandler } from "@your-project/core/errors/exception-handler";

const exceptionHandler = new ExceptionHandler();

try {
  await processOrder(orderId);
} catch (error) {
  return exceptionHandler.handle(error);
}

AI Coding Instructions

  • Route caught application errors through ExceptionHandler.handle() instead of duplicating error-formatting logic in controllers, services, or jobs.
  • Preserve the original error when passing it to handle(); avoid replacing it with a generic message before the handler can classify it.
  • Keep domain-specific validation and business errors distinguishable from unexpected infrastructure errors so the handler can apply the correct response policy.
  • Ensure new execution entry points, such as HTTP handlers, queue consumers, and CLI commands, integrate with this centralized error-handling path.

Relationships

  • IMPORTS → Logger

Was this page helpful?

Download as PDF
ExceptionHandler — NestJS head-to-head