Node.js / NestJS

Error Handling

Exception filters, custom exceptions, HTTP exceptions, error responses, logging

20 domande da colloquioยท
Junior
1

What is an exception filter in NestJS?

Risposta

An exception filter is a NestJS component that intercepts exceptions thrown in the application and transforms them into appropriate HTTP responses. It centralizes error handling and avoids code duplication by applying common logic to all exceptions. Filters can be global, applied to a specific controller, or to a particular route depending on the needs.

2

Which NestJS class should be used to return an HTTP 404 error?

Risposta

NotFoundException is the standard NestJS class for returning a 404 (Not Found) error. It inherits from HttpException and automatically configures the appropriate HTTP status code. This exception is ideal when a requested resource does not exist in the database. NestJS provides dedicated classes for each common HTTP error type to simplify development.

3

How to create a custom HttpException in NestJS?

Risposta

To create a custom exception, extend the HttpException class by calling super with the error message and HTTP status code. This approach allows defining business-specific exceptions for the application while respecting NestJS conventions. Custom exceptions facilitate maintenance and make code more expressive by giving meaningful names to different error situations.

4

Where to place the @Catch decorator for a global exception filter?

5

Which method must be implemented in a custom exception filter?

+17 domande da colloquio

Padroneggia Node.js / NestJS per il tuo prossimo colloquio

Accedi a tutte le domande, flashcards, test tecnici, esercizi di code review e simulatori di colloquio.

Inizia gratis