ratWonder
asked on
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('l oad',myFun ction,fals e);
else if(window.attachEvent) window.attachEvent('onload ',myFuncti on);
---
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('l oad',myFun ction,fals e);
else if(window.attachEvent) window.attachEvent('onload ',myFuncti on);
---
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.
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
else if(window.attachEvent) window.attachEvent('onload
---
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
else if(window.attachEvent) window.attachEvent('onload
---
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.
Sounds like a known issue - I have subscribed to this question. Hope you find a way.
ASKER
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?
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?
ASKER
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!!
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
}
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!!
ASKER
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',doEr ror,false) }
else if(window.attachEvent) {window.attachEvent('onloa d',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?
--------
function throwError(error) {
window.setTimeout(function
}
function doError() {
var error = new Error('a really really big error');
if(document.all) {throw error}
else {throwError(error)}
}
if(window.addEventListener
else if(window.attachEvent) {window.attachEvent('onloa
--------
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?
Be sure to keep us posted.
You can ask for a PAQ and refund in community support
You can ask for a PAQ and refund in community support
ASKER
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?
What do I do with a question that I answered myself?
You wait 4 days and it will be PAQ'd (saved for the future) and your points will be refunded
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
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
ASKER
Thanks guys.
PAQ sounds fine.
Robin.
PAQ sounds fine.
Robin.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.