Node.js / NestJS

Error Handling

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

20 面接問題·
Junior
1

What is an exception filter in NestJS?

回答

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?

回答

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?

回答

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 面接問題

次の面接に向けてNode.js / NestJSをマスター

すべての問題、flashcards、技術テスト、コードレビュー演習、面接シミュレーターにアクセス。

無料で始める