@plumeframework/error-handler
v1.0.2
Published
Plume framework error handler.
Downloads
2
Maintainers
Readme
Plume Error Handler
This error handler for Express was initially designed with the Plume framework in mind. However, it can be seamlessly integrated into any Express projects.
This is a simple Express error handler built according to the Express documentation (see Express error handler).
Installation
To install the package, use the following command:
npm install @plume-framework/error-handler
Make sure you have the required dependencies installed as well.
Usage
const { ErrorHandler } = require('@plumeframework/error-handler');
// Create an instance of the error handler
const errorHandler = new ErrorHandler({ log: true });
// Use the error handler middleware in your Express app
app.use(errorHandler.handleError);
// Customize the options as needed
Configuration
The package supports customization through configuration options. When creating an instance of the ErrorHandler class, you can provide an options object with the following properties:
log
(boolean, default: true): Controls whether errors are logged to the console and error stack returned with the response. Set it to true to enable logging, or false to disable it.
Here's an example of how to customize the options:
const errorHandler = new ErrorHandler({ log: false });
app.use(errorHandler.handleError);
Examples
// Example 1: Handling errors in an Express route
app.get('/example', (req, res, next) => {
try {
// Some code that may throw an error
} catch (error) {
next(error);
}
});
// Example 2: Customizing the error handler options
const errorHandler = new ErrorHandler({ log: false });
app.use(errorHandler.handleError);
Handled errors will be returned as an Express Response with the following shape:
type HttpErrorResponse = {
message: string;
status: number;
stack?: string;
};
Changelog
See CHANGELOG.md
License
This project is licensed under the CC-BY-NC-4.0 license. See the CC-BY-NC-4.0 file for more details.