Side navigation
#2471 closed feature (wontfix)
Opened March 07, 2008 05:58AM UTC
Closed May 12, 2008 01:02AM UTC
try catch abstraction proposal
Reported by: | frink | Owned by: | |
---|---|---|---|
Priority: | minor | Milestone: | |
Component: | core | Version: | |
Keywords: | try catch trycatch developer tools | Cc: | |
Blocked by: | Blocking: |
Description
We still have to write try/catch statements without a jQuery construct. It would make more sense to use an abstraction function to do this. It would reduce the code size for plugins but also use console.log() correctly.
You would use as follows:
try script and log errors if possible
$.try(function() { error prone code });
try script and run a custom function on error
$.try(function() { error prone code }, function(error) { alert(error); });
//try and run a custom script on success to filter return value
$.try(function() {
return 'success';
}, function(r) {
if(r == 'success')
alert('I succeed!');
}, function(error) {
alert(error);
});
The console error still give the correct line numbers in Firebug but you reduce the extra code you have to write when you want to try some experimental code.
Here is the code for this:
$.try = function(testfn, okfn, errfn) {
if(!errfn) {
errfn = okfn;
okfn = null;
}
var r, rr;
try {
r = testfn();
} catch(error) {
if(window.console && window.console.log)
console.log(error);
if(errfn)
return errfn(error);
return error;
}
if(okfn)
rr = okfn(r);
if(typeof rr != 'undefined')
return rr;
return r;
};
Attachments (0)
Change History (1)
Changed May 12, 2008 01:02AM UTC by comment:1
resolution: | → wontfix |
---|---|
status: | new → closed |
type: | enhancement → feature |
This can make a great plugin, but it's not a functionality for the core.
jQuery doesn't devote much code to error handling, and this approach is actually more verbose than a normal try/catch.