-
-
Save Arcath/91caa45b53e6454171de1f61de9dfd7e to your computer and use it in GitHub Desktop.
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
// Where `error` is the error caught by yout try/catch block | |
const stack = error instanceof Error ? error.stack : error; | |
const re = /^\s+at\s+(.+?\s+)?\(?([0-z._\-\\\/]+):(\d+):(\d+)\)?$/gm | |
let match: RegExpExecArray | null | |
let outStack = error.toString() | |
while(match = re.exec(stack)){ | |
if(match[2] === 'main'){ | |
const pos = consumer.originalPositionFor({ | |
line: parseInt(match[3], 10), | |
column: parseInt(match[4], 10) | |
}) | |
if(pos.line != null){ | |
if (pos.name) { | |
outStack += `\n at ${pos.name} (${pos.source}:${pos.line}:${pos.column})` | |
} else { | |
if (match[1]) { | |
// no original source file name known - use file name from given trace | |
outStack += `\n at ${match[1]} (${pos.source}:${pos.line}:${pos.column})` | |
} else { | |
// no original source file name known or in given trace - omit name | |
outStack += `\n at ${pos.source}:${pos.line}:${pos.column}` | |
} | |
} | |
} else { | |
// no known position | |
break; | |
} | |
} else { | |
// no more parseable lines | |
break; | |
} | |
} | |
console.log(outStack) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment