5

I'm capturing js errors in the application with window.onerror, but the thing is - in Chrome if dev tools isn't opened - then the url parameter passed to onerror handler always equals to the opened url.

While if dev tools is opened - then the url points to the exact .js file the caused the js error.

How do you deal with it? Are there any workarounds?

And to be more clear - here are 2 results:

  1. Uncaught ReferenceError: a is not defined index:122 - this was received after fetching a page
  2. Uncaught ReferenceError: a is not defined List.js:122 - this was received after fetching the same page with dev tools opened. This is an expected result - I've put a(); call to the List.js file for testing.

UPD: this is done for functional testing (using selenium webdriver) - I want to capture js errors for further investigations.

2
  • If you're looking for an exception that needs to be fixed, I'd say to open the dev tools' Sources tab and then use the "Pause on all/uncaught exceptions" in the bottom left of the panel. Commented Apr 10, 2013 at 21:42
  • 1
    @Fabrício Matté: it's for functional testing - I'd like to capture js errors for further investigation. Sorry I didn't mention it initially Commented Apr 10, 2013 at 21:43

2 Answers 2

1

Let's pose the following architecture:

window.addEventListener("error", handleException, false);

function handleException(I_sMsg) {

    if (I_sMsg.stack) {
            sMsg = I_sMsg.stack.replaceAll(getBaseURL(), "");
        alert(sMsg);
    } else if (I_sMsg.message) {
        alert(I_sMsg.message);
    }   

    return cancelEvent(I_sMsg);
} 

Now any throw new Error("description"); will go through the first part of the if statement and have a nice stack for you to parse with the urls.

It also works for unexpected exceptions, having as a result the following message (in this case after calling the unexisting bibi() function)

screenshot of an unexpected exception

After further investigation, my framework is using some kind of home made job management (as shown in the stack actually) where every single action belongs to a job.

The job execution method is the following (simplified)

    try {
        oTask.func.apply(oTask.obj, oTask.prms);
    } catch(ex) {
        handleException(ex);
        return false;
    }

So it means every single execution is encapsulated within this single try catch block. As you see, the exception is caught, and passed to the handler. Not the error.

I though it was working in the other file but it was because the call was encapsulated, while within the api.js file directly it was a free call not managed by the framework.

Sign up to request clarification or add additional context in comments.

15 Comments

It's not actually about application level error handling, but about unexpected js errors, like calling non-functions or accessing properties of undefined variables.
yes, it works as well. I'm going to add a screenshot in 2 minutes
that would be nice, since for my example with calling non existing a() function it doesn't give anything new
edited. Please note you don't see the full URL because the handleException I am using truncates it. But you have the full URI which is I believe what you asked for.
Yes, for the async operations I also made a specific manager which handles this situation. ;)
|
1

More of a something to try answer really but it might help.

Chrome recently added chrome://inspect/ to the list of handy URLs (see chrome://chrome-urls/ for the complete list). I cannot find the tweet or blog post I read about this unfortunately but I think it was within the last month. The URL works on Chrome 28 for sure.

chrome://inspect/ lists all open tabs with an inspect link which redirects back to the existing open page but also opens DevTools.

I'm thinking that the selenium test could open the site under test in one tab and in a second tab open the inspect page, follow the inspect link back to the test page but this time with DevTools open, allowing window.onerror to capture better errors.

Something like:

document.getElementsByClassName('row')[n].getElementsByTagName('a')[0].click()

1 Comment

"and in a second tab open the inspect page" --- I'm not sure, but if it could - then this might be a solution, thanks :-)

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.