Global Error Handling
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
Global Error Handling in the jahid19978 project is designed to provide a centralized, consistent mechanism for catching and processing exceptions across the entire application. The primary goal is to ensure that the system remains stable when unexpected errors occur, while providing meaningful feedback to clients and detailed logs for developers.
The architecture separates operational errors (predictable problems like invalid input) from programming errors (bugs like null pointer exceptions). This distinction allows the application to respond gracefully to users without leaking sensitive internal details.
Sources: src/app.js, src/utils/AppError.js
Error Handling Architecture
The system utilizes a centralized middleware approach to intercept all errors propagated through the Express.js routing stack. When an error occurs in a controller or service, it is passed to the next middleware via the next(err) function, which triggers the global error controller.
The AppError Class
The AppError class serves as the foundation for operational errors. It extends the native JavaScript Error class and adds properties for HTTP status codes and operational status flags.
| Property | Type | Description |
|---|---|---|
statusCode | Number | The HTTP status code (e.g., 400, 404, 500). |
status | String | 'fail' for 4xx errors or 'error' for 5xx errors. |
isOperational | Boolean | Flag indicating if the error is a known operational error. |
Sources: src/utils/AppError.js:1-15
Centralized Middleware Flow
The flow of an error through the system is managed by the error controller, which differentiates between development and production environments to determine the level of detail included in the response.
The diagram shows how errors are intercepted and branched based on the environment and the nature of the error.
Sources: src/controllers/errorController.js:10-50, src/app.js:25
Implementation Details
Global Error Controller
The errorController.js handles the formatting of the JSON response sent to the client. It captures the stack trace and provides comprehensive debugging information when the application is running in a non-production state.
module.exports = (err, req, res, next) => {
err.statusCode = err.statusCode || 500;
err.status = err.status || 'error';
if (process.env.NODE_ENV === 'development') {
res.status(err.statusCode).json({
status: err.status,
error: err,
message: err.message,
stack: err.stack
});
}
};
Sources: src/controllers/errorController.js:1-15
Error Integration in App Configuration
The error handling middleware is registered at the very end of the app middleware stack in app.js. This ensures that any errors occurring in routes defined earlier are correctly caught.
const globalErrorHandler = require('./controllers/errorController');
// ... other middlewares and routes
app.use(globalErrorHandler);
Sources: src/app.js:30-35
Operational vs. Programming Errors
The system distinguishes between two main types of errors to ensure security and reliability:
- Operational Errors: These are problems that the application is expected to encounter, such as "user not found" or "validation failed". These are handled using the
AppErrorclass and return specific status codes. - Programming Errors: These are unexpected bugs (e.g., calling a method on an undefined object). In production, these are treated as generic 500 errors to prevent exposing implementation details.
Sources: src/utils/AppError.js, src/controllers/errorController.js:40-60
Summary
The Global Error Handling system in jahid19978 centralizes exception management through a dedicated AppError utility and an Express middleware controller. By differentiating between operational and programming errors, the project maintains a high level of security while providing developers with the necessary tools to debug issues effectively in development environments.