Performance: Catch decorator
MEDIUM IMPACT
This affects how errors are handled during request processing, impacting response time and user experience.
import { Catch, ExceptionFilter, ArgumentsHost } from '@nestjs/common'; @Catch() export class GlobalCatch implements ExceptionFilter { catch(exception: any, host: ArgumentsHost) { const ctx = host.switchToHttp(); const response = ctx.getResponse(); // offload heavy work asynchronously setImmediate(() => { // logging or cleanup }); response.status(500).json({ message: 'Internal error' }); } }
import { Catch, ExceptionFilter, ArgumentsHost } from '@nestjs/common'; @Catch() export class GlobalCatch implements ExceptionFilter { catch(exception: any, host: ArgumentsHost) { // heavy synchronous processing or logging for (let i = 0; i < 1000000; i++) { /* expensive loop */ } const ctx = host.switchToHttp(); const response = ctx.getResponse(); response.status(500).json({ message: 'Internal error' }); } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous heavy work in Catch decorator | Minimal | 0 | Blocks main thread delaying paint | [X] Bad |
| Asynchronous error handling with deferred work | Minimal | 0 | Non-blocking, smooth paint and interaction | [OK] Good |