IError
This is a module provides support for some missing features of native errors, working as a drop in replacement for native errors:
- chain of causes
- custom information for the error
- timestamp of the error occurrence
- serialization and composing back the object from serialization
Instalation
npm install @itavy/ierror
Quick Example
const IError = require('@itavy/ierror').IError;
const err = new IError('test error');
console.log(err);
this will produce:
{ ERROR: test error
at Object.<anonymous> (/**********/test.js:3:13)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
name: 'ERROR',
ts: 1492430124565,
severity: 'ERROR',
source: null,
code: null,
extra: null }
API
object
itavy/ierror : Kind: global namespace
-
itavy/ierror :
object
-
.IError
- new IError(options)
-
.message :
String
-
.name :
String
-
.ts :
Number
-
.severity :
String
-
.source :
String
-
.code :
String
-
.extra :
*
-
.origStack :
String
-
.cause :
IError
-
.toJSON() ⇒
IErrorLiteral
-
.toString() ⇒
String
-
.hasErrorWithName(name) ⇒
Boolean
-
.hasErrorWithCode(code) ⇒
Boolean
-
.getErrorWithName(name) ⇒
IError
|null
-
.getErrorWithCode(code) ⇒
IError
|null
-
.rejectIError(request) ⇒
Promise
-
.resolveIError(request) ⇒
Promise
-
.IErrorLiteral :
Object
-
.IError
itavy/ierror.IError
Error struture
Kind: static class of itavy/ierror
-
.IError
- new IError(options)
-
.message :
String
-
.name :
String
-
.ts :
Number
-
.severity :
String
-
.source :
String
-
.code :
String
-
.extra :
*
-
.origStack :
String
-
.cause :
IError
-
.toJSON() ⇒
IErrorLiteral
-
.toString() ⇒
String
-
.hasErrorWithName(name) ⇒
Boolean
-
.hasErrorWithCode(code) ⇒
Boolean
-
.getErrorWithName(name) ⇒
IError
|null
-
.getErrorWithCode(code) ⇒
IError
|null
new IError(options)
Param | Type | Description |
---|---|---|
options | IErrorLiteral |
error info |
String
iError.message : Error message
Kind: instance property of IError
String
iError.name : Error name
Kind: instance property of IError
Default: 'ERROR'
Number
iError.ts : Error UNIX timestamp
Kind: instance property of IError
Default: Date.now()
String
iError.severity : Error severity
Kind: instance property of IError
Default: 'ERROR'
String
iError.source : Error source
Kind: instance property of IError
Default: null
String
iError.code : Error code
Kind: instance property of IError
Default: null
*
iError.extra : Error extra information
Kind: instance property of IError
Default: null
String
iError.origStack : Error stack trace
Kind: instance property of IError
Default: null
Read only: true
IError
iError.cause : Error cause
Kind: instance property of IError
Default: null
Read only: true
IErrorLiteral
iError.toJSON() ⇒ get IError literal representation
Kind: instance method of IError
Returns: IErrorLiteral
- IError literal representation
Access: public
String
iError.toString() ⇒ get IError json stringify
Kind: instance method of IError
Returns: String
- JSON representation of IError
Access: public
Boolean
iError.hasErrorWithName(name) ⇒ Check all cause chain to see if an error with requested name exists
Kind: instance method of IError
Returns: Boolean
- true if an error with a name exists
Access: public
Param | Type | Description |
---|---|---|
name | String |
name of the error to look after |
Boolean
iError.hasErrorWithCode(code) ⇒ Check all cause chain to see if an error with requested code exists
Kind: instance method of IError
Returns: Boolean
- true if an error with a code exists
Access: public
Param | Type | Description |
---|---|---|
code | String |
code of the error to look after |
IError
| null
iError.getErrorWithName(name) ⇒ Traverse all chain to get error with requested name
Kind: instance method of IError
Returns: IError
| null
- IError found or null otherwise
Access: public
Param | Type | Description |
---|---|---|
name | String |
name of the error to look after |
IError
| null
iError.getErrorWithCode(code) ⇒ Traverse all chain to get error with requested code
Kind: instance method of IError
Returns: IError
| null
- IError found or null otherwise
Access: public
Param | Type | Description |
---|---|---|
code | String |
code of the error to look after |
Promise
itavy/ierror.rejectIError(request) ⇒ Rejects with IError
Kind: static method of itavy/ierror
Returns: Promise
- rejects with an IError
Param | Type | Description |
---|---|---|
request |
IErrorLiteral | Error | IError
|
error to reject with |
Promise
itavy/ierror.resolveIError(request) ⇒ Resolves with IError
Kind: static method of itavy/ierror
Returns: Promise
- resolves with an IError
Access: public
Param | Type | Description |
---|---|---|
request | Object |
error to resolve with |
Object
itavy/ierror.IErrorLiteral : Kind: static typedef of itavy/ierror
Properties
Name | Type | Default | Description |
---|---|---|---|
message | String |
Error message | |
[name] | String |
'ERROR' |
Error name |
[ts] | Number |
Date.now() |
Error UNIX timestamp |
[severity] | String |
'ERROR' |
Error severity |
[source] | String |
|
Error source |
[code] | String |
|
Error code |
[extra] | * |
|
Extra information needed to be held with the error |
[cause] |
IError | Error | null
|
original cause which triggered | |
[origStack] | String |
stack of the error this error on chain |
Usage
see Example