Skip to content

Instantly share code, notes, and snippets.

@Arcath

Arcath/sourceMap.js Secret

Created Dec 11, 2017
Embed
What would you like to do?
// 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