Skip to main content

@helix/shared/filters

Shared exception filters for consistent error handling.

Overview

Provides two exception filters:

  • HttpExceptionFilter - Formats HTTP exceptions consistently
  • PrismaExceptionFilter - Maps Prisma database errors to HTTP responses

Both filters use CLS to include request context in error logs.

Installation

import { HttpExceptionFilter, PrismaExceptionFilter } from '@helix/shared/filters';

Usage

Global Filters

// In main.ts
const app = await NestFactory.create(AppModule);
const cls = app.get(ClsService);

app.useGlobalFilters(
new HttpExceptionFilter(cls),
new PrismaExceptionFilter(cls),
);

Error Response Format

All errors return this structure:

{
"success": false,
"statusCode": 404,
"timestamp": "2025-01-20T10:30:00.000Z",
"path": "/api/users/123",
"error": {
"message": "Resource not found",
"code": "DB_002",
"details": {}
},
"requestId": "550e8400-e29b-41d4-a716-446655440000"
}

Prisma Error Mapping

Prisma CodeHTTP StatusError CodeMessage
P2002409 ConflictDUPLICATE_RESOURCEResource already exists
P2025404 Not FoundNOT_FOUNDResource not found
P2003400 Bad RequestCONSTRAINT_VIOLATIONInvalid reference
P2014400 Bad RequestCONSTRAINT_VIOLATIONRequired relation missing
Other500 Server ErrorDATABASE_ERRORDatabase operation failed

License

Proprietary - CleverChain Limited