npm install --save @mitmaro/errors
const {ErrorHandler} = require('@mitmaro/errors');
const errorHandler = new ErrorHandler((msg) => process.stderr.write(msg));
import {ErrorHandler} from '@mitmaro/errors';
const myLogger = async (msg: string = ''): Promise<void> => {process.stderr.write(msg)};
const errorHandler = new ErrorHandler(myLogger);
errorHandler.register((logger, err) => {
if (err instanceof MyError) {
logger('My Error Occurred');
logger(err.message);
}
});
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);
try {
throw new Error('My Error');
}
catch (err) {
errorHandler.handle(err);
}
try {
throw new Error('My Error');
}
catch (err) {
errorHandler.handle(err);
}
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.
const {RuntimeError} = require('@mitmaro/errors');
class MyError extends RuntimeError {
constructor(message, cause) {
super(message, 'MyError', cause);
}
}
import {RuntimeError} from '@mitmaro/errors';
class MyError extends RuntimeError {
public constructor(message: string, cause?: error) {
super(message, 'MyError', cause);
}
}
Development is done using Node 8 and NPM 5, and tested against both Node 6, Node 8 and Node 10. To get started:
git clone git@github.com:MitMaro/node-errors.git
cd node-errors
npm install
npm run test
This project is released under the ISC license. See LICENSE.
A logger to log the error information
Type guard for error with a cause property
Generated using TypeDoc
An error handler that can handle a number of error types
the logger instance
the error instance
true if the error was handled, otherwise false