Skip to content

Commit f7216e1

Browse files
snapwichmatthewlane
authored andcommitted
Allow error handlers in ajax requests (#1032)
1 parent fc9899b commit f7216e1

File tree

3 files changed

+63
-8
lines changed

3 files changed

+63
-8
lines changed

src/adapters/rubicon.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,29 @@ function RubiconAdapter() {
6262
try {
6363
// Video endpoint only accepts POST calls
6464
if (bid.mediaType === 'video') {
65-
ajax(VIDEO_ENDPOINT, bidCallback, buildVideoRequestPayload(bid, bidderRequest), {withCredentials: true});
65+
ajax(
66+
VIDEO_ENDPOINT,
67+
{
68+
success: bidCallback,
69+
error: bidError
70+
},
71+
buildVideoRequestPayload(bid, bidderRequest),
72+
{
73+
withCredentials: true
74+
}
75+
);
6676
} else {
67-
ajax(buildOptimizedCall(bid), bidCallback, undefined, {withCredentials: true});
77+
ajax(
78+
buildOptimizedCall(bid),
79+
{
80+
success: bidCallback,
81+
error: bidError
82+
},
83+
undefined,
84+
{
85+
withCredentials: true
86+
}
87+
);
6888
}
6989
} catch(err) {
7090
utils.logError('Error sending rubicon request for placement code ' + bid.placementCode, null, err);
@@ -85,6 +105,11 @@ function RubiconAdapter() {
85105
}
86106
}
87107

108+
function bidError(err, xhr) {
109+
utils.logError('Request for rubicon responded with:', xhr.status, err);
110+
addErrorBid();
111+
}
112+
88113
function addErrorBid() {
89114
let badBid = bidfactory.createBid(STATUS.NO_BID, bid);
90115
badBid.bidderCode = bid.bidder;

src/ajax.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const XHR_DONE = 4;
99
* Note: x-domain requests in IE9 do not support the use of cookies
1010
*
1111
* @param url string url
12-
* @param callback object callback
12+
* @param callback {object | function} callback
1313
* @param data mixed data
1414
* @param options object
1515
*/
@@ -20,6 +20,19 @@ export function ajax(url, callback, data, options = {}) {
2020
let useXDomainRequest = false;
2121
let method = options.method || (data ? 'POST' : 'GET');
2222

23+
let callbacks = typeof callback === "object" ? callback : {
24+
success: function() {
25+
utils.logMessage('xhr success');
26+
},
27+
error: function(e) {
28+
utils.logError('xhr error', null, e);
29+
}
30+
};
31+
32+
if(typeof callback === "function") {
33+
callbacks.success = callback;
34+
}
35+
2336
if (!window.XMLHttpRequest) {
2437
useXDomainRequest = true;
2538
} else{
@@ -32,23 +45,28 @@ export function ajax(url, callback, data, options = {}) {
3245
if (useXDomainRequest) {
3346
x = new window.XDomainRequest();
3447
x.onload = function () {
35-
callback(x.responseText, x);
48+
callbacks.success(x.responseText, x);
3649
};
3750

3851
// http://stackoverflow.com/questions/15786966/xdomainrequest-aborts-post-on-ie-9
3952
x.onerror = function () {
40-
utils.logMessage('xhr onerror');
53+
callbacks.error("error", x);
4154
};
4255
x.ontimeout = function () {
43-
utils.logMessage('xhr timeout');
56+
callbacks.error("timeout", x);
4457
};
4558
x.onprogress = function() {
4659
utils.logMessage('xhr onprogress');
4760
};
4861
} else {
4962
x.onreadystatechange = function () {
50-
if (x.readyState === XHR_DONE && callback) {
51-
callback(x.responseText, x);
63+
if (x.readyState === XHR_DONE) {
64+
let status = x.status;
65+
if(status >= 200 && status < 300 || status === 304) {
66+
callbacks.success(x.responseText, x);
67+
} else {
68+
callbacks.error(x.statusText, x);
69+
}
5270
}
5371
};
5472
}

test/spec/adapters/rubicon_spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,18 @@ describe('the rubicon adapter', () => {
583583
expect(bids[0].getStatusCode()).to.equal(CONSTANTS.STATUS.NO_BID);
584584
});
585585

586+
it('should handle error contacting endpoint', () => {
587+
server.respondWith([404, {}, ""]);
588+
589+
rubiconAdapter.callBids(bidderRequest);
590+
591+
server.respond();
592+
593+
expect(bidManager.addBidResponse.calledOnce).to.equal(true);
594+
expect(bids).to.be.lengthOf(1);
595+
expect(bids[0].getStatusCode()).to.equal(CONSTANTS.STATUS.NO_BID);
596+
});
597+
586598
it('should not register an error bid when a success call to addBidResponse throws an error', () => {
587599

588600
server.respondWith(JSON.stringify({

0 commit comments

Comments
 (0)