1.0.2 • Published 5 years ago

@tobes/application-error v1.0.2

Weekly downloads
1
License
BSD-3-Clause
Repository
github
Last release
5 years ago

ApplicationError

Super simple, bare bones extension of Error to support error codes.

Purpose

When being consumed by a client, service errors that depend on message strings only are very hard to make decisions in code with.

Making those decisions based on Error subclass names puts you in the situation where you are transcribing class names to the client and being pulled in two directions when you name the classes.

Extending Error with ApplicationError provides errors with codes for easy consumption in code and messages for human/programmer logging/debuging/comprehension.

Usage

const { ApplicationError } = require('@tobes/application-error');

// code is required
const appError = new ApplicationError('CODE');

// appError.appcode === 'CODE'

// message and the other Error class arguments are optional
const appError = new ApplicationError('CODE', 'Human message');

// appError.appcode === 'CODE'
// appError.message === 'Human message'

ThrowsCode

A chai plugin to add throwCode to chai expect/should statements

Purpose

Allow testing of ApplicationError throwing code to be precise and test for specific errors without adding all the fragility involved in testing for specific human readable error messages or having to subclass everything.

Usage

const { ThrowCode, ApplicationError } = require('@tobes/application-error');
const chai = require('chai');
const expect = chai.expect;
chai.use( ThrowCode );

describe('.some function', () => {
    it('some test case', () => {
        const throwingFunction = () => { throw new ApplicationError('ERROR_CODE') };
        expect(throwingFunction).to.throwCode(ApplicationError, 'ERROR_CODE');
    });
};

Tests

$ git clone git@github.com:tobespangler/application-error.git
$ cd application-error
$ npm install
$ npm test

 ApplicationError
    #constuctor
      ✓ Error code is set
      ✓ Error code is mandatory
      ✓ Error message is set
      ✓ Error message is not mandatory
      ✓ Parent class is Error
    .toString
      ✓ Contains error name, code and message
      ✓ Contains error name and message and code when code is empty string
      ✓ Contains error name message and code when code is null
      ✓ Contains error name and code when message is empty string
      ✓ Contains error name and code when message is not provided
      ✓ Contains error name message and code when message is null
      ✓ Contains error name and code when message is undefined

  ThrowCode
    ✓ expected code and error received
    ✓ expected code and compatible error received
    ✓ expected code not received
    ✓ expected error class not received
    ✓ expected error class overrides wrong code if both are wrong
    ✓ no error thrown
    ✓ correct error thrown but error class does not have codes
    ✓ negate not allowed


  20 passing (19ms)

----------------------|----------|----------|----------|----------|-------------------|
File                  |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------------------|----------|----------|----------|----------|-------------------|
All files             |      100 |      100 |      100 |      100 |                   |
 application_error.js |      100 |      100 |      100 |      100 |                   |
 throw_code.js        |      100 |      100 |      100 |      100 |                   |
----------------------|----------|----------|----------|----------|-------------------|
1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago