Skip to content

Commit

Permalink
add support for catching errors from postscribe library (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibb-jile authored Aug 25, 2021
1 parent 3d4d4bf commit 4066462
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 12 additions & 10 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export function writeAdUrl(adUrl, width, height) {
}

export function writeAdHtml(markup) {
postscribe(document.body, markup);
postscribe(document.body, markup, {
error: console.error
});
}

export function sendRequest(url, callback) {
Expand Down Expand Up @@ -99,15 +101,15 @@ export function loadScript(currentWindow, tagSrc, successCallback, errorCallback

/**
* Return comment element
* @param {*} bid
* @param {*} bid
*/
export function getCreativeComment(bid) {
return document.createComment(`Creative ${bid.crid} served by Prebid.js Header Bidding`);
}

/**
* Returns comment element markup
* @param {*} bid
* @param {*} bid
*/
export function getCreativeCommentMarkup(bid) {
let creativeComment = exports.getCreativeComment(bid);
Expand Down Expand Up @@ -138,7 +140,7 @@ export function transformAuctionTargetingData(tagData) {
function isMacroPresent(paramName) {
return !!(
tagData[paramName] && (
(isPlainObject(tagData[paramName]) && Object.keys(tagData[paramName]).length > 0) ||
(isPlainObject(tagData[paramName]) && Object.keys(tagData[paramName]).length > 0) ||
(isStr(tagData[paramName]) && tagData[paramName] !== '')
)
);
Expand Down Expand Up @@ -169,7 +171,7 @@ export function transformAuctionTargetingData(tagData) {
function convertKeyPairStringToMap(keywordsStr) {
let keywordsMap = {};
const keywordsArr = keywordsStr.split(',');

if (keywordsArr.length > 0) {
keywordsArr.forEach(function(keyPairStr) {
let keyPairArr = keyPairStr.split(':');
Expand All @@ -182,7 +184,7 @@ export function transformAuctionTargetingData(tagData) {
}
return keywordsMap;
}

/**
* Rename key if it's part of the auctionKeyMap object; if not, leave key as is
* Store the resultant keypair in the auctionData object for later use in renderingManager.renderAd()
Expand All @@ -192,19 +194,19 @@ export function transformAuctionTargetingData(tagData) {
Object.keys(adServerKeyMap).forEach(function(key) {
let internalKey = auctionKeyMap[key] || key;
auctionData[internalKey] = adServerKeyMap[key];
});
});
}

let auctionData = {};
let formattedKeyMap = {};

if (isMacroPresent('targetingMap')) {
formattedKeyMap = convertTargetingMapToNormalMap(tagData.targetingMap);
} else if (isMacroPresent('targetingKeywords')) {
formattedKeyMap = convertKeyPairStringToMap(tagData.targetingKeywords);
}
renameKnownAuctionKeys(formattedKeyMap);

// set keys not in defined map macros (eg targetingMap) and/or the keys setup within a non-DFP adserver
Object.keys(tagData).forEach(function (key) {
if (key !== 'targetingMap' && key !== 'targetingKeywords' && isStr(tagData[key]) && tagData[key] !== '') {
Expand All @@ -218,7 +220,7 @@ export function parseUrl(url) {
let parsed = document.createElement('a');

parsed.href = decodeURIComponent(url);

return {
href: parsed.href,
protocol: (parsed.protocol || '').replace(/:$/, ''),
Expand Down
32 changes: 32 additions & 0 deletions test/spec/renderingManager_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as domHelper from 'src/domHelper';
import { expect } from 'chai';
import { mocks } from 'test/helpers/mocks';
import { merge } from 'lodash';
import * as postscribe from "postscribe";

const renderingMocks = {
messages: [],
Expand Down Expand Up @@ -159,6 +160,37 @@ describe('renderingManager', function() {
expect(writeHtmlSpy.callCount).to.equal(1);
expect(sendRequestSpy.args[0][0]).to.equal('https://prebid.adnxs.com/pbc/v1/cache?uuid=123');
});

it('should catch errors from creative', function (done) {
window.addEventListener('error', e => {
done(e.error);
});

const consoleErrorSpy = sinon.spy(console, 'error');

const renderObject = newRenderingManager(mockWin, env);
let ucTagData = {
cacheHost: 'example.com',
cachePath: '/path',
uuid: '123',
size: '300x250'
};

renderObject.renderAd(mockWin.document, ucTagData);

let response = {
width: 300,
height: 250,
crid: 123,
adm: '<script src="notExistingScript.js"></script>'
};
requests[0].respond(200, {}, JSON.stringify(response));

setTimeout(()=>{
expect(consoleErrorSpy.callCount).to.equal(1);
done();
}, 10);
});
});

describe('amp creative', function() {
Expand Down

0 comments on commit 4066462

Please sign in to comment.