Last active
January 11, 2024 21:32
-
-
Save colelawrence/fc3a738b58a76a4bfd63f4da57166d1a to your computer and use it in GitHub Desktop.
DevError removes irrelevant stack trace lines
This file contains hidden or 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
// regexes to remove lines from thrown error stacktraces | |
const AT_NODE_INTERNAL_RE = /^\s*at.+node:internal.+/gm; | |
const AT_TYPES_INTERNAL_RE = /^\s*at.+\/types\/src\/.+/gm; | |
const AT_INVARIANT_RE = /^\s*(at|[^@]+@) (?:Object\.)?invariant.+/gm; | |
const AT_INVARIANT_MORE_RE = /^\s*at.+(?:invariant|asError|createErrorObj).+/gm; | |
const AT_TEST_HELPERS_RE = /^\s*(at|[^@]+@).+test\-helpers.+/gm; | |
// const AT_WEB_MODULES = /^\s*(at|[^@]+@).+(web_modules|\-[a-f0-9]{8}\.js).*/gm | |
const AT_ASSORTED_HELPERS_RE = | |
/^\s*(at|[^@]+@).+(debounce|invariant|iife)\.[tj]s.*/gm; | |
/** | |
* `DevError` removes lines from the `Error.stack` stack trace string | |
* which cleans up the stack trace, making it more developer friendly to read. | |
*/ | |
export class DevError extends Error { | |
found: any; | |
records: any; | |
cause: any; | |
constructor(message: string) { | |
super(message); | |
// const before = this.stack | |
// prettier-ignore | |
if (this.stack) { | |
this.stack = this.stack | |
.replace(AT_INVARIANT_RE, "") | |
.replace(AT_INVARIANT_MORE_RE, "") | |
.replace(AT_TYPES_INTERNAL_RE, "") | |
.replace(AT_ASSORTED_HELPERS_RE, "") | |
.replace(AT_TEST_HELPERS_RE, "") | |
.replace(AT_NODE_INTERNAL_RE, ""); | |
// console.error({ before, after: this.stack }) | |
} | |
} | |
toJSON() { | |
return { | |
message: this.message, | |
found: this.found, | |
cause: this.cause, | |
records: this.records, | |
stack: this.stack, | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment