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

How do getResponse() and getStatus() work on an HttpException and what can getResponse() return?

Difficulty: 5/10
HttpException, error handling, response payload

getStatus() always returns a number — the HTTP status code. getResponse() returns either a string or an object depending on how the exception was constructed. When thrown with a string message it returns that string. When thrown with an object it returns that object. When thrown with no arguments it returns a default object with statusCode, message, and error fields.

getResponse() return shapes
Important rules when using getResponse():
  1. 1

    Always guard with typeof res === 'string' before accessing .message — both shapes are valid.

  2. 2

    getStatus() is always a number — safe to use directly without type checking.

  3. 3

    Omitting the typeof check is the most common bug in custom exception filters.

  4. 4

    When the body is an object, access nested fields like (body as any).message or use optional chaining.

  5. 5

    exception.message is always a string — a safe alternative to getResponse() for simple use cases.

Scenario Questions

0-2 years experience

  1. 1If you catch a NestJS HttpException in a controller and call getStatus() and getResponse(), what values would you expect for a 404 Not Found error thrown with a custom message?
  2. 2Suppose you have a service that throws new HttpException('Invalid data', HttpStatus.BAD_REQUEST). In a unit test, how would you assert that getResponse() returns the string you passed versus an object?

2-5 years experience

  1. 1During a recent feature, you noticed that some HttpExceptions are returning an object with 'statusCode' and 'message' fields, while others return just a string. What could cause this inconsistency, and how would you standardize the response shape across the app?
  2. 2Your API gateway logs the status from getStatus() but sometimes the body from getResponse() is undefined. Walk me through how you would debug why getResponse() is empty for certain exceptions.
  3. 3You need to map different HttpException subclasses to specific error DTOs before sending them to the client. How would you use getResponse() to decide which DTO to instantiate, and what edge cases would you guard against?

5-8 years experience

  1. 1Design a global exception filter that logs the status and response of every HttpException, but also masks sensitive fields that might appear in getResponse(). What strategy would you use to detect and strip those fields without breaking the original payload?
  2. 2In a high‑traffic microservice, you want to avoid the overhead of serializing large error objects returned by getResponse(). How would you modify the exception handling pipeline to limit the size of the response while preserving getStatus() semantics?
  3. 3Explain how you would integrate NestJS’s HttpException handling with an external monitoring system that expects a flat error record. How does getResponse() shape affect the transformation, and what fallback would you implement for unexpected response types?

8+ years experience

  1. 1Across several teams, different modules throw HttpExceptions with varying response formats (string, object, array). Propose an organization‑wide contract and migration plan to unify getResponse() outputs, considering backward compatibility and CI enforcement.
  2. 2Your company is moving from NestJS to a new framework but wants to keep existing error handling semantics. How would you abstract getStatus() and getResponse() behavior so that legacy code can be ported with minimal changes, and what risks do you anticipate?

Follow-up Questions

  • Can you give an example of a custom error object you’ve returned from getResponse()?
  • What would happen if you called getStatus() on a non‑HttpException error?
  • How does NestJS decide whether getResponse() returns a string versus an object?
Share

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