Subclass of Node.js Error for unification of errors.
The instance has message
and code
attributes for recognizing the error type and base info about the Error. The constructor accepts the payload
field which is parsed as instance's attributes.
$ npm install smart-error
// This will override default Node.js Error
import Error from 'smart-error';
// It throws the catchable error
throw new Error('User already exists in database', 'existing_user', { email: 'test@test.com' });
// The fields are
// message -> User already exists in database
// code -> ERR_EXISTING_USER
// email -> test@test.com
- register(SmartError, code, message, payload, description)
Registers the error to the SmartError object. The code is accesible as the object's function with message and payload fields.
- unregister(SmartError, code)
Removes the error from the SmartError object.
- codes() ⇒
Array.<string>
Gets all registered codes as an array.
- docs() ⇒
DocsObject
Gets the documentation of the registered error.
- _call(code, SmartError)
Calls the registered function.
Kind: global class
Creates new instance of SmartError.
Param | Type |
---|---|
message | string | SmartError | Error | ErrorObject |
code | string |
payload | Object |
smartError.clone() ⇒ SmartError
Clones current instance and creates new one.
Kind: instance method of SmartError
Converts the instance to JSON object.
Kind: instance method of SmartError
Param | Type | Default | Description |
---|---|---|---|
stack | boolean |
false |
If true the stack is added in the JSON object. |
Gets the upper cased error with ERR_ prefix from the code. If the code already has the prefix, the code is not altered.
Kind: instance method of SmartError
Param | Type |
---|---|
code | string |
Sets the payload fields as instance fields. Message, code and stack are ignored.
Kind: instance method of SmartError
Param | Type |
---|---|
payload | Object |
Parses the payload from the SmartError instance.
Kind: instance method of SmartError
Param | Type |
---|---|
err | SmartError |
Registers the error to the object. The code is accesible as the object's function with message and payload fields.
Kind: static method of SmartError
Param | Type |
---|---|
code | string |
message | string |
payload | object |
description | string |
Removes the error from th object.
Kind: static method of SmartError
Param | Type |
---|---|
code | string |
Registers the error to the SmartError object. The code is accesible as the object's function with message and payload fields.
Kind: global function
Param | Type | Default |
---|---|---|
SmartError | SmartError |
|
code | string |
|
message | string |
|
payload | Object |
|
description | string |
null |
Removes the error from the SmartError object.
Kind: global function
Param | Type |
---|---|
SmartError | SmartError |
code | string |
Gets all registered codes as an array.
docs() ⇒ DocsObject
Gets the documentation of the registered error.
Calls the registered function.
Kind: global function
Param | Type |
---|---|
code | string |
SmartError | SmartError |
Kind: global typedef Properties
Name | Type |
---|---|
message | string |
code | string |
payload | Object |
Kind: global typedef
Param | Type |
---|---|
code.description | string |
Properties
Name | Type |
---|---|
code | object |