473,473 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

window.onerror and XMLHTTPRequest

I'm working on a fairly sizeable javascript application. I recently
added some error-handling to it, using window.onerror to catch them.
This sometimes works and sometimes doesn't; if I introduce a deliberate
mistake "near the top" of the system then it's trapped and handled
properly, but there's lots of code where mistakes only show up in my
Firebug console.

My first thought is that, since a lot of the work happens in callbacks
after an XMLHTTPRequest, this somehow puts the error handler out of
scope. However, logging "window" to my Firebug console, in such a
callback, just before deliberately invalid code, shows that it has a
suitable onerror function but that it is not called. Does anyone know
why this might be?

Thanks,

Pete
Dec 15 '06 #1
5 2908
VK

Pete Verdon wrote:
I'm working on a fairly sizeable javascript application. I recently
added some error-handling to it, using window.onerror to catch them.
This sometimes works and sometimes doesn't; if I introduce a deliberate
mistake "near the top" of the system then it's trapped and handled
properly, but there's lots of code where mistakes only show up in my
Firebug console.

My first thought is that, since a lot of the work happens in callbacks
after an XMLHTTPRequest, this somehow puts the error handler out of
scope. However, logging "window" to my Firebug console, in such a
callback, just before deliberately invalid code, shows that it has a
suitable onerror function but that it is not called. Does anyone know
why this might be?
Most probably because you have a wrong idea of what does onerror
handler do.
window.onerror is a very primitive way to do something (say inform
user) *after* the execution broke because of an error. It was widely
used at old times (before try-catch) only because of the lack of
anything better.

Let me stress it once again: the execution breaks first, window.onerror
is called only after. You can return true from the handler, but it has
nothing to do with VB-like OnError Resume Next or similar.
It simply prevents the error notification to appear in the browser
statusbar (where such behavior exists). This way
window.onerror = function(){return true;};
// prevent visual error notification
is the only task window.onerror can be really used.

If you want an exception handling in your program (with possible
execution continued after the exception handled) then try-catch blocks
are the only way.

Dec 15 '06 #2
VK wrote:
Pete Verdon wrote:
>I'm working on a fairly sizeable javascript application. I recently
added some error-handling to it, using window.onerror to catch them.
This sometimes works and sometimes doesn't; if I introduce a deliberate
mistake "near the top" of the system then it's trapped and handled
properly, but there's lots of code where mistakes only show up in my
Firebug console.
Let me stress it once again: the execution breaks first, window.onerror
is called only after. You can return true from the handler, but it has
nothing to do with VB-like OnError Resume Next or similar.
It simply prevents the error notification to appear in the browser
statusbar (where such behavior exists). This way
window.onerror = function(){return true;};
// prevent visual error notification
is the only task window.onerror can be really used.

If you want an exception handling in your program (with possible
execution continued after the exception handled) then try-catch blocks
are the only way.
I think you have misunderstood what I am trying to do. I am only trying
to detect when an error has occurred, not respond to it. While it would
be nice to have everything properly contained in try/catch blocks and
handle exceptions in a sensible way, there are thousands of lines of
legacy javascript horribly interspersed with mangled HTML. There isn't
any realistic way to continue execution after something breaks somewhere
in the pile. All I want to do is notify the user, in a way they will
understand, that something has failed and that any further activity is
on a best-effort basis only (together with instructions on restarting
the application if they prefer). I also log the error so that I can
investigate later. This error-handling works fine when it is called. My
problem is that in about 50% of cases, it's not called at all. I am
trying to find out why.

Pete

Dec 18 '06 #3
VK

Pete Verdon wrote:
I think you have misunderstood what I am trying to do. I am only trying
to detect when an error has occurred, not respond to it.
Oh, I see, sorry then.
This error-handling works fine when it is called. My
problem is that in about 50% of cases, it's not called at all. I am
trying to find out why.
Did you check all involved procs if they have try-carch blocks? Some
libraries have a bad habit to die "in the proud solitude" w/o
re-throwing the catch.

You can also get fancy effects if ajaxing with (i)frames, because here
the final destination - window - can be separate for each frame.

Just two blind shots, so sorry if missed.

Dec 18 '06 #4
VK wrote:
Pete Verdon wrote:
>This error-handling works fine when it is called. My
problem is that in about 50% of cases, it's not called at all. I am
trying to find out why.
Did you check all involved procs if they have try-carch blocks? Some
libraries have a bad habit to die "in the proud solitude" w/o
re-throwing the catch.
Ah, that's an interesting possibility. I wouldn't have expected the kind
of code I'm looking at to have used catch blocks at all, but I suppose
it might have done. There could also be places where my new code is
catching more than it should, too :-)
You can also get fancy effects if ajaxing with (i)frames, because here
the final destination - window - can be separate for each frame.
I do indeed make significant use of iframes. I *thought* I had an
appropriate handler in the frames, but it's worth looking at more closely.
Just two blind shots, so sorry if missed.
You've given me two good places to look, so thanks very much.

Pete
Dec 19 '06 #5
Pete Verdon wrote:
VK wrote:
>Did you check all involved procs if they have try-carch blocks? Some
libraries have a bad habit to die "in the proud solitude" w/o
re-throwing the catch.

Ah, that's an interesting possibility. I wouldn't have expected the kind
of code I'm looking at to have used catch blocks at all, but I suppose
it might have done. There could also be places where my new code is
catching more than it should, too :-)
It seems I do have a couple of places with, perhaps, some overzealous
exception catching. But it occurs to me that, if the errors were showing
up in my Firebug console, they could not have been caught by a catch
block. Is my thinking correct in that?

Pete
Dec 19 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jürgen Heyn | last post by:
Good afternoon, I am handing a string as a parameter pImage = "Images/Available.jpg" If this image is NOT available I would like to load Images/NoImage.jpg. Unfortunately the following code...
2
by: Tee | last post by:
what's the differences between "Overrides Sub OnError" and "Sub Page_Error" ?
35
by: eyoung | last post by:
I call a function that takes the unit price and quantity ordered to create an amount...it looks something like this. function calculateCost() { quantity=document.RFO.quantity.value;...
1
by: Jim Davis | last post by:
I've been happily using a custom error handler of the following form for a while now: window.onerror = function(Message, URL, Line) { ... } The current case is an intranet application (support...
2
by: yawnmoth | last post by:
Say I have the following script: <script> var xmlHttp = false; // http://blogs.msdn.com/ie/archive/2006/01/23/516393.aspx if (window.XmlHttpRequest) { alert("part 1"); xmlHttp = new...
2
by: crazydave | last post by:
Hi, I've been using window.onerror to capture and report JavaScript errors from other users for debugging an application I've written. But I've run into a strange issue with Firefox and...
3
by: jaynick | last post by:
Hello, everyone! I'm hoping that someone in this forum will be kind enough to help me out. I am having a Javascript issue related to Ajax and I just can't seem to figure it out. I am currently...
1
by: soms2m | last post by:
HELLO ALL, I want to fill the parent window height with respect to the sub window height which is loading using ajax (mootools). For example if the parent window height is normal and the loading...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.