I use stacktrace.js to get the correct stack trace on exception but I wonder if it’s possible to extract part of the code to get the context of where the exception has arisen
Is there any good and simple way to do that ?
I use stacktrace.js to get the correct stack trace on exception but I wonder if it’s possible to extract part of the code to get the context of where the exception has arisen
Is there any good and simple way to do that ?
 
    
    So after reading the code the answer is yes and stacktrace already store source code in cache. We can retrieve it like that :
create a helper:
async extractFromCache(cache: any, filename: string) {
  let originContent: string = "";
  
  if (cache[filename]) {
    if (typeof cache[filename] === "string") {
      originContent = cache[filename];
    } else if (typeof cache[filename] === "string" && cache[filename].constructor.name === "ZoneAwarePromise") {
      originContent = await cache[filename];
    }
  }
  return originContent;
}
then with stacktracejs need to do like that:
const cache = {};
const trace = await StackTrace.fromError(message, { sourceCache: cache });
const sourceCode = await extractFromCache(cache, trace[0].fileName);
and then use trace.lineNumber to extract some line before and after from sourceCode !
