Skip to content

Commit

Permalink
Allow error handlers in ajax requests (#1032)
Browse files Browse the repository at this point in the history
  • Loading branch information
snapwich authored and matthewlane committed Mar 24, 2017
1 parent fc9899b commit f7216e1
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
29 changes: 27 additions & 2 deletions src/adapters/rubicon.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,29 @@ function RubiconAdapter() {
try {
// Video endpoint only accepts POST calls
if (bid.mediaType === 'video') {
ajax(VIDEO_ENDPOINT, bidCallback, buildVideoRequestPayload(bid, bidderRequest), {withCredentials: true});
ajax(
VIDEO_ENDPOINT,
{
success: bidCallback,
error: bidError
},
buildVideoRequestPayload(bid, bidderRequest),
{
withCredentials: true
}
);
} else {
ajax(buildOptimizedCall(bid), bidCallback, undefined, {withCredentials: true});
ajax(
buildOptimizedCall(bid),
{
success: bidCallback,
error: bidError
},
undefined,
{
withCredentials: true
}
);
}
} catch(err) {
utils.logError('Error sending rubicon request for placement code ' + bid.placementCode, null, err);
Expand All @@ -85,6 +105,11 @@ function RubiconAdapter() {
}
}

function bidError(err, xhr) {
utils.logError('Request for rubicon responded with:', xhr.status, err);
addErrorBid();
}

function addErrorBid() {
let badBid = bidfactory.createBid(STATUS.NO_BID, bid);
badBid.bidderCode = bid.bidder;
Expand Down
30 changes: 24 additions & 6 deletions src/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const XHR_DONE = 4;
* Note: x-domain requests in IE9 do not support the use of cookies
*
* @param url string url
* @param callback object callback
* @param callback {object | function} callback
* @param data mixed data
* @param options object
*/
Expand All @@ -20,6 +20,19 @@ export function ajax(url, callback, data, options = {}) {
let useXDomainRequest = false;
let method = options.method || (data ? 'POST' : 'GET');

let callbacks = typeof callback === "object" ? callback : {
success: function() {
utils.logMessage('xhr success');
},
error: function(e) {
utils.logError('xhr error', null, e);
}
};

if(typeof callback === "function") {
callbacks.success = callback;
}

if (!window.XMLHttpRequest) {
useXDomainRequest = true;
} else{
Expand All @@ -32,23 +45,28 @@ export function ajax(url, callback, data, options = {}) {
if (useXDomainRequest) {
x = new window.XDomainRequest();
x.onload = function () {
callback(x.responseText, x);
callbacks.success(x.responseText, x);
};

// http://stackoverflow.com/questions/15786966/xdomainrequest-aborts-post-on-ie-9
x.onerror = function () {
utils.logMessage('xhr onerror');
callbacks.error("error", x);
};
x.ontimeout = function () {
utils.logMessage('xhr timeout');
callbacks.error("timeout", x);
};
x.onprogress = function() {
utils.logMessage('xhr onprogress');
};
} else {
x.onreadystatechange = function () {
if (x.readyState === XHR_DONE && callback) {
callback(x.responseText, x);
if (x.readyState === XHR_DONE) {
let status = x.status;
if(status >= 200 && status < 300 || status === 304) {
callbacks.success(x.responseText, x);
} else {
callbacks.error(x.statusText, x);
}
}
};
}
Expand Down
12 changes: 12 additions & 0 deletions test/spec/adapters/rubicon_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,18 @@ describe('the rubicon adapter', () => {
expect(bids[0].getStatusCode()).to.equal(CONSTANTS.STATUS.NO_BID);
});

it('should handle error contacting endpoint', () => {
server.respondWith([404, {}, ""]);

rubiconAdapter.callBids(bidderRequest);

server.respond();

expect(bidManager.addBidResponse.calledOnce).to.equal(true);
expect(bids).to.be.lengthOf(1);
expect(bids[0].getStatusCode()).to.equal(CONSTANTS.STATUS.NO_BID);
});

it('should not register an error bid when a success call to addBidResponse throws an error', () => {

server.respondWith(JSON.stringify({
Expand Down

0 comments on commit f7216e1

Please sign in to comment.