473,769 Members | 1,723 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Javascript error checking

All right, since my previous idea (calling functions through a
wrapper) was apparently so awful no one could suggest any
improvements, I'll try a different tack. My end goal is to make
detecting and reporting script errors easy and painless. This could
be accomplished easily if window.onerror were ubiquitous, but it is
not. My new idea is to hack together something superficially
resembling C's assert() macro, perhaps something like

function assert( expr_as_string, cond ) {
var result=eval( expr_as_string );
if( cond == null ) {
cond=true;
}
if( result != cond ) {
alert( 'Assertion failed: '+expr_as_strin g+'=='+result+' , expected
'+cond );
return false;
}
return true;
}

which would be used like

assert( '5 != 3' ); // true
assert( '5*10 == 3' ); // false

Now obviously this is, again, probably unacceptably obfuscatory.
That's why I'm posting and hoping, desperately, for suggested
improvements and/or other ideas. Surely given the amount of script
that's out there someone has conceived a half-reasonable error checking
framework...

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Oct 5 '05 #1
17 2734
Surely given the amount of script
that's out there someone has conceived a half-reasonable error checking
framework...


Sure they have.
Firefox has Venkman and the javascript console, and MSIE has a windows
javascript debugger whose name I forget.

That (good testing using these tools) and try/catch and good coding
with checks (i.e. checking for object before using it) should handle
pretty much anything that comes up, really.

My 2 cents, of course.

Oct 5 '05 #2
I would agree with nikki.

I have written an application running to some 1MB (60,000 lines) of
Javascript, with only object/property checking (and a single try catch
for file saving) , with no problems, so I struggle to see the uses of
an error checking framework. Could you give some examples of
applications you would use it in?

Oct 5 '05 #3
Baconbutty <ju****@baconbu tty.com> wrote:
I have written an application running to some 1MB (60,000 lines) of
Javascript, with only object/property checking (and a single try catch
for file saving) , with no problems, so I struggle to see the uses of
an error checking framework. Could you give some examples of
applications you would use it in?


So the 60,000 lines of script never, ever has unexpected bugs? I know
our script is written and tested by humans and occasionally bugs creep
in. If a Safari user encounters one of these bugs, how do you find
out about it? I'm really struggling to understand how you avoid ever
making a mistake in 60,000 lines of script.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Oct 5 '05 #4
Lee
Christopher Benson-Manica said:

All right, since my previous idea (calling functions through a
wrapper) was apparently so awful no one could suggest any
improvements , I'll try a different tack. My end goal is to make
detecting and reporting script errors easy and painless. This could
be accomplished easily if window.onerror were ubiquitous, but it is
not. My new idea is to hack together something superficially
resembling C's assert() macro, perhaps something like


An important feature of assert() is that it can be compiled
out of the code for production release. Also, your assert()
will only be useful when checking global variables, since
variable names local to the calling function

Why not just add reasonable error checking to the code where
appropriate?

function myLibraryFuncti on(id, size, shape) {
if(size<MIN_SIZ E || size>MAX_SIZE || !validShape["SHAPE_"+sh ape]) {
alert(...)
return;
}
var ref=document.ge tElementById(id );
if(ref) {
} else {
alert("invalid id \""+id+"\" passed to myLibraryFuncti on");
}
...
}

Oct 5 '05 #5
Lee
Lee said:

Christopher Benson-Manica said:

All right, since my previous idea (calling functions through a
wrapper) was apparently so awful no one could suggest any
improvement s, I'll try a different tack. My end goal is to make
detecting and reporting script errors easy and painless. This could
be accomplished easily if window.onerror were ubiquitous, but it is
not. My new idea is to hack together something superficially
resembling C's assert() macro, perhaps something like


An important feature of assert() is that it can be compiled
out of the code for production release. Also, your assert()
will only be useful when checking global variables, since
variable names local to the calling function


[continuing that truncated paragraph]

variable names local to the calling function won't be available
in the scope of the eval() expression.

Oct 5 '05 #6
Lee <RE************ **@cox.net> wrote:
An important feature of assert() is that it can be compiled
out of the code for production release. Also, your assert()
will only be useful when checking global variables, since
variable names local to the calling function
Ey-yi-yi... well, so much for THAT plan. I actually don't want it
compiled out for a production release - a bug is a bug, and if the
script on a page is broken our users (and us!) need to know.
Why not just add reasonable error checking to the code where
appropriate?


Well, it seems that there is indeed no other choice, but it sure takes
up a lot of space. Additionally, the error handling is not always the
same, depending the UA (no lectures please) and whether the CGI
application is on a production or test system.

I appreciate the help, however.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Oct 5 '05 #7
In article <11************ *********@z14g2 000cwz.googlegr oups.com>, Baconbutty
says...

I would agree with nikki.

I have written an application running to some 1MB (60,000 lines) of
Javascript, with only object/property checking (and a single try catch
for file saving) , with no problems, so I struggle to see the uses of
an error checking framework. Could you give some examples of
applications you would use it in?


Maybe it's just me but I've been spoiled by the debugger built-in to Perl.
Besides a basic syntax check, which usually causes problems somewhere in 60K
lines of code, you can also step through your script line by line and test
variables to make sure your program is doing what it's suppose to. Would be nice
to have a desktop tool for Javascript to test your scripts through and maybe
check for browser compatibility as a bonus.
Rich
--
Newsguy -- http://newsguy.com

Oct 5 '05 #8
>>So the 60,000 lines of script never, ever has unexpected bugs?
I'm really struggling to understand how you avoid ever

making a mistake in 60,000 lines of script.

Sorry, I am probably not quite understanding the problem you are
seeking to solve, and as I am working alone I don't face the same
problems you may have of managing the inputs from several programmers:
so my example was unfair.

Don't get me wrong, during development there were plenty of bugs and
errors, usually to do with unexpected variable types (Javascript being
loosely typed) or bad logic, which I identified and corrected with the
brute force test and debug approach. .

In this, I tried to identify those functions which could cause damage
if they were the wrong data/or produced the wrong output, or which
relied on something external (e.g. initialising a component), and built
in specific tests, expectations and fall backs. It is here that I
would perhaps include error checking: i.e. mission critical functions.

But apart from that, once tested and debugged, there were relatively
few such functions, and the program in practice has usually failed me
only in very subtle ways to do with the logic of the program, which I
assumed no amount of error catching code could help with.

Oct 6 '05 #9
Christopher Benson-Manica wrote:
My new idea is to hack together something superficially
resembling C's assert() macro, perhaps something like

function assert( expr_as_string, cond ) {
var result=eval( expr_as_string );
if( cond == null ) {
cond=true;
}
if( result != cond ) {
alert( 'Assertion failed: '+expr_as_strin g+'=='+result+' , expected
'+cond );
return false;
}
return true;
}

which would be used like

assert( '5 != 3' ); // true
assert( '5*10 == 3' ); // false


I think what you are trying to accomplish is not bad.
Just like assert in Java it can also be useful in Javascript.
I find assert especially useful to check the pre-conditions of my methods.
I use a validate method myself at the start of a lot of methods to check
the argument types.
This will make the rest of my method body much simpler, because I do not
have to take into account all the ways someone could have called my method.

Anyway, what I do not understand is why you use a string argument for
assert and not something like:

Assert.assertEq uals(5, 3, "error message");

Personally I use a Application.deb ug variable if I don't want to do any
validation for example for production environments. Although I usually
just keep it on, because it can produce some useful stacktraces for me.
Oct 6 '05 #10

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

Similar topics

0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9863
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7408
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.