05 / 06

How do interceptors handle errors vs exception filters in NestJS — what is the division of responsibility?

Interceptors via catchError() remap exceptions between types before filters run — used to translate domain errors into HTTP exceptions. Exception filters shape the HTTP error response sent to the client. Interceptors fix the type of exception; filters fix the format of the response.

Interceptor remapping vs filter formatting
Clear division of responsibility:
  1. 1

    Interceptors (catchError) — translate exception types: EntityNotFoundError → NotFoundException.

  2. 2

    Exception filters — format the HTTP response: shape JSON body, set headers, log errors.

  3. 3

    Interceptors run before filters in the pipeline — remap first, then format.

  4. 4

    Use interceptors when the error type needs changing; use filters when only the response format needs changing.

  5. 5

    Always re-throw from interceptor catchError if the error is not handled — let the filter catch it.

Difficulty: 5/10
Topics: interceptors, exception filters, error handling

Scenario Questions

0-2 years experience
  1. 1

    If you need to log the execution time of a controller method and also want to return a custom error response when something throws, which would you use – an interceptor, an exception filter, or both? Explain how you'd set them up.

  2. 2

    Suppose a service throws a standard JavaScript Error inside a request pipeline. What will happen if you only have a global exception filter but no interceptor that catches errors? Describe the flow.

2-5 years experience
  1. 1

    You added an interceptor that transforms the response data, but after a recent change, some errors are being swallowed and the client receives a 200 with an empty body. Walk me through why that might happen and how you'd fix it using exception filters.

  2. 2

    During debugging, you notice that a thrown HttpException is being logged twice – once in an interceptor and again in an exception filter. How would you adjust the responsibilities to avoid duplicate handling?

5-8 years experience
  1. 1

    In a microservices gateway, you need to enforce request validation, log latency, and ensure consistent error formatting across dozens of services. How would you architect the use of interceptors and exception filters to balance performance and maintainability?

  2. 2

    Consider a scenario where an interceptor performs async operations and may reject a promise. How does NestJS propagate that error compared to an exception filter, and what are the implications for high‑throughput endpoints?

8+ years experience
  1. 1

    Your organization is migrating legacy NestJS modules that currently use custom error‑handling middleware to a unified strategy. How would you decide which error handling logic belongs in interceptors versus exception filters, and what guidelines would you set for future teams?

  2. 2

    When scaling to thousands of concurrent requests, you notice that global exception filters add noticeable latency. Propose a design that minimizes overhead while preserving clear separation of concerns between interceptors and exception filters.

Follow-up Questions

  • What happens if an interceptor itself throws an exception?
  • Can an exception filter be scoped to a single controller, and why would you do that?
  • How does NestJS decide which exception filter runs when multiple are applicable?