@mitmaro/errors
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

Node Errors

Dependency Status Build Status Coverage Status NPM version GitHub license Known Vulnerabilities

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 git@github.com: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.

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 2.0.0
    0
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 2.0.0
    0
  • 1.0.0
    2,531
  • 0.0.1
    0

Package Sidebar

Install

npm i @mitmaro/errors

Weekly Downloads

2,531

Version

2.0.0

License

ISC

Unpacked Size

15.2 kB

Total Files

16

Last publish

Collaborators

  • mitmaro