node-error-classes

Custom error classes for node.js

Usage no npm install needed!

<script type="module">
  import nodeErrorClasses from 'https://cdn.skypack.dev/node-error-classes';
</script>

README

Errors : object

Custom Node and Browser Error Classes

npm install node-error-classes

Easy to read isomorphic, custom and extensible error classes for node.js and the browser that extend the native error classes.

This will work in the browser too with webpack, browserify, or any other common.js module loader!

Sometimes, just throwing an error isn't that helpful. Sometimes, handling an error elegantly preventing a crash just isn't quite good enough. During team development there are times when you want to throw explicit errors to both help developers understand whats going on, as well as enforce some strict rules. That is what this module is for.

Kind: global namespace
Example

'use strict';

Errors=require('node-error-classes');

Errors.Immutable ⇐ Error

Kind: static class of Errors
Extends: Error

new Immutable()

Error for Immutable variables

Immutable.setMessage(varaibleName, scope) ⇒ String

Kind: static method of Immutable
Returns: String - compiled error message

Param Type Description
varaibleName String name of immutable variable
scope Any Scope of varaible

Example

const Errors = require('node-error-classes');
    class User{
        constructor(name,age){
            Object.defineProperties(
                this,
                {
                    age:{
                        enumerable:true,
                        writable:true,
                        value:age
                    },
                    name:{
                        enumerable:true,
                        get:getName,
                        set:setName
                    }
                }
            );

            let userName=null;

            function getName(){
                return userName;
            }

            function setName(value){
                if(userName){
                    const err=new Errors.Immutable;
                    err.setMessage(
                        'name',
                        this
                    );

                    throw err;
                }
                userName=value;
                return userName;
            }

            if(name){
                this.name=name;
            }
        }
    }

    const bob=new User('bob',42);

    bob.name='bob';

Example


     git/node-error-classes/example/immutable.js:40
                   throw err;
                   ^

    Immutable: 'name' is Immutable and may not be modified.
            User { age: 42, name: [Getter/Setter] }
        at Immutable (/home/bmiller/git/node-error-classes/lib/Immutable.js:10:1)
        at User.setName [as name] (/home/bmiller/git/node-error-classes/example/immutable.js:34:26)

Errors.InvalidMethod ⇐ Error

Kind: static class of Errors
Extends: Error

new InvalidMethod()

Error for methods which are either undefined or not methods (functions)

InvalidMethod.setMessage(methodName, method, [scope]) ⇒ String

Kind: static method of InvalidMethod
Returns: String - compiled error message

Param Type Description
methodName String method name
method Any expected method
[scope] Any scope in which the invalid method is expected

Example

const Errors = require('node-error-classes');

         class User{
            constructor(name,age){
                this.name=name;
                this.age=age;
            }
         }

        const bob=new User('bob',42);

        if(!bob.getInfo || typeof bob.getInfo !== 'function'){
            const err=new Errors.InvalidMethod;
            err.setMessage('getInfo',bob.getInfo,bob);
        }

Example


git/node-error-classes/example/invalidMethod.js:23
       throw err;
       ^

    UndefinedMethod: Expects 'getInfo' to be Function but got undefined
                Scope : User { name: 'bob', age: 42 }
        at InvalidMethod (/home/bmiller/git/node-error-classes/lib/InvalidMethod.js:10:1)
        at Object.<anonymous> (/home/bmiller/git/node-error-classes/example/invalidMethod.js:15:14)

Errors.InvalidParameter ⇐ Error

Kind: static class of Errors
Extends: Error

new InvalidParameter()

Error for invalid parameters

InvalidParameter.setMessage(parameterName, expected, got, [scope]) ⇒ String

Kind: static method of InvalidParameter
Returns: String - compiled error message

Param Type Description
parameterName Any name of parameter
expected Any what it expected
got Any what it got
[scope] Any optional value where the parameter came from like an object or array

Example

let test={a:1,b:0};

if(test.b<1){
    err=new Errors.InvalidParameter;
    err.setMessage(
        'b',
        'a value greater than 0',
        test.b,
        test
    );
    throw err;
}

Example


git/node-error-classes/example/invalidParam.js:19
        throw err;
        ^

        InvalidParameter: 'b' Expects 'a value greater than 0' but got 0

        at InvalidParameter (/home/bmiller/git/node-error-classes/lib/InvalidParameter.js:11:1)
        at multiplyNumbers (/home/bmiller/git/node-error-classes/example/invalidParam.js:13:13)

Errors.RequiredParameter ⇐ Error

Kind: static class of Errors
Extends: Error

new RequiredParameter()

error for required params that are not set or passed

RequiredParameter.setMessage(parameterName, [scope]) ⇒ String

Kind: static method of RequiredParameter
Returns: String - compiled error message

Param Type Description
parameterName Any name of parameter
[scope] Any optional value where the parameter came from like an object or array

Example

let test={a:1,b:0};

if(!test.c){
    err=new Errors.RequiredParameter;
    err.setMessage(
        'c',
        test
    );
    throw err;
}

Example


git/node-error-classes/example/requiredParam.js:17
        throw err;
        ^

        RequiredParameter: Expects 'numberOne' to be defined

        at RequiredParameter (/home/bmiller/git/node-error-classes/lib/RequiredParameter.js:12:1)
        at multiplyNumbers (/home/bmiller/git/node-error-classes/example/requiredParam.js:13:13)

Errors.SocketUnavailable ⇐ Error

Kind: static class of Errors
Extends: Error

new SocketUnavailable()

Error for when an expected socket is not available

SocketUnavailable.setMessage(socketPath, [scope]) ⇒ String

Kind: static method of SocketUnavailable
Returns: String - compiled error message

Param Type Description
socketPath Any name of parameter
[scope] Any optional value with information on the socket or constructor

Example

const ipc=require('node-ipc');
        const Errors=require('node-error-classes');
        ipc.config.id   = 'hello';
        ipc.config.maxRetries = 3;
        ipc.config.silent=true;

        ipc.connectTo(
            'world',
            function(){
                ipc.of.world.on(
                    'destroy',
                    function(data){
                        const err=new Errors.SocketUnavailable;
                        err.setMessage(
                            ipc.of.world.path,
                            ipc.of.world
                        );
                        throw err;
                    }
                );
            }
        );

Example


        git/node-error-classes/example/socketUnavailable.js:19
                throw err;
                ^

        SocketUnavailable: Socket of '/tmp/app.world' Unavailable

        at SocketUnavailable (/home/bmiller/git/node-error-classes/lib/SocketUnavailable.js:11:1)
        at Object.<anonymous> (/home/bmiller/git/node-error-classes/example/socketUnavailable.js:14:27)
        at Object.pub (/home/bmiller/git/node-error-classes/node_modules/node-ipc/node_modules/event-pubsub/event-pubsub.js:69:19)
        at Object.trigger (/home/bmiller/git/node-error-classes/node_modules/node-ipc/node_modules/event-pubsub/event-pubsub.js:111:21)
        at Socket.connectionClosed (/home/bmiller/git/node-error-classes/node_modules/node-ipc/dao/client.js:157:24)
        at emitOne (events.js:77:13)
        at Socket.emit (events.js:169:7)
        at Pipe._onclose (net.js:469:12)

Errors.Type ⇐ TypeError

Kind: static class of Errors
Extends: TypeError

new Type()

Used for normalizing the message of a type error

Type.setMessage(parameterName, type, value, [scope]) ⇒ String

Kind: static method of Type
Returns: String - compiled error message

Param Type Description
parameterName Any name of parameter
type String Type String
value Any value that caused error
[scope] Any optional value where the parameter came from like an object or array

Example

let test={a:1,b:0};

if(typeof test.b!=='object'){
    err=new Errors.Type;
    err.setMessage(
        'b',
        'Object',
        test.b,
        test
    );
    throw err;
}

Example


git/node-error-classes/example/typeError.js:19
        throw err;
        ^

        TypeError: 'numberOne' Expects String but got number : 6

        at Type (/home/bmiller/git/node-error-classes/lib/Type.js:12:1)
        at multiplyNumbers (/home/bmiller/git/node-error-classes/example/typeError.js:13:13)

Errors.UndefinedValue ⇐ Error

Kind: static class of Errors
Extends: Error

new UndefinedValue()

Error for undefined values

UndefinedValue.setMessage(variableName, variable, [scope]) ⇒ String

Kind: static method of UndefinedValue
Returns: String - compiled error message

Param Type Description
variableName String varible name
variable Any varible
[scope] Any scope in which the variable should exist

Example

const Errors = require('node-error-classes');

    const user={
       name:'bob'
    }

    if(!user.age){
      const err = new Errors.UndefinedValue;
      err.setMessage(
          'age',
          user.age,
          user
      );
      throw err;
    }

Example


    git/node-error-classes/example/undefinedValue.js:14
       throw err;
       ^

    Undefined: 'string'