Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get the local variable and parameter values with window.onerror

I have a simple javascript error logging mechanism in place and it looks somewhhat like this:

window.onerror = function (ErrorMsg, Url, LineNumber, Col, Error) {
// ajax these to the server, including Error.stack}

The problem is that I'd also like to get the value of the local variables and function parameters when the error occurred. Is this even possible?

I'm thinking about modifying the Function prototype so that each time a function runs, its arguments are stored in a global array of strings and then the error handler would just add this array to the ajax call. Can JavaScript do this?

like image 500
frenchie Avatar asked Dec 04 '15 17:12

frenchie


People also ask

What is window Onerror?

Introduction. onerror is a DOM event handler. It is started when an error occurs during object loading. While window. onerror is an event handler, there is no error event being fired: instead, when there is an uncaught exception or compile-time error, the window.


1 Answers

#1 Can local scope be recovered in onerror() without black magic?

Without this being bound in the scope of window.onerror() or the surrounding variables being directly accessible, it's impossible to regain access to the variables you had set.

What you're mostly wanting access to is this.arguments or arguments or the equivalent, but that's destroyed. Any hope of obtaining a key-value associative array or hash-like object would involve meta-programming ( i.e. reading the function definition to obtain the variable names, and obtaining an exception report to attempt to salvage data ).

See this answer for more on something similar:

  • Getting All Variables In Scope

But this "lacking functionality" is a good thing:

If you could gain access to what you're asking for, that would likely be a fault in the Javascript engine. Why? Because the variable states and contents themselves are what caused the exception/error, assuming bad code wasn't the issue to begin with.

In other words, if you could get access to a faulty variable, that might be a door into an infinite loop:

  1. Failure due to variable contents.
  2. Error handler triggered.
  3. Trace contents of variable.
  4. Failure due to variable contents.
  5. Error handler triggered.
  6. Trace contents of variable.
  7. Etc.

#2 Can Javascript store all arguments of every function call by voodoo?

Yes. It can. This is probably a really bad idea ( see #1 ) but it is possible. Here is a pointer on where to start:

  • Is there a way to wrap all JavaScript methods with a function?

From there, what you're wanting to do is push this.arguments or equivalent to a stack of function calls. But again, this is approaching insanity for many reasons. Not the least of which is the need to duplicate all the values, lest you reference mutated variables, or be unable to access the data at all... and like I said above, the problem of bad data in general. But still, it is possible.

like image 154
digitalextremist Avatar answered Oct 22 '22 04:10

digitalextremist