Options
All
  • Public
  • Public/Protected
  • All
Menu

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.

Index

Type aliases

Handler

Handler: function

An error handler that can handle a number of error types

param

the logger instance

param

the error instance

returns

true if the error was handled, otherwise false

Type declaration

    • <T>(logger: Logger, error: T): Promise<boolean>
    • Type parameters

      • T: Error

      Parameters

      Returns Promise<boolean>

Logger

Logger: function

A logger to log the error information

param

A string to be logged

Type declaration

    • (message?: undefined | string): Promise<void>
    • Parameters

      • Optional message: undefined | string

      Returns Promise<void>

Variables

Private Const unhandledErrorHandler

unhandledErrorHandler: function = unhandledErrorHandlerFactory()

Type declaration

    • <T>(logger: Logger, error: T): Promise<boolean>
    • Type parameters

      • T: Error

      Parameters

      Returns Promise<boolean>

Functions

Private isErrorWithCause

  • Type guard for error with a cause property

    Parameters

    Returns boolean

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc