Skip to content

jQuery Plugin: Wrap jqXHR promise callbacks #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions plugins/jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,19 @@ $.fn.ready = function ravenjQueryReadyWrapper(fn) {

var _oldAjax = $.ajax;
$.ajax = function ravenAjaxWrapper(url, options) {
var keys = ['complete', 'error', 'success'], key;
var jqXHR;

function wrapObjectKeys(obj, keys) {
var key;

/*jshint -W084*/
while (key = keys.pop()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well inline the var key = keys.pop() here now.

if ($.isFunction(obj[key])) {
obj[key] = Raven.wrap(obj[key]);
}
}
/*jshint +W084*/
}

// Taken from https://github.com/jquery/jquery/blob/eee2eaf1d7a189d99106423a4206c224ebd5b848/src/ajax.js#L311-L318
// If url is an object, simulate pre-1.5 signature
Expand All @@ -56,16 +68,16 @@ $.ajax = function ravenAjaxWrapper(url, options) {
// Force options to be an object
options = options || {};

/*jshint -W084*/
while(key = keys.pop()) {
if ($.isFunction(options[key])) {
options[key] = Raven.wrap(options[key]);
}
}
/*jshint +W084*/
// wrap options callbacks
wrapObjectKeys(options, ['complete', 'error', 'success']);

try {
return _oldAjax.call(this, url, options);
// call _oldAjax and store jqXHR
jqXHR = _oldAjax.call(this, url, options);
// wrap jqXHR promise handlers
wrapObjectKeys(jqXHR, ['done', 'success', 'fail', 'error', 'always', 'then', 'complete']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this part to outside the try...catch ?

I don't want to swallow an exception caused by wrapObjectKeys or something. Just bad practice since we only want to capture the exception from _oldAjax.call

// return jqXHR
return jqXHR;
} catch (e) {
Raven.captureException(e);
throw e;
Expand Down