How the error handling works
RequestHttpException
designed to manage errors in a structured and consistent manner across the application. When throwing exceptions, it’s mandatory to use RequestHttpException
instead of the built-in exception classes from NestJS. This practice ensures that error handling remains consistent and informative throughout your application.
Here’s how you should use RequestHttpException
:
RequestHttpException
className
: The name of the class where the exception is being thrown. This helps in identifying the source of the error.methodName
: The name of the method where the exception is being thrown. This provides a clear context on where the error occurred.message
: A brief description of the error, useful for logging and debugging purposes.readableMessagePath
: The path to the error message in the i18n translation files, which allows for localized error messages. Whenever a new readable error message is introduced, ensure it’s also added to the i18n translation files under i18n/<language_code>/error.json
.status
: The HTTP status code which will be sent in the response.RequestHttpException
, the HttpExceptionFilter
catches it as part of the global exception handling mechanism in NestJS. The filter does the following:
SentryService
injected into the HttpExceptionFilter
.