I'm a full-stack developer (MEAN stack), studying Computer Science at EELU in Alexandria. In my last post I showed the interceptor that makes every successful FaceMark response one shape. This is the other half: the global exception filter that makes every failed response one shape too, and decides what clients get to learn from a failure.
FaceMark is my face-recognition attendance project (NestJS API, Python AI service, MySQL). Here's the filter, trimmed:
// src/common/filters/http-exception.filter.ts
@Catch()
export class HttpExceptionFilter implements ExceptionFilter {
catch(exception: any, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const request = ctx.getRequest<Request>();
let status = exception instanceof HttpException
? exception.getStatus()
: HttpStatus.INTERNAL_SERVER_ERROR;
// TypeORM database errors get their own treatment
if (exception instanceof QueryFailedError) {
status = HttpStatus.BAD_REQUEST;
message = {
message: 'Database operation failed: An integrity constraint or data format issue occurred.',
hint: 'Check for duplicate entries or missing required fields.',
code: (exception as any).code,
statusCode: status,
};
}
console.error(`[Error] ${request.method} ${request.url}`, exception);
const isDevelopment = process.env.NODE_ENV === 'development';
response.status(status).json({
success: false,
statusCode: status,
timestamp: new Date().toISOString(),
path: request.url,
error: typeof message === 'string' ? { message } : message,
stack: isDevelopment ? (exception instanceof Error ? exception.stack : null) : undefined,
});
}
}
The decisions inside it
Three error families, three treatments. Known HTTP exceptions (my own throw new ForbiddenException(...)) keep their status and message, because those messages were written for clients. TypeORM's QueryFailedError (duplicate emails, bad foreign keys) becomes a 400 with a safe hint instead of the raw SQL error. Everything unknown becomes a 500 with "Internal server error" and nothing else.
The client never sees a stack trace in production. The isDevelopment check means I get full stacks while building, and clients get a clean shape in production. Stack traces tell attackers which framework, ORM and file layout you run. This one ternary is the cheapest security win in the file.
The log and the response are different audiences. console.error gets the full exception for me; the response gets the shaped version for the client. Beginner-me wrote these as the same thing. They are not.
How it pairs with the interceptor
The transform interceptor wraps successes as { success: true, data, timestamp }. This filter wraps failures as { success: false, statusCode, timestamp, path, error }. Between the two, the frontend has exactly two response shapes to handle, forever. When I add endpoint number thirty, it changes nothing on the client.
What I'd tell another developer
Write the error shape before you need it. The first time a duplicate-email signup returns raw SQL to the browser, you'll wish this file existed.
Classify, don't just catch. @Catch() with no argument catches everything; the value is in the instanceof checks that give each family the right status and a safe message.
Your errors are a data leak or a feature. A 403 that says "your role 'student' can't access this, required: [admin, staff]" (my roles guard does exactly this) tells the legitimate caller what to ask for. A raw 500 with a stack trace tells an attacker what to attack.
Everything above is from the real repo.
I'm a full-stack developer (Angular, NestJS, TypeScript, Python) in Alexandria, Egypt, open to freelance work and new roles. More posts from this project:
- Circuit breaker + WAL engine: https://dev.to/ziadtaha/i-wrote-a-circuit-breaker-and-a-bitcask-style-wal-engine-for-my-face-recognition-api-with-real-1gcp
The NestJS-to-Python boundary: https://dev.to/ziadtaha/my-nestjs-api-talks-to-a-python-ai-microservice-heres-how-i-keep-the-boundary-honest-with-real-160g
GitHub: https://github.com/ZiadtahaM
Email: ziad.taha3456@gmail.com
dev.to: https://dev.to/ziadtaha
Top comments (0)