Skip to main content

Getting Started


Welcome to the Exceptions Library, designed to streamline exception handling in your applications, aligning with RFC7807 standards.

Why Use the Exceptions Library?

  • Unified Exception Handling: Consistency across server and client-side.
  • Enhanced Swagger Documentation: Document exceptions effectively.
  • Clear Error Distinction: Separate genuine exceptions from business logic errors.
  • Improved Logging: Efficient tracking of exceptions.

Versatility

This library is versatile enough for use in various contexts, not limited to Softkit projects.

Installation

Setting up the Exceptions Library is easy:

yarn add @softkit/exceptions

With Bootstrap Library

Integrate seamlessly with @softkit/bootstrap for default interceptors and filters.

Language Support

Works with @softkit/i18n for customizable messages (default in English).

Usage

Default Interceptors

  • AnyExceptionFilter: Handles any exceptions with a 500 status.
  • HttpExceptionFilter: Manages AbstractHttpException instances, returning appropriate HTTP statuses.
  • ForbiddenExceptionFilter: Catches NestJS ForbiddenException, returns a 403 status in RFC format.
  • NotFoundExceptionFilter: Deals with NestJS NotFoundException, ensuring a 404 status in RFC format.

Custom Exception Creation

Create your exceptions for tailored error handling:

import { AbstractHttpException } from '@softkit/exceptions';

export class YourException extends AbstractHttpException {
constructor(rootCause?: unknown) {
super(
i18nString('exception.YOUR_EXCEPTION.TITLE'),
i18nString('exception.YOUR_EXCEPTION.GENERAL_DETAIL'),
HttpStatus.FORBIDDEN,
undefined,
rootCause,
);
}
}

Localization Override

Customize default titles and details using a local exception.json file, integrated with @softkit/i18n for automatic substitution.