Error Classes
Express Error Handling Documentation
This module defines custom error classes extending the native JavaScript Error class, specifically tailored for HTTP responses in an Express application. These classes include:
-
ExpressError:
- Purpose: Base class for all custom errors, allowing to specify an error message and a HTTP status code.
- How: Inherits from
Error, adds astatusproperty for the HTTP status code.
-
NotFoundError (404):
- Use: Indicates a resource is not found.
- Default Message: “Not Found”.
-
UnauthorizedError (401):
- Use: Indicates a request lacks valid authentication credentials.
- Default Message: “Unauthorized”.
-
BadRequestError (400):
- Use: Signifies a client-side error in the request.
- Default Message: “Bad Request”.
-
ForbiddenError (403):
- Use: Used when authentication is successful but the user doesn’t have permission for the requested operation.
- Default Message: “Forbidden Request”.
These classes enable consistent and clear error handling in the application, ensuring that appropriate HTTP status codes and messages are returned in response to various error conditions. This enhances the robustness and maintainability of the application’s error handling mechanism.