smart-error v3.0.0
SmartError
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.
Installation
$ npm install smart-error
Usage
// 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
Classes
Functions
Typedefs
SmartError
Kind: global class
new SmartError(message, code, payload)
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
smartError.toJSON(stack) ⇒ Object
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. |
smartError._getCode(code)
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 |
smartError._setPayload(payload)
Sets the payload fields as instance fields. Message, code and stack are ignored.
Kind: instance method of SmartError
Param | Type |
---|---|
payload | Object |
smartError._parsePayload(err) ⇒ Object
Parses the payload from the SmartError instance.
Kind: instance method of SmartError
Param | Type |
---|---|
err | SmartError |
SmartError.register(code, message, payload, description)
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 |
SmartError.unregister(code)
Removes the error from th object.
Kind: static method of SmartError
Param | Type |
---|---|
code | string |
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.
Kind: global function
Param | Type | Default |
---|---|---|
SmartError | SmartError | |
code | string | |
message | string | |
payload | Object | |
description | string | null |
unregister(SmartError, code)
Removes the error from the SmartError object.
Kind: global function
Param | Type |
---|---|
SmartError | SmartError |
code | string |
codes() ⇒ Array.<string>
Gets all registered codes as an array.
Kind: global function
docs() ⇒ DocsObject
Gets the documentation of the registered error.
Kind: global function
_call(code, SmartError)
Calls the registered function.
Kind: global function
Param | Type |
---|---|
code | string |
SmartError | SmartError |
ErrorObject
Kind: global typedef Properties
Name | Type |
---|---|
message | string |
code | string |
payload | Object |
DocsObject
Kind: global typedef
Param | Type |
---|---|
code.description | string |
Properties
Name | Type |
---|---|
code | object |