Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Callstack for javascript
import { sep } from 'path';
function splitPath(filename, level: number) {
const paths = filename.split(sep);
return paths.splice(paths.length - level).join(sep);
}
export function caller(level: number) {
const oldStackTrace = Error.prepareStackTrace;
try {
// eslint-disable-next-line handle-callback-err
Error.prepareStackTrace = (err, structuredStackTrace) =>
structuredStackTrace;
Error.captureStackTrace(this);
const callSite = this.stack[level];
return (
splitPath(callSite.getFileName(), 2) + ':' + callSite.getLineNumber()
);
} finally {
Error.prepareStackTrace = oldStackTrace;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment