@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 Code | HTTP Status | Error Code | Message |
|---|---|---|---|
| P2002 | 409 Conflict | DUPLICATE_RESOURCE | Resource already exists |
| P2025 | 404 Not Found | NOT_FOUND | Resource not found |
| P2003 | 400 Bad Request | CONSTRAINT_VIOLATION | Invalid reference |
| P2014 | 400 Bad Request | CONSTRAINT_VIOLATION | Required relation missing |
| Other | 500 Server Error | DATABASE_ERROR | Database operation failed |
License
Proprietary - CleverChain Limited