Questions
21 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?
21 / 25

What is the cause option on HttpException and how should you use it for error tracing?

Difficulty: 5/10
HttpException, cause option, error tracing

The options.cause property attaches an inner Error to the HttpException for server-side observability. It is never serialized into the HTTP response body. In your global filter you can read exception.cause and log the full stack trace while sending only a safe public message to the client.

Using cause for safe error tracing
How to use cause correctly:
  1. 1

    cause is purely for server-side observability — it is not included in the HTTP response body by default.

  2. 2

    Attach the original database, SDK, or third-party error as cause when wrapping it in an HttpException.

  3. 3

    Read exception.cause inside your global filter to log the full original stack trace.

  4. 4

    This pattern separates what you log (everything) from what you expose to clients (safe messages only).

  5. 5

    exception.cause is available as a public property on any HttpException instance.

Scenario Questions

0-2 years experience

  1. 1If you throw new HttpException('Invalid input', HttpStatus.BAD_REQUEST, { cause: new ValidationError() }), what will NestJS include in the response and how can you later retrieve the original ValidationError?
  2. 2You need to log the stack trace of an underlying error when returning a 500 error from a controller. How would you use the cause option on HttpException to achieve that?
  3. 3What happens if you omit the cause option when wrapping another error in HttpException? Will the original error be lost for logging?

2-5 years experience

  1. 1During a recent feature you wrapped a database error inside an HttpException using the cause option, but the logs only show the HttpException message. What could be misconfigured in your NestJS setup that prevents the cause from being logged?
  2. 2You have a global exception filter that formats error responses. How would you modify it to include information from the cause property of HttpException without exposing sensitive details?
  3. 3Explain why using the cause option is preferable to concatenating error messages when you need to trace errors across async service calls.

5-8 years experience

  1. 1In a high‑traffic microservice, you need to propagate error context across several layers (service, repository, external API). How would you design the error handling strategy using HttpException's cause to maintain traceability while keeping performance overhead low?
  2. 2Your team is migrating from custom error classes to NestJS HttpException with cause. What edge cases (e.g., circular causes, serialization) must you watch for and how would you mitigate them?
  3. 3When integrating with a logging platform that expects a flat error object, how would you extract and flatten the nested cause chain from HttpException without losing stack information?

8+ years experience

  1. 1Across multiple NestJS services, you want a unified error‑tracking convention that leverages HttpException's cause. How would you define a shared library or pattern that enforces consistent use, supports correlation IDs, and integrates with centralized observability tools?
  2. 2Consider a legacy monolith where errors are thrown as plain strings. What migration path would you propose to adopt HttpException with cause, ensuring backward compatibility and minimal disruption to existing error handling middleware?
  3. 3At scale, storing full error cause chains can increase log volume. How would you design a policy for truncating or sampling cause information while still enabling root‑cause analysis for critical incidents?

Follow-up Questions

  • How would you write a unit test to verify the cause is attached correctly?
  • What would you do if the cause itself is another HttpException?
  • Are there any risks when serializing the cause for an HTTP response?
Share

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