ErrorHandler is a customizable error management package designed to enhance error handling in your applications. It allows you to log errors, send errors to external services, and define custom error-handling behavior.
- Error Logging: Automatically logs errors to the console and an internal log.
- Error Reporting: Optionally sends errors to an external service.
- Custom Error Handling: Allows you to define custom behavior for handling errors.
- Error History: Maintains a log of all errors encountered.
No installation is required for this code as it is self-contained and can be added directly to your project.
- Create an ErrorHandler Instance
const errorHandler = new ErrorHandler({ logErrors: true, // Enable or disable error logging sendErrors: true, // Enable or disable error reporting to an external service customHandler: (error) => { console.error('Custom Error Handler:', error.message); // Add custom error handling logic here }, });
- Handling Errors To handle an error, simply call the handleError method:
try {
throw new Error('Something went wrong!');
} catch (error) {
errorHandler.handleError(error);
}
- Accessing the Error Log To access the error log:
console.log('Error Log:', errorHandler.getErrorLog());