Bug Tracker

Ticket #2471 (closed feature: wontfix)

Opened 9 months ago

Last modified 7 months ago

try catch abstraction proposal

Reported by: frink Assigned to: anonymous
Type: feature Priority: minor
Milestone: Component: core
Version: Keywords: try catch trycatch developer tools
Cc: Needs: Review

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

Change History

Changed 7 months ago by flesler

  • status changed from new to closed
  • type changed from enhancement to feature
  • resolution set to wontfix

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.