Bug Tracker

Opened 12 years ago

Closed 12 years ago

#2471 closed feature (wontfix)

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;

};

Change History (1)

comment:1 Changed 12 years ago by flesler

Resolution: wontfix
Status: newclosed
Type: enhancementfeature

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.

Note: See TracTickets for help on using tickets.