Skip to content
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

PBjs core: new BIDDER_ERROR event and onBidderError function called when ajax call fail #7438

Merged
merged 1 commit into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions src/adapterManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -618,4 +618,9 @@ adapterManager.callBidViewableBidder = function(bidder, bid) {
tryCallBidderMethod(bidder, 'onBidViewable', bid);
};

adapterManager.callBidderError = function(bidder, error, bidderRequest) {
const param = { error, bidderRequest };
tryCallBidderMethod(bidder, 'onBidderError', param);
};

export default adapterManager;
7 changes: 4 additions & 3 deletions src/adapters/bidderFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,11 @@ export function newBidder(spec) {

// If the server responds with an error, there's not much we can do. Log it, and make sure to
// call onResponse() so that we're one step closer to calling done().
function onFailure(err) {
function onFailure(errorMessage, error) {
onTimelyResponse(spec.code);

logError(`Server call for ${spec.code} failed: ${err}. Continuing without bids.`);
adapterManager.callBidderError(spec.code, error, bidderRequest)
events.emit(CONSTANTS.EVENTS.BIDDER_ERROR, { error, bidderRequest });
logError(`Server call for ${spec.code} failed: ${errorMessage} ${error.status}. Continuing without bids.`);
onResponse();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"NO_BID": "noBid",
"BID_WON": "bidWon",
"BIDDER_DONE": "bidderDone",
"BIDDER_ERROR": "bidderError",
"SET_TARGETING": "setTargeting",
"BEFORE_REQUEST_BIDS": "beforeRequestBids",
"BEFORE_BIDDER_HTTP": "beforeBidderHttp",
Expand Down
31 changes: 31 additions & 0 deletions test/spec/unit/core/adapterManager_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,37 @@ describe('adapterManager tests', function () {
});
}); // end onBidViewable

describe('onBidderError', function () {
const bidder = 'appnexus';
const appnexusSpec = { onBidderError: sinon.stub() };
const appnexusAdapter = {
bidder,
getSpec: function() { return appnexusSpec; },
}
before(function () {
config.setConfig({s2sConfig: { enabled: false }});
});

beforeEach(function () {
adapterManager.bidderRegistry[bidder] = appnexusAdapter;
});

afterEach(function () {
delete adapterManager.bidderRegistry[bidder];
});

it('should call spec\'s onBidderError callback when callBidderError is called', function () {
const bidderRequest = getBidRequests().find(bidRequest => bidRequest.bidderCode === bidder);
const xhrErrorMock = {
status: 500,
statusText: 'Internal Server Error'
};
adapterManager.callBidderError(bidder, xhrErrorMock, bidderRequest);
sinon.assert.calledOnce(appnexusSpec.onBidderError);
sinon.assert.calledWithExactly(appnexusSpec.onBidderError, { error: xhrErrorMock, bidderRequest });
});
}); // end onBidderError

describe('S2S tests', function () {
beforeEach(function () {
config.setConfig({s2sConfig: CONFIG});
Expand Down
62 changes: 61 additions & 1 deletion test/spec/unit/core/bidderFactory_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,17 +552,27 @@ describe('bidders created by newBidder', function () {

describe('when the ajax call fails', function () {
let ajaxStub;
let callBidderErrorStub;
let eventEmitterStub;
let xhrErrorMock = {
status: 500,
statusText: 'Internal Server Error'
};

beforeEach(function () {
ajaxStub = sinon.stub(ajax, 'ajax').callsFake(function(url, callbacks) {
callbacks.error('ajax call failed.');
callbacks.error('ajax call failed.', xhrErrorMock);
});
callBidderErrorStub = sinon.stub(adapterManager, 'callBidderError');
eventEmitterStub = sinon.stub(events, 'emit');
addBidResponseStub.reset();
doneStub.reset();
});

afterEach(function () {
ajaxStub.restore();
callBidderErrorStub.restore();
eventEmitterStub.restore();
});

it('should not spec.interpretResponse()', function () {
Expand All @@ -580,6 +590,14 @@ describe('bidders created by newBidder', function () {

expect(spec.interpretResponse.called).to.equal(false);
expect(doneStub.calledOnce).to.equal(true);
expect(callBidderErrorStub.calledOnce).to.equal(true);
expect(callBidderErrorStub.firstCall.args[0]).to.equal(CODE);
expect(callBidderErrorStub.firstCall.args[1]).to.equal(xhrErrorMock);
expect(callBidderErrorStub.firstCall.args[2]).to.equal(MOCK_BIDS_REQUEST);
sinon.assert.calledWith(eventEmitterStub, CONSTANTS.EVENTS.BIDDER_ERROR, {
error: xhrErrorMock,
bidderRequest: MOCK_BIDS_REQUEST
});
});

it('should not add bids for each adunit code into the auction', function () {
Expand All @@ -598,6 +616,14 @@ describe('bidders created by newBidder', function () {

expect(addBidResponseStub.callCount).to.equal(0);
expect(doneStub.calledOnce).to.equal(true);
expect(callBidderErrorStub.calledOnce).to.equal(true);
expect(callBidderErrorStub.firstCall.args[0]).to.equal(CODE);
expect(callBidderErrorStub.firstCall.args[1]).to.equal(xhrErrorMock);
expect(callBidderErrorStub.firstCall.args[2]).to.equal(MOCK_BIDS_REQUEST);
sinon.assert.calledWith(eventEmitterStub, CONSTANTS.EVENTS.BIDDER_ERROR, {
error: xhrErrorMock,
bidderRequest: MOCK_BIDS_REQUEST
});
});

it('should call spec.getUserSyncs() with no responses', function () {
Expand All @@ -616,6 +642,40 @@ describe('bidders created by newBidder', function () {
expect(spec.getUserSyncs.calledOnce).to.equal(true);
expect(spec.getUserSyncs.firstCall.args[1]).to.deep.equal([]);
expect(doneStub.calledOnce).to.equal(true);
expect(callBidderErrorStub.calledOnce).to.equal(true);
expect(callBidderErrorStub.firstCall.args[0]).to.equal(CODE);
expect(callBidderErrorStub.firstCall.args[1]).to.equal(xhrErrorMock);
expect(callBidderErrorStub.firstCall.args[2]).to.equal(MOCK_BIDS_REQUEST);
sinon.assert.calledWith(eventEmitterStub, CONSTANTS.EVENTS.BIDDER_ERROR, {
error: xhrErrorMock,
bidderRequest: MOCK_BIDS_REQUEST
});
});

it('should call spec.getUserSyncs() with no responses', function () {
const bidder = newBidder(spec);

spec.isBidRequestValid.returns(true);
spec.buildRequests.returns({
method: 'POST',
url: 'test.url.com',
data: {}
});
spec.getUserSyncs.returns([]);

bidder.callBids(MOCK_BIDS_REQUEST, addBidResponseStub, doneStub, ajaxStub, onTimelyResponseStub, wrappedCallback);

expect(spec.getUserSyncs.calledOnce).to.equal(true);
expect(spec.getUserSyncs.firstCall.args[1]).to.deep.equal([]);
expect(doneStub.calledOnce).to.equal(true);
expect(callBidderErrorStub.calledOnce).to.equal(true);
expect(callBidderErrorStub.firstCall.args[0]).to.equal(CODE);
expect(callBidderErrorStub.firstCall.args[1]).to.equal(xhrErrorMock);
expect(callBidderErrorStub.firstCall.args[2]).to.equal(MOCK_BIDS_REQUEST);
sinon.assert.calledWith(eventEmitterStub, CONSTANTS.EVENTS.BIDDER_ERROR, {
error: xhrErrorMock,
bidderRequest: MOCK_BIDS_REQUEST
});
});
});
});
Expand Down