Last active
January 17, 2020 07:20
-
-
Save Boorj/e9942ede6793842cd39c29a696d30b9b to your computer and use it in GitHub Desktop.
JS: get stack trace
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
/** | |
* getStack | |
* | |
* @param {string | string[]} [excludeStr] - substrings that will mark stack line to be excluded | |
* @return {{ | |
* method ?: string, | |
* url : string, | |
* line : number, | |
* column : number, | |
* }[]|[]} | |
*/ | |
export function getStack(excludeStr ) { | |
const originalStack = new Error().stack; | |
if (!originalStack) | |
return []; | |
const resultStack = []; | |
originalStack.split("\n").slice(1).forEach(function(line) { | |
const matches = line.match(/^ +at (?:([\w.]+) \()?([^()]+?)(?::(\d+))?(?::(\d+))?\)?$/); | |
if (!matches) | |
return; | |
if (excludeStr) { | |
const check = [].concat(excludeStr); | |
for (const ignoreString of check) { | |
if (line.includes(ignoreString)) | |
return; | |
} | |
} | |
resultStack.push({ | |
method : matches[1], | |
url : matches[2], | |
line : matches[3] && parseInt(matches[3], 10), | |
column : matches[4] && parseInt(matches[4], 10) | |
}); | |
}); | |
return resultStack; | |
} |
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
console.table(getStack(['node_modules', 'internal/process'])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment