2

I would like to check if a certain console error has occurred using javascript, and alert() myself if it has.

The error will look like this:

00:00:34:0359 TimeEvent.COMPLETE
    (anonymous function) @ VM17617:1

And the algorithm will look something like this:

function checkError(console) {
    if(console.error === "TimeEvent.COMPLETE") {
        alert("The error is present");
    }
}

I'm not very familiar with the console, and haven't gotten much further with Google research. Can somebody point me in the right direction?

JSW189
  • 6,267
  • 11
  • 44
  • 72
  • Wouldn't it be better to try to hook into the actual event that triggers the error? – Jan Jun 29 '15 at 00:37
  • @Jan: That was my original plan but I can't figure that out either. This is the event: `try { __flash__toXML(console.error("00:02:30:0596 TimeEvent.COMPLETE")) ; } catch (e) { "" + e + ""; }` Any ideas? – JSW189 Jun 29 '15 at 00:50
  • That has to reside within a function or method somewhere. You could get the method, inject something into it and then overwrite it with your own logging. – Jan Jun 29 '15 at 00:54

2 Answers2

4

I ultimately solved my question by following this blog post on taking over the console with javascript.

Here is my final code:

var original = window.console
window.console = {
    error: function(){      

        //Gets text from error message.
        errorText = arguments['0'];

        if (errorText.includes('TimeEvent.COMPLETE')) {
            //DO STUFF HERE
        }

        original.error.apply(original, arguments)
    }
}
JSW189
  • 6,267
  • 11
  • 44
  • 72
0

You didn't provide the whole picture about how and when the console is getting the error. If you raise the error yourself, or if you are able to catch it inside a try catch, that would be the best place to intercept those errors.

However, if you have no control about how those error are raised, you should try to intercept your console's error calls. I never tried it myself but this SO answer explains how to intercept the console's log calls. Knowing that the console usually have a function named error that is similar to the log function, I'm sure you could apply the same logic to intercept the errors sent to the console.

If you are using chrome, you may refer to the console documentation for more details about the error function. I'm not sure if there's a standard butInternet Explorer and Firefox also has support for console error function.

Community
  • 1
  • 1
The_Black_Smurf
  • 5,178
  • 14
  • 52
  • 78