npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-error-classes

v2.0.1

Published

Custom error classes for node.js

Downloads

6

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'