Gathering detailed insights and metrics for pony-cause
Gathering detailed insights and metrics for pony-cause
Gathering detailed insights and metrics for pony-cause
Gathering detailed insights and metrics for pony-cause
npm install pony-cause
99.3
Supply Chain
100
Quality
76.8
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
49 Stars
154 Commits
3 Forks
3 Watching
7 Branches
4 Contributors
Updated on 25 Nov 2024
JavaScript (99.69%)
Shell (0.31%)
Cumulative downloads
Total Downloads
Last day
-5.4%
238,453
Compared to previous day
Last week
4.4%
1,337,842
Compared to previous week
Last month
7.5%
5,432,383
Compared to previous month
Last year
85.7%
49,540,301
Compared to previous year
29
Helpers and ponyfill for Error Causes
findCauseByReference
- finding an error of a specific type within the cause chaingetErrorCause
- getting the direct cause of an error, if there is anymessageWithCauses
- gets the error message with the messages of its cause chain appended to itstackWithCauses
- gets a stack trace for the error + all its causesAll the above are backwards compatible with causes created by the VError
module which predated the Error Causes spec and is still used in parts of the ecosystem.
ErrorWithCause
- an exported Error
subclass that works like the Error Causes spec. By using this class you "ponyfill" the spec locally rather than eg. polyfilling it globally.pony-cause
is dual published as both CommonJS and ESM, use whichever you like and make use of the TypeScript compliant types no matter which.
ErrorWithCause
Ponyfill of the cause
-supporting Error
class
1const { ErrorWithCause } = require('pony-cause'); 2 3try { /* Something that can break */ } catch (err) { 4 throw new ErrorWithCause('Failed doing what I intended', { cause: err }); 5}
findCauseByReference
Finding an error of a specific type within the cause chain. Is typescript friendly.
1const { findCauseByReference } = require('pony-cause'); 2 3try { /* Something that can break */ } catch (err) { 4 /** @type {MySpecialError} */ 5 const specialErr = findCauseByReference(err, MySpecialError); 6 7 if (specialErr && specialErr.specialProperty === 'specialValue') { 8 // Its okay, chill! 9 } else { 10 throw err; 11 } 12}
Used to find a specific type of error in the chain of causes in an error.
Similar to VError.findCauseByName
but resolves causes in both Error Causes style, .cause
, and VError style, .cause()
+ takes a reference to the Error class that you are looking for rather than simply the name of it, as that enables the TypeScript types to properly type the returned error, typing it with the same type as the reference.
Can be useful if there's some extra data on it that can help determine whether it's an unexpected error or an error that can be handled.
If it's an error related to a HTTP request, then maybe the request can be retried? If its a database error that tells you about a duplicated row, then maybe you know how to work with that? Maybe forward that error to the user rather than show a 500
error?
Note: findCauseByReference
has protection against circular causes
getErrorCause
Getting the direct cause of an error, if there is any
1const { getErrorCause } = require('pony-cause'); 2 3try { /* Something that can break */ } catch (err) { 4 // Returns the Error cause, VError cause or undefined 5 const cause = getErrorCause(err); 6}
The output is similar to VError.cause()
but resolves causes in both Error Causes style, .cause
, and VError style, .cause()
.
Always return an Error
, a subclass of Error
or undefined
. If a cause in Error Causes style cause is not an Error
or a subclass of Error
, it will be ignored and undefined
will be returned.
messageWithCauses
Gets the error message with the messages of its cause chain appended to it.
1const { messageWithCauses, ErrorWithCause } = require('pony-cause'); 2 3try { 4 try { 5 // First error... 6 throw new Error('First error'); 7 } catch (err) { 8 // ...that's caught and wrapped in a second error 9 throw new ErrorWithCause('Second error', { cause: err }); 10 } 11} catch (err) { 12 // Logs the full message trail: "Second error: First error" 13 console.log(messageWithCauses(err)); 14}
The output is similar to the standard VError
behaviour of appending message
with the cause.message
, separating the two with a :
.
Since Error Causes doesn't do this, messageWithCauses
exist to mimic that behaviour.
It respects VError
messages, it won't append any error message of their causes, though it will walk past the VError
causes to see if there's a non-VError cause up the chain and then append that.
The reason to use this method is explained by VError
:
The idea is that each layer in the stack annotates the error with a description of what it was doing. The end result is a message that explains what happened at each level.
If an inner error has a message ENOENT, stat '/nonexistent'
, an outer error wraps it and adds Can't perform X
and maybe one more error wraps that and adds Can't start program
, then messageWithCauses
will join those three errors together when providing it with the outer most error and return Can't start program: Can't perform X: ENOENT, stat '/nonexistent'
which provides details about both cause and effect as well as the connection between the two – each which on their own would be a lot harder to understand the impact of.
Note: messageWithCauses
has protection against circular causes
stackWithCauses
Gets a stack trace for the error + all its causes.
1const { stackWithCauses } = require('pony-cause'); 2 3try { /* Something that can break */ } catch (err) { 4 console.log('We had a mishap:', stackWithCauses(err)); 5}
The output is similar to VError.fullStack()
but resolves causes in both Error Causes style, .cause
, and VError style, .cause()
.
Note: stackWithCauses
has protection against circular causes
Output looks like:
Error: something really bad happened here
at Object.<anonymous> (/examples/fullStack.js:5:12)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3
caused by: Error: something bad happened
at Object.<anonymous> (/examples/fullStack.js:3:12)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3
Available as part of the Tidelift Subscription.
The maintainers of pony-cause
and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source packages you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact packages you use. Learn more.
verror
– a module which has long enabled error causes in javascript. Superseded by the new Error Cause proposal. Differs in that.cause
represents a function that returns the cause, its not the cause itself.@netflix/nerror
– a Netflix fork of verror
error-cause
– strict polyfill for the Error Cause proposal. Provides no helpers or similar to achieve VError
-like functionality, which pony-cause
does.No vulnerabilities found.
Reason
security policy file detected
Details
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
GitHub workflow tokens follow principle of least privilege
Details
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 0/25 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More