Hooking into a constructor call (Error)

24 views
Skip to first unread message

Francisco Tolmasky

unread,
Dec 30, 2016, 11:05:22 AM12/30/16
to v8-users
I would like some way to hook into whenever the Error contractor is called. I'll let you know what I'm doing in case there is some other way to accomplish this, but basically I am trying to get access to the actual stack trace for Error objects, without breaking the stack property. I've tried doing an object->Clone() on the error, setting a custom Error.prepareStackTrace, calling stack, then resetting Error.prepareStackTrace, and this almost works, but on some versions of chrome this still breaks the original error:


    Error.prepareStackTrace = function(e, s) { stack = s };
    clone = clone(error);
    Error.prepareStackTrace = function() { } // fine in v8 used in node 5, 0.12, and 0.10, but breaks in node 6

SO, if there were some way to hook into the actual Error constructor, I could just instantiate a NEW error at that point, and keep it hidden away in a WeakMap so that when I needed to check the stack, I could do with it as I please without affecting the real one.

jgr...@chromium.org

unread,
Jan 10, 2017, 2:55:34 AM1/10/17
to v8-users
On Friday, December 30, 2016 at 5:05:22 PM UTC+1, Francisco Tolmasky wrote:
I would like some way to hook into whenever the Error contractor is called. I'll let you know what I'm doing in case there is some other way to accomplish this, but basically I am trying to get access to the actual stack trace for Error objects, without breaking the stack property. I've tried doing an object->Clone() on the error, setting a custom Error.prepareStackTrace, calling stack, then resetting Error.prepareStackTrace, and this almost works, but on some versions of chrome this still breaks the original error:


    Error.prepareStackTrace = function(e, s) { stack = s };
    clone = clone(error);
    Error.prepareStackTrace = function() { } // fine in v8 used in node 5, 0.12, and 0.10, but breaks in node 6

How does the original error break? Do you have a more complete code example to reproduce this, and could you perhaps open a bug report at crbug.com/v8/new?
 

SO, if there were some way to hook into the actual Error constructor, I could just instantiate a NEW error at that point, and keep it hidden away in a WeakMap so that when I needed to check the stack, I could do with it as I please without affecting the real one.


I'm afraid I don't have any great suggestions for this use case. One approach could be to subclass error, but that only works assuming you're interested in your own errors.
Reply all
Reply to author
Forward
0 new messages