Performance: Exception filters
MEDIUM IMPACT
Exception filters affect the server response time and error handling flow, impacting how quickly errors are processed and sent to the client.
import { Catch, ExceptionFilter, ArgumentsHost, Logger } from '@nestjs/common'; @Catch() export class GlobalFilter implements ExceptionFilter { private readonly logger = new Logger(GlobalFilter.name); catch(exception: any, host: ArgumentsHost) { const ctx = host.switchToHttp(); const response = ctx.getResponse(); const status = exception.status || 500; // Asynchronous logging without blocking setImmediate(() => this.logger.error(exception.message)); response.status(status).json({ message: exception.message }); } }
import { Catch, ExceptionFilter, ArgumentsHost } from '@nestjs/common'; @Catch() export class GlobalFilter implements ExceptionFilter { catch(exception: any, host: ArgumentsHost) { const ctx = host.switchToHttp(); const response = ctx.getResponse(); const status = exception.status || 500; // Synchronous heavy logging for (let i = 0; i < 1000000; i++) { /* heavy loop */ } response.status(status).json({ message: exception.message }); } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous heavy processing in filter | 0 (server-side) | 0 (server-side) | 0 (server-side) | [X] Bad |
| Asynchronous logging in filter | 0 (server-side) | 0 (server-side) | 0 (server-side) | [OK] Good |