Add a stack trace reference to an existing object with a context error

I am getting an occasional error returning from a library call, however the error stack trace does not save the full stack trace, making it difficult to determine where this is coming from.

I would like to figure out a way to add a reference to where / what this error stack trace is being accessed. I may not be able to catch the top level function name, but even if I could get the line numbers of the error handler, it would be enough for me to point me in the right direction.

After tackling this, I believe I may have a rather hacky solution that might work (although not tested in my production environment).

var addStackFrame = function(e) {
  var splitStack = e.stack.split("\n");

  // We splice from the 3rd element to drop
  // the error name, and addStackFrame() reference.
  var thisLine = new Error().stack
                  .split("\n")
                  .slice(2, 3)
                  .join('\n');

  e.stack = [splitStack[0], thisLine]
            .concat(splitStack.slice(1))
            .join("\n");

  return e;
};

      

This log adds the last three methods that were invoked on the object's stack trace.

I've also seen this question , which suggests that there might be a native V8 way to implement this, which would be much more fun to use.

Is there a native way to add stack trace references to an existing Error object? If not, is there a more elegant way to handle the context for a stack trace?

+3


source to share





All Articles