Skip to content

Instantly share code, notes, and snippets.

@Arcath
Created December 11, 2017 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arcath/91caa45b53e6454171de1f61de9dfd7e to your computer and use it in GitHub Desktop.
Save Arcath/91caa45b53e6454171de1f61de9dfd7e to your computer and use it in GitHub Desktop.
// 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