Skip to content

Instantly share code, notes, and snippets.

@sasin91
Created February 12, 2024 13:13
Show Gist options
  • Save sasin91/c261bae11a065b0b93d88f2a4a58ae67 to your computer and use it in GitHub Desktop.
Save sasin91/c261bae11a065b0b93d88f2a4a58ae67 to your computer and use it in GitHub Desktop.
getCalleeFromStackTrace.js
export const getCalleeFromStackTrace = () => {
const e = new Error();
const frame = e.stack.split(`\n`)[2]; // change to 3 for grandparent func
// const lineNumber = frame.split(`:`).reverse()[1];
// const functionName = frame.split(` `)[5];
const regex = /(\S+)\s+\(([^:]+):(\d+):(\d+)\)/;
const match = frame.trim().match(regex);
if (!match) {
return null;
}
const [, functionName, fileName, lineNumber] = match;
return {
functionName,
fileName,
lineNumber: parseInt(lineNumber)
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment