ErrorHandler
This project provides a customizable error handling library for Node.js applications. ErrorHandler sends alerts using Opsgenie, logs error messages to a file, and can be easily integrated into other projects.
Usage
To use the ErrorHandler in your project, first install it as a dependency:
Add the following values do your .env
file
OPSGENIE_GENERIC_CHANNEL_API_KEY=your_api_key
PRODUCTION_CRITICAL_SLACK_ALERT_CHANNEL_ID=PRODUCTION_CRITICAL_SLACK_ALERT_CHANNEL_ID
LOG_FILE_PATH=/path/to/error.log
npm install --save souq-error-handler
Then, import and use the ErrorHandler in your project:
const ErrorHandler = require('errorHandler');
// Initialize ErrorHandler instance
const errorHandler = new ErrorHandler(
process.env.OPSGENIE_GENERIC_CHANNEL_API_KEY || '',
process.env.LOG_FILE_PATH || 'error.log'
);
// Log error to file
errorHandler.logError('Logging an example error message into the error.log');
// Send error to team devices with specified severity
errorHandler.slackAlert('Logging an example critical error message into the slack', 'critical').catch((err) => {
console.error('A critical slack alert:', err);
});
errorHandler.slackAlert('Logging an example moderate error message into the slack', 'moderate').catch((err) => {
console.error('A moderate slack alert:', err);
});
// Send error to team devices and send generic alert
errorHandler.genericAlert('Logging an example error directly to team devices').catch((err) => {
console.error('A generic alert:', err);
});
Running Tests
To run tests, execute the following command:
npm test
Linting
This project uses ESLint for linting. To fix linting issues, run:
npm run lint --fix
Further Development
- Clone this repository:
git clone https://github.com/yourusername/errorHandler.git
- Install dependencies:
npm install
- Copy the .env.example file to create a new .env file:
cp .env.example .env
-
Update the .env file with your API keys, recipients, and other configuration values.
-
(optional) Run the example usage script to see what happens:
node src/example_usage.js
Folder Structure
|-- .env
|-- .env.example
|-- .eslintignore
|-- .eslintrc.json
|-- .gitignore
|-- README.md
|-- index.js
|-- package-lock.json
|-- package.json
|-- src
| |-- errorHandler.js
| |-- example_usage.js
|-- tests
|-- errorHandler.test.js