[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 418
  • Last Modified:

Throwing JS errors inside event listeners - FireFox

Hiya guys.
I'm trying to throw errors in on of my functions. I am using this syntax:

---
var myFunction = function() {throw new Error('a new error')}
myFunction();
---

This works in both FireFox and IE. In FireFox JS console if gives you a nice pretty error with the line number and a reference to where the error occurred in the document.

However, if you try to throw this error from inside an event listener:

---
var myFunction = function() {throw new Error('a new error')}
if(window.addEventListener) window.addEventListener('load',myFunction,false);
else if(window.attachEvent) window.attachEvent('onload',myFunction);
---

Then while it behaves exactly the same in IE, in FireFox you get an ugly "[Exception... "'Error: a new error' when calling method: " style error message. This offends me, I want the nice pretty and functional error message.

It must be possible, because if I generate a default error, e.g.:

---
var myFunction = function() {alert(afgad.adfg)}
if(window.addEventListener) window.addEventListener('load',myFunction,false);
else if(window.attachEvent) window.attachEvent('onload',myFunction);
---

It works properly again.

Does anyone know if there is a technique for throwing this error properly from within an event listener for FireFox?

Thanks.
Robin.
0
ratWonder
Asked:
ratWonder
1 Solution
 
Michel PlungjanIT ExpertCommented:
Sounds like a known issue - I have subscribed to this question. Hope you find a way.
0
 
ratWonderAuthor Commented:
If it's a known issue, I don't understand why they haven't fixed it. Surely it couldn't be that hard?

I posted on the mozilla developer message board. http://groups.google.com/group/mozilla.dev.tech.javascript/topics. No reply yet.

Anyone else have a clue?
0
 
ratWonderAuthor Commented:
I've found a way around this!!

If you throw the error inside a window.setTimeout() then it works properly! I assume because it's now "outside" the event, as far as mozilla is concerned.

so:
------------
function throwError(error) {
    window.setTimeout(function() {throw error});
}

window.addEventListener(
    'load',
    function() {
        throwError(new Error('a really really big error'))
    },
    false
);
-------------

NB: It's important NOT to initiate the error inside the function itself, because the line number is wherever the error is initiated. So if it was inside the function you would never know where the error actually was called from.

And it works!! I'm so happy!!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ratWonderAuthor Commented:
Oh bollox. In IE it still says the line where the error is thrown from (line 2 in this case) rather than where the error is created. So I guess you're gonna have to browser sniff before that (hate browser sniffing):

--------
function throwError(error) {
    window.setTimeout(function() {throw error});
}
function doError() {
    var error = new Error('a really really big error');
    if(document.all) {throw error}
    else {throwError(error)}
}
if(window.addEventListener) {window.addEventListener('load',doError,false)}
else if(window.attachEvent) {window.attachEvent('onload',doError);}
--------

Now I'm wondering if there's any way around this so you can just call one function - nice and neat. Anyone know of a way to either get the current line number, or make the IE error correspond to where the error object was initialised or something?
0
 
Michel PlungjanIT ExpertCommented:
Be sure to keep us posted.
You can ask for a PAQ and refund in community support
0
 
ratWonderAuthor Commented:
I think this is about as close as I could get. to finding the answer. And anything I did find out was found out on my own.

What do I do with a question that I answered myself?
0
 
Michel PlungjanIT ExpertCommented:
You wait 4 days and it will be PAQ'd (saved for the future) and your points will be refunded
0
 
b0lsc0ttCommented:
ratWonder,

As Mplungjan mentioned PAQ means this will be saved.  It will become a Previously Asked Question (i.e. PAQ).  Since you did answer this yourself the points will even be refunded.  That was the recommendation I made above and a moderator will act on it after at least 4 days.  There is information on this and other closing options in the Help section of the site if you would like more info.  I hope our comments clear up any confusion.  Let me know if you have a question about something I said.  Good job on the solution and thanks for posting the info so this could be kept.

b0lsc0tt
EE Cleanup Volunteer
0
 
ratWonderAuthor Commented:
Thanks guys.

PAQ sounds fine.

Robin.
0
 
Computer101Commented:
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now