-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serialize err.cause
in a nice way when possible
#78
Merged
mcollina
merged 4 commits into
pinojs:master
from
voxpelli:add-support-for-error-causes
Nov 24, 2021
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
'use strict' | ||
|
||
// ************************************************************** | ||
// * Code initially copied/adapted from "pony-cause" npm module * | ||
// * Please upstream improvements there * | ||
// ************************************************************** | ||
|
||
/** | ||
* @param {Error|{ cause?: unknown|(()=>err)}} err | ||
* @returns {Error|undefined} | ||
*/ | ||
const getErrorCause = (err) => { | ||
if (!err) return | ||
|
||
/** @type {unknown} */ | ||
// @ts-ignore | ||
const cause = err.cause | ||
|
||
// VError / NError style causes | ||
if (typeof cause === 'function') { | ||
// @ts-ignore | ||
const causeResult = err.cause() | ||
|
||
return causeResult instanceof Error | ||
? causeResult | ||
: undefined | ||
} else { | ||
return cause instanceof Error | ||
? cause | ||
: undefined | ||
} | ||
} | ||
|
||
/** | ||
* Internal method that keeps a track of which error we have already added, to avoid circular recursion | ||
* | ||
* @private | ||
* @param {Error} err | ||
* @param {Set<Error>} seen | ||
* @returns {string} | ||
*/ | ||
const _stackWithCauses = (err, seen) => { | ||
if (!(err instanceof Error)) return '' | ||
|
||
const stack = err.stack || '' | ||
|
||
// Ensure we don't go circular or crazily deep | ||
if (seen.has(err)) { | ||
return stack + '\ncauses have become circular...' | ||
} | ||
|
||
const cause = getErrorCause(err) | ||
|
||
if (cause) { | ||
seen.add(err) | ||
return (stack + '\ncaused by: ' + _stackWithCauses(cause, seen)) | ||
} else { | ||
return stack | ||
} | ||
} | ||
|
||
/** | ||
* @param {Error} err | ||
* @returns {string} | ||
*/ | ||
const stackWithCauses = (err) => _stackWithCauses(err, new Set()) | ||
|
||
/** | ||
* Internal method that keeps a track of which error we have already added, to avoid circular recursion | ||
* | ||
* @private | ||
* @param {Error} err | ||
* @param {Set<Error>} seen | ||
* @param {boolean} [skip] | ||
* @returns {string} | ||
*/ | ||
const _messageWithCauses = (err, seen, skip) => { | ||
if (!(err instanceof Error)) return '' | ||
|
||
const message = skip ? '' : (err.message || '') | ||
|
||
// Ensure we don't go circular or crazily deep | ||
if (seen.has(err)) { | ||
return message + ': ...' | ||
} | ||
|
||
const cause = getErrorCause(err) | ||
|
||
if (cause) { | ||
seen.add(err) | ||
|
||
// @ts-ignore | ||
const skipIfVErrorStyleCause = typeof err.cause === 'function' | ||
|
||
return (message + | ||
(skipIfVErrorStyleCause ? '' : ': ') + | ||
_messageWithCauses(cause, seen, skipIfVErrorStyleCause)) | ||
} else { | ||
return message | ||
} | ||
} | ||
|
||
/** | ||
* @param {Error} err | ||
* @returns {string} | ||
*/ | ||
const messageWithCauses = (err) => _messageWithCauses(err, new Set()) | ||
|
||
module.exports = { | ||
getErrorCause, | ||
stackWithCauses, | ||
messageWithCauses | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the message a concatenation of the three errors? Can you show an example of the full serialized error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's because all three describe the error that has happened, but at three different levels. An example:
If
err
consists of thrown and wrapped errors in these three steps:ENOENT, stat '/nonexistent'
Failed to open cache
Can't cache example.com/robots.txt
Then when someone does:
It will log:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the idea is that the top most error should only describe the error on its abstraction level, not on any of the lower levels, but rather refer to the error causes for adding those description at those levels.
This makes it easy to distinguish between
Can't cache example.com/robots.txt
errors caused byFailed to open cache
if there's also another reason going around that's maybeMalformed cache file content
.Here's a real life example: https://github.com/dependency-check-team/dependency-check/blob/c0537d04452098208132c6633277374dc9ad0a8d/lib/resolve-target.js#L40-L43
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. My biggest concern was that I thought I was using the standard error serializer from this package and that this change in message shape would break me. But it seems I'm not using it. Still, this will be a semver major change unless it serializes like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we keep the nested
cause
and only extendstack
, then I think we can make it a minor, then follow up withmessage
+ removing redundantcause
in a major?Thoughts @jsumners @mcollina?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking a bit more about this, I think safest would be to simply make this a major change and make all changes at once