Performance: Global exception filters
MEDIUM IMPACT
Global exception filters affect the response time and error handling flow, impacting interaction responsiveness and server response speed.
import { Catch, ExceptionFilter, ArgumentsHost } from '@nestjs/common'; @Catch() export class GlobalFilter implements ExceptionFilter { async catch(exception: any, host: ArgumentsHost) { const ctx = host.switchToHttp(); const response = ctx.getResponse(); const status = exception.status || 500; // Use async non-blocking logging await asyncLogging(exception); response.status(status).json({ message: 'Error occurred' }); } }
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 or blocking operation here heavySyncLogging(exception); response.status(status).json({ message: 'Error occurred' }); } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous blocking logging in global filter | 0 (server-side) | 0 (server-side) | 0 (server-side) | [X] Bad |
| Asynchronous non-blocking logging in global filter | 0 (server-side) | 0 (server-side) | 0 (server-side) | [OK] Good |