object
Errors : 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=;
- Errors :
object
- .Immutable ⇐
Error
- .InvalidMethod ⇐
Error
- .InvalidParameter ⇐
Error
- .RequiredParameter ⇐
Error
- .SocketUnavailable ⇐
Error
- .Type ⇐
TypeError
- .UndefinedValue ⇐
Error
- .Immutable ⇐
Error
Errors.Immutable ⇐ Kind: static class of Errors
Extends: Error
- .Immutable ⇐
Error
new Immutable()
Error for Immutable variables
String
Immutable.setMessage(varaibleName, scope) ⇒ 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 = ; { Object; let userName=null; { return userName; } { ifuserName const err=; err; throw err; userName=value; return userName; } ifname thisname=name; } const bob='bob'42; bobname='bob';
Example
git/node-error-classes/example/immutable.js:40 throw err; ^ Immutable: 'name' is Immutable and may not be modified. User
Error
Errors.InvalidMethod ⇐ Kind: static class of Errors
Extends: Error
- .InvalidMethod ⇐
Error
new InvalidMethod()
Error for methods which are either undefined or not methods (functions)
String
InvalidMethod.setMessage(methodName, method, [scope]) ⇒ 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 = ; { thisname=name; thisage=age; } const bob='bob'42; if!bobgetInfo || typeof bobgetInfo !== 'function' const err=; err;
Example
git/node-error-classes/example/invalidMethod.js:23 throw err; ^ UndefinedMethod: Expects 'getInfo' to be Function but got undefined Scope : User
Error
Errors.InvalidParameter ⇐ Kind: static class of Errors
Extends: Error
- .InvalidParameter ⇐
Error
new InvalidParameter()
Error for invalid parameters
String
InvalidParameter.setMessage(parameterName, expected, got, [scope]) ⇒ 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:1b:0; iftestb<1 err=; err; 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 at multiplyNumbers
Error
Errors.RequiredParameter ⇐ Kind: static class of Errors
Extends: Error
- .RequiredParameter ⇐
Error
new RequiredParameter()
error for required params that are not set or passed
String
RequiredParameter.setMessage(parameterName, [scope]) ⇒ 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:1b:0; if!testc err=; err; throw err;
Example
git/node-error-classes/example/requiredParam.js:17 throw err; ^ RequiredParameter: Expects 'numberOne' to be defined at RequiredParameter at multiplyNumbers
Error
Errors.SocketUnavailable ⇐ Kind: static class of Errors
Extends: Error
- .SocketUnavailable ⇐
Error
new SocketUnavailable()
Error for when an expected socket is not available
String
SocketUnavailable.setMessage(socketPath, [scope]) ⇒ 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=; const Errors=; ipcconfigid = 'hello'; ipcconfigmaxRetries = 3; ipcconfigsilent=true; ipc;
Example
git/node-error-classes/example/socketUnavailable.js:19 throw err; ^ SocketUnavailable: Socket of '/tmp/app.world' Unavailable at SocketUnavailable at Object.<anonymous> at Object.pub at Object.trigger at Socket.connectionClosed at emitOne at Socket.emit at Pipe._onclose
TypeError
Errors.Type ⇐ Kind: static class of Errors
Extends: TypeError
- .Type ⇐
TypeError
new Type()
Used for normalizing the message of a type error
String
Type.setMessage(parameterName, type, value, [scope]) ⇒ 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:1b:0; iftypeof testb!=='object' err=; err; throw err;
Example
git/node-error-classes/example/typeError.js:19 throw err; ^ TypeError: 'numberOne' Expects String but got number : 6 at Type at multiplyNumbers
Error
Errors.UndefinedValue ⇐ Kind: static class of Errors
Extends: Error
new UndefinedValue()
Error for undefined values
String
UndefinedValue.setMessage(variableName, variable, [scope]) ⇒ 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 = ; const user= name:'bob' if!userage const err = ; err; throw err;
Example
git/node-error-classes/example/undefinedValue.js:14 throw err; ^ Undefined: 'string'