@mitmaro/errors
v2.0.0
Published
A Node error system
Downloads
13,719
Readme
Node Errors
Install
npm install --save @mitmaro/errors
Documentation
Usage
Creating in instance
JavaScript
const {ErrorHandler} = require('@mitmaro/errors');
const errorHandler = new ErrorHandler((msg) => process.stderr.write(msg));
TypeScript
import {ErrorHandler} from '@mitmaro/errors';
const myLogger = async (msg: string = ''): Promise<void> => {process.stderr.write(msg)};
const errorHandler = new ErrorHandler(myLogger);
Registering a handler function
JavaScript
errorHandler.register((logger, err) => {
if (err instanceof MyError) {
logger('My Error Occurred');
logger(err.message);
}
});
TypeScript
const myErrorHandler = async <MyError>(logger: Logger, err: MyError) => {
if (err instanceof MyError) {
logger('My Error Occurred\n');
logger(err.message);
return true;
}
return false;
};
errorHandler.register(myErrorHandler);
Handling errors
JavaScript
try {
throw new Error('My Error');
}
catch (err) {
errorHandler.handle(err);
}
TypeScript
try {
throw new Error('My Error');
}
catch (err) {
errorHandler.handle(err);
}
Custom errors
This library exports two error that are meant to be extended when creating custom errors. They are RuntimeError
this
is meant for non-recoverable errors that may occur during the running of an application. The other is a BaseError
that
is meant for all other errors. Both errors take a optional cause
argument that allows for an error chain. The error
handler handles logging of errors that have a cause.
JavaScript
const {RuntimeError} = require('@mitmaro/errors');
class MyError extends RuntimeError {
constructor(message, cause) {
super(message, 'MyError', cause);
}
}
TypeScript
import {RuntimeError} from '@mitmaro/errors';
class MyError extends RuntimeError {
public constructor(message: string, cause?: error) {
super(message, 'MyError', cause);
}
}
Development
Development is done using Node 8 and NPM 5, and tested against both Node 6, Node 8 and Node 10. To get started:
- Install Node 8 from NodeJS.org or using nvm
- Clone the repository using
git clone [email protected]:MitMaro/node-errors.git
cd node-errors
- Install the dependencies
npm install
- Make changes, add tests, etc.
- Run linting and test suite using
npm run test
License
This project is released under the ISC license. See LICENSE.