I'd like to hear your opinions about the appropriate way to deal with
non-critical errors that can occur in user-defined functions. For
example, an application chooses to extend String.prototyp e with a
format() method similar to sprintf(). Several formats (%d, %f, etc) are
supported, but %z would be invalid. During the parsing of the format
string, the function encounters an invalid format which (most likely)
was caused by a programmer error.
Should it:
- try a best-guess resolution and continue?
- fail silently (returning an empty or partial result)?
- throw an exception? If so, which one (a string, a user-defined object,
one of the built-in Exception objects)?
- display an alert()?
- do something else?
The target clients are modern browsers, and exceptions are available.
At the moment, I'm using a mix of the above; something similar to this
(reduced example):
function warn (msg) {
if (window.console && typeof window.console. error == "function") {
// If the Firebug console is available, display the error, but
// allow the calling function to continue
window.console. error(msg);
} else {
throw new MyCustomExcepti on(msg);
}
}
but I'm not happy with it, because the program flow is different
depending on the availability of Firebug. The format() method may want
to signal a non-critical error, but still try to continue. I guess what
I'm looking for is a way to issue warnings without Firebug. I'd rather
not use alert(), because there could be a large number of warnings when
things go wrong.
- Conrad
non-critical errors that can occur in user-defined functions. For
example, an application chooses to extend String.prototyp e with a
format() method similar to sprintf(). Several formats (%d, %f, etc) are
supported, but %z would be invalid. During the parsing of the format
string, the function encounters an invalid format which (most likely)
was caused by a programmer error.
Should it:
- try a best-guess resolution and continue?
- fail silently (returning an empty or partial result)?
- throw an exception? If so, which one (a string, a user-defined object,
one of the built-in Exception objects)?
- display an alert()?
- do something else?
The target clients are modern browsers, and exceptions are available.
At the moment, I'm using a mix of the above; something similar to this
(reduced example):
function warn (msg) {
if (window.console && typeof window.console. error == "function") {
// If the Firebug console is available, display the error, but
// allow the calling function to continue
window.console. error(msg);
} else {
throw new MyCustomExcepti on(msg);
}
}
but I'm not happy with it, because the program flow is different
depending on the availability of Firebug. The format() method may want
to signal a non-critical error, but still try to continue. I guess what
I'm looking for is a way to issue warnings without Firebug. I'd rather
not use alert(), because there could be a large number of warnings when
things go wrong.
- Conrad
Comment