-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Serialize
err.cause
in a nice way when possible (#78)
* Serialize `err.cause` in a nice way when possible * Self-host pony-cause helpers * Add pony-cause banner
- Loading branch information
Showing
3 changed files
with
150 additions
and
3 deletions.
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