Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

browser-error-classes

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

browser-error-classes

## Errors : <code>object</code> # Custom and Explicit Javascript Error Classes

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

Errors : object

Custom and Explicit Javascript Error Classes

bower install browser-error-classes

Easy to read custom and extensible error classes for the browser which extend the native error classes.

Include all the errors with Error.js OR include just what you need from the lib/ dir.

Sometimes, just throwing an error isn't that helpful. Sometimes, handling an error elegantly preventing a crash just isn't quite good enough sometimes you want to handle explicit errors in the UI in specific ways. 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

<script src='bower_components/browser-error-classes/Errors.js'></script>

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

ParamTypeDescription
varaibleNameStringname of immutable variable
scopeAnyScope of varaible

Example

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



immutable.html:39 Uncaught Immutable: "name" is Immutable and may not be modified.
    Scope : {
    "age": 42,
    "name": "bob"
}

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

ParamTypeDescription
methodNameStringmethod name
methodAnyexpected method
[scope]Anyscope in which the invalid method is expected

Example

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



 invalidMethod.html:25 Uncaught InvalidMethod: Expects "getInfo" to be Function but got undefined
             Scope : {
     "name": "bob",
     "age": 42
 }

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

ParamTypeDescription
parameterNameAnyname of parameter
expectedAnywhat it expected
gotAnywhat it got
[scope]Anyoptional 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




        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

ParamTypeDescription
parameterNameAnyname of parameter
[scope]Anyoptional 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



        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

ParamTypeDescription
socketPathAnyname of parameter
[scope]Anyoptional 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



        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

ParamTypeDescription
parameterNameAnyname of parameter
typeStringType String
valueAnyvalue that caused error
[scope]Anyoptional 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

ParamTypeDescription
variableNameStringvarible name
variableAnyvarible
[scope]Anyscope in which the variable should exist

Example

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'

FAQs

Package last updated on 25 Dec 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc