Search code examples
javascripteventsfirefoxthrow

catching error event message from firefox


I can't find a way to catch the error message under firefox:

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

...

function handleException(e) {   
    alert(e);
    return false;
}

...

<script>
throw new Error('sdasd');
</script>

This enters very well the handleException method however the e parameter is an error event under firefox and I don't know how to get the associated message. In chrome for instance, I get either the message through e.message because after the error bubbles up to not being caught, there's an automatic error fired at window level (See this fiddle: the final error is "Uncaught") that contains the original error that I raised manually.

So to have the same behaviour under firefox (if you run the fiddle under firefox you'll see that the message is "undefined") I found a workaround consisting in encapsulating an error raising function to setup a manual "last error" architecture:

function err(I_sText) { 
    g_lastManualError = new Error(I_sText);
    throw g_lastManualError; //this variable is global so I can get the message from anywhere
}

So instead of doing throw new Error(..) I only call err(..). That works, at least for user defined exceptions, which are my biggest concern. In my handler handleException I'm consulting the global variable.

Do you know how I could do otherwise? I'm not happy with this solution.

Thank you, S.


Solution

  • I modified your code a little as a demo:

    function handleException(e) { 
        console.log(e);
        alert(e);
        return false;
    }
    window.addEventListener("error", handleException, false);
    
    try {
      throw new Error('sdasd');
    }
    catch (e) {
      console.log(e)
    }
    console.log('after exception 1');
    throw new Error('foo');
    console.log('after exception 2');
    

    Running this code (in Firebug) showed me this:

    Error: sdasd
    [Break On This Error]   
    
    Filtered chrome url chrome://firebug/content/console/commandLineExposed.js
    
    comman...osed.js (line 175)
    <System>
    
    after exception 1
    
    "Error: foo `    throw new Error('foo');` @14"
    

    If you're trying to catch an error, use try {...} catch { ...}. It looks like you're just binding to an error event, so the exception you're throwing will still propagate up to window and tell the JS engine to halt. Run this code in Chrome, you'll see that you never see "after exception 2", but you will see "after exception 1".

    The purpose of exceptions (created by throw) is to stop code execution unless there's code made to handle that particular exception. You can see an example on the MDN page for try-catch

    Edit: it just occurred to me that you might be trying to catch a jQuery error event. If this is the case, your event binding is correct but you shouldn't be testing it with throw

    Edit 2: I should've noticed this sooner, but you're trying to listen for a DOM error event with window.addEventListener. Error events will not break execution. Exceptions do.

    Replace your code throw new Error('sdasd'); with this code:

    var customEvent = new CustomEvent('error')
    
    window.dispatchEvent(customEvent);
    

    That should solve your problem. I used the MDN page on custom events as a reference.