Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@mitmaro/errors

MitMaro23.3kISC2.0.0TypeScript support: included

A Node error system

errors, handling, logging

readme

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.

changelog

Change Log

All notable changes to this project will be documented in this file. This project adheres to Semantic Versioning. The format conforms to Keep a Changelog.

2.0.0 - 2018-07-28

Changed

  • Rewritten using TypeScript

1.0.0 - 2018-01-28

Added

  • Initial project release