2.1.1 • Published 3 years ago

@yo1dog/cerror v2.1.1

Weekly downloads
17
License
ISC
Repository
github
Last release
3 years ago

node-cerror

Chainable Errors

npm install @yo1dog/cerror

Quick Start

const {CError} = require('@yo1dog/cerror');
const {MyError} = require('MyError');

const sysErr = new Error('system failure');

// chain with CError class
console.log(
  new CError(sysErr, 'Unable to do thing A.')
);

/*
CError: Unable to do thing A.
    at readme.js:6:51
    at Script.runInThisContext (vm.js:123:20)
    ...
    
----- Caused By -----

Error: system failure
    at readme.js:4:16
    at Script.runInThisContext (vm.js:123:20)
    ...
*/
// chain custom/any error classes
console.log(
  CError.chain(sysErr, new MyError('Unable to do thing B.'));
)

/*
MyError: Unable to do thing B.
    at readme.js:7:34
    at Script.runInThisContext (vm.js:123:20)
    ...
    
----- Caused By -----

Error: system failure
    at readme.js:4:16
    at Script.runInThisContext (vm.js:123:20)
    ...
*/
// chain non-Error instances
const errObj = {status:500};

console.log(
  new CError(errObj, 'Unable to do thing C.')
);

/*
MyError: Unable to do thing C.
    at readme.js:7:34
    at Script.runInThisContext (vm.js:123:20)
    ...
    
----- Caused By -----

(type: object, constructor: Object) {status: 500}
*/

Docs

new CError(cause, [message])

paramtypedescription
causeErrorThe cause of this Error.
messagestringA human-readable description of the error.

Creates an error with a cause.

Equivalent to:

CError.chain(cause, new Error(message))

CError.prototype.getCause()

Returns the cause of this error (the next error in the chain).

Equivalent to:

this[CError.causeSymbol]
CError.getCause(this)

CError.prototype.getUnchainedStack()

Returns the original stack of this error before it was chained.

Equivalent to:

this[CError.origStackSymbol]
CError.getUnchainedStack(this)

CError.chain(...errs)

paramtypedescription
errsError[]Errors to chain together.

Chains together errors such that the first error is the root cause and the last error is the result.

Returns the last error.


CError.getCause(err)

paramtypedescription
errErrorStart of error chain to traverse.

Returns the cause of the given error (the next error in the chain).

Equivalent to:

err[CError.causeSymbol]
CError.getChain(err)[1]

CError.getUnchainedStack(err)

paramtypedescription
errErrorError in chain.

Returns the original stack of the given error before it was chained.

Equivalent to:

err[CError.origStackSymbol]

CError.getRootError(err)

paramtypedescription
errErrorStart of error chain to traverse.

Returns the root error of the given error's chain.

Note: If the given error does not have a cause, the given error is the root and is returned.

Equivalent to

CError.getChain(err).pop()

CError.getChain(err)

paramtypedescription
errErrorStart of error chain to traverse.

Returns the given error's chain of errors as an array.

Note: the chain contains the given error at index 0.

Equivalent to

Array.from(CError.getChainIterator(err, true))

CError.findInChain(err, callback)

paramtypedescription
errErrorStart of error chain to traverse.
callbackfunctionTesting function.

Callback params: param | type | description --------|--------|------------- err | Error | Current error in chain. depth | number | Current depth in chain.

Returns the first error in the given error's chain that satisfies the given testing function.

Similar to except the arguments passed to callback differ slighty:

CError.getChain(err).find(callback)

CError.getFirstInstanceOf(err, constructor)

paramtypedescription
errErrorStart of error chain to traverse.
constructorfunctionTesting function.

Returns the first error in the given error's chain that is an instance of the given constructor.

Equivalent to:

CError.findInChain(err, err => err instanceof constructor)

CError.getErrorAt(err, depth)

paramtypedescription
errErrorStart of error chain to traverse.
depthnumberDepth to traverse to.

Returns the error in the given depth in the given error's chain.

A depth of 0 will return the given error. 1 will return the given error's cause. etc.

Similar to except this function will traverse circular references (won't throw an error):

CError.getChain(err)[depth]

CError.getChainIterator(err, [checkCircular])

paramtypedescription
errErrorStart of error chain to traverse.
checkCircularbooleanIf an error should be thrown on circular references (true) or not (false). Defaults to false.

Returns an interator that traverses the given error's chain.


CError.isChainable(val)

paramtypedescription
valanyValue to check.

Returns if the given value can store a cause and be at the end or middle of a chain.


CError.causeSymbol

Symbol for accessing the cause of an error.


CError.origStackSymbol

Symbol for accessing the original stack of an error before it was chained.

2.1.1

3 years ago

2.1.0

3 years ago

2.0.5

3 years ago

2.0.4

3 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

1.1.1

4 years ago

1.2.0

4 years ago

1.1.0

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

2.0.0

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago