Menu

Every error from my API looks the same too: a global exception filter in NestJS (with real code)
📰

Every error from my API looks the same too: a global exception filter in NestJS (with real code)

DEV Community: nestjs·Ziad Taha·6 days ago
#MnBUzuet
#devtonestjs#devto#dev#exception#error
Long article (663 words) — no AI summary yet.
Reading 0:00
15s threshold

I'm a junior full-stack developer from Alexandria, Egypt. 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 .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.