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?

3
  • Wouldn't it be better to try to hook into the actual event that triggers the error? Commented Jun 29, 2015 at 0: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) { "<exception>" + e + "</exception>"; } Any ideas? Commented Jun 29, 2015 at 0: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. Commented Jun 29, 2015 at 0:54

2 Answers 2

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)
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.