Questions
15 of 25
1What built-in HTTP exceptions does NestJS provide out of the box?
2What interface must an exception filter implement and what are the two parameters of catch()?
3How do you apply an exception filter at the method, controller, and global level in NestJS?
4How do you add custom response headers or set cookies inside an exception filter in NestJS?
5How do you unit test an exception filter in NestJS?
6How do you implement a filter that adds a request correlation ID to every error response in NestJS?
7How do getResponse() and getStatus() work on an HttpException and what can getResponse() return?
8How do you create a domain-specific custom exception class in NestJS?
9How does exception filter resolution order work when multiple filters are registered at different levels in NestJS?
10How do you register multiple global filters that each handle a different exception type in NestJS?
11What does the @Catch() decorator do and what happens when you omit its argument?
12How do you extend the built-in BaseExceptionFilter to add logging while keeping default NestJS behaviour?
13How do you handle exceptions differently in a filter depending on whether the request is HTTP, WebSocket, or RPC?
14What are the two ways to register a global exception filter in NestJS and what is the key difference?
15How do interceptors and exception filters divide error-handling responsibility in NestJS?
16How do you write an exception filter that handles GraphQL errors differently from REST errors in NestJS?
17How do you prevent sensitive information from leaking through exception responses in production NestJS apps?
18How do you handle async operations inside an exception filter in NestJS, for example logging to a database?
19What is HttpException in NestJS and what two arguments does its constructor take?
20What does NestJS's built-in global exception layer do when no custom filter is registered?
21What is the cause option on HttpException and how should you use it for error tracing?
22How do you write a production-ready global all-exceptions filter that handles HttpException and unexpected errors differently?
23How do you integrate a third-party error tracker like Sentry inside a global exception filter in NestJS?
24How do you map database and ORM exceptions to HTTP exceptions using a global filter in NestJS?
25How do you re-throw an exception from within a filter to let a higher-scoped filter handle it in NestJS?
15 / 25

How do interceptors and exception filters divide error-handling responsibility in NestJS?

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

Interceptors via catchError() remap exception types — they convert domain or ORM errors into HttpException subclasses before the filter sees them. Exception filters shape the HTTP error response sent to the client. Interceptors fix what the exception is; filters fix how the error response looks. Interceptors run before filters in the pipeline.

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

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

  2. 2

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

  3. 3

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

  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.

Scenario Questions

0-2 years experience

  1. 1You have a NestJS controller that throws a NotFoundException. If you add a global exception filter but also have a logging interceptor, which one will see the error first and why?
  2. 2How would you use an interceptor to transform a successful response while still letting an exception filter handle errors for the same route?
  3. 3If you forget to bind an exception filter to a route, what happens when an error is thrown inside an interceptor?

2-5 years experience

  1. 1We noticed validation errors are being logged twice, once in our interceptor and again in the exception filter. Walk me through why that is happening and how you'd fix it.
  2. 2During a recent deployment, a custom HttpException wasn't being caught by our global exception filter when it originated from a middleware. Explain the chain of responsibility and what changes you'd make.
  3. 3Our team wants to add a retry interceptor for external API calls but still have a central exception filter for all uncaught errors. How would you structure them to avoid conflicts?

5-8 years experience

  1. 1At scale, we have many microservices each with their own interceptors and a shared exception filter library. Discuss the trade‑offs of handling errors in interceptors vs. filters, especially regarding performance and consistency.
  2. 2We need to implement a feature where certain errors should bypass the global exception filter and be returned as raw responses. How would you design the interceptor/filter interaction to support this without breaking existing error handling?
  3. 3Explain how you would test that interceptors and exception filters correctly partition error handling across a complex request pipeline.

8+ years experience

  1. 1Our organization is migrating legacy Express middleware into NestJS modules. The legacy code relies on throwing errors that are caught by a top‑level error handler. How would you redesign the error‑handling strategy using interceptors and exception filters to maintain backward compatibility while adopting NestJS best practices?
  2. 2Across multiple teams, some put business‑logic error handling in interceptors, others in exception filters. Propose a governance model and architectural guidelines to standardize responsibility division.
  3. 3If we plan to expose a GraphQL API alongside REST, how would you align interceptor and exception filter usage to provide a unified error format without duplicating logic?

Follow-up Questions

  • Can you give an example of when you'd handle an error in an interceptor instead of a filter?
  • How does the order of global versus scoped interceptors affect error propagation?
  • What considerations does this division introduce for unit testing?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.