Cross-Cutting Concerns

Preview — 3 of 10 questions

A method-level @Catch(ConflictException) filter and a global @Catch() catch-all both exist. A ConflictException is thrown. Which handles it and why?

javascript
@Controller('orders')
export class OrdersController {
  @Post()
  @UseFilters(ConflictFilter) // method scope, specific -> wins over global @Catch()
  create(): never {
    throw new ConflictException('duplicate order');
  }
}
AThe global catch-all, because broader filters win
BThe method-level specific filter, because scope precedence (method > controller > global) and specificity favor the nearer, more specific filter
CBoth run, specific first
DNeither — Nest uses the default handler

What is the cleanest pattern to report all unhandled errors to Sentry while preserving Nest's default responses?

javascript
import { Catch, ArgumentsHost, HttpException } from '@nestjs/common';
import { BaseExceptionFilter } from '@nestjs/core';
import * as Sentry from '@sentry/node';

@Catch()
export class SentryFilter extends BaseExceptionFilter {
  catch(exception: unknown, host: ArgumentsHost): void {
    const status = exception instanceof HttpException ? exception.getStatus() : 500;
    if (status >= 500) Sentry.captureException(exception);
    super.catch(exception, host);
  }
}
AWrap every controller method in try/catch and call Sentry manually
BAdd Sentry to a pipe
CExtend BaseExceptionFilter, capture non-HTTP (or 5xx) exceptions to Sentry, then delegate to super.catch()
DLog errors only in middleware

You have pure domain exceptions (e.g., OrderAlreadyShippedError) that must not import @nestjs/common. How do you map them to HTTP responses?

javascript
import { Catch, ArgumentsHost, ExceptionFilter, ConflictException } from '@nestjs/common';
import { DomainError, OrderAlreadyShippedError } from '../domain/errors';

@Catch(DomainError)
export class DomainErrorFilter implements ExceptionFilter {
  catch(error: DomainError, host: ArgumentsHost): void {
    const http =
      error instanceof OrderAlreadyShippedError ? new ConflictException(error.message) : error;
    const res = host.switchToHttp().getResponse();
    const status = 'getStatus' in http ? (http as any).getStatus() : 500;
    res.status(status).json({ message: error.message });
  }
}
AMake every domain error extend HttpException
BReturn status codes directly from the domain layer
CKeep domain errors framework-free and add an exception filter that maps each domain error type to the appropriate HttpException/status
DCatch them in middleware

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.