Skip to content

Commit

Permalink
Merge eba158e into e01cf0d
Browse files Browse the repository at this point in the history
  • Loading branch information
dskamiotis authored Oct 2, 2024
2 parents e01cf0d + eba158e commit 7056277
Show file tree
Hide file tree
Showing 29 changed files with 149 additions and 375 deletions.
5 changes: 5 additions & 0 deletions .changeset/good-planets-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@guardian/commercial': patch
---

remove raven client from commercial for error reporting
5 changes: 5 additions & 0 deletions jest.setupTestFrameworkScriptFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ window.guardian = {
active: undefined,
onDetect: [],
},
modules: {
sentry: {
reportError: jest.fn(),
},
},
};

window.fetch = jest.fn().mockImplementation(() =>
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@
"fastdom": "^1.0.11",
"lodash-es": "^4.17.21",
"process": "^0.11.10",
"raven-js": "^3.27.2",
"tslib": "^2.6.2",
"web-vitals": "^4.2.1"
},
Expand Down
8 changes: 0 additions & 8 deletions pnpm-lock.yaml

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

1 change: 0 additions & 1 deletion src/core/messenger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ describe('Cross-frame messenger', () => {
source: 'saucy',
})
.then(() => {
console.log(postMessage);
expect(postMessage).toHaveBeenCalledWith(
{
error: null,
Expand Down
34 changes: 6 additions & 28 deletions src/core/messenger.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { reportError } from 'utils/report-error';
import { postMessage } from './messenger/post-message';

/**
Expand Down Expand Up @@ -117,11 +118,6 @@ type ListenerOptions = {
window?: WindowProxy;
};

type MessengerErrorHandler = (
err: Error,
features: Record<string, string>,
) => void;

/**
* Types of functions to register a listener for a given type of iframe message
*/
Expand Down Expand Up @@ -163,9 +159,6 @@ type RespondCallback = (

const LISTENERS: Listeners = {};
let REGISTERED_LISTENERS = 0;
let reportError: MessengerErrorHandler = () => {
// not set yet
};

const error405 = {
code: 405,
Expand Down Expand Up @@ -374,9 +367,7 @@ const onMessage = async (event: MessageEvent): Promise<void> => {
respond(message.id, event.source, null, response);
})
.catch((ex: Error) => {
reportError(ex, {
feature: 'native-ads',
});
reportError(ex, 'native-ads');
respond(
message.id,
event.source,
Expand Down Expand Up @@ -500,24 +491,12 @@ export const unregister: UnregisterListener = (type, callback, options) => {
* @param persistentListeners The persistent listener registration functions
*/
export const init = (
listeners: Array<
(
register: RegisterListener,
errorHandler: MessengerErrorHandler,
) => void
>,
persistentListeners: Array<
(
register: RegisterPersistentListener,
errorHandler: MessengerErrorHandler,
) => void
>,
errorHandler: MessengerErrorHandler,
listeners: Array<(register: RegisterListener) => void>,
persistentListeners: Array<(register: RegisterPersistentListener) => void>,
): void => {
reportError = errorHandler;
listeners.forEach((moduleInit) => moduleInit(register, errorHandler));
listeners.forEach((moduleInit) => moduleInit(register));
persistentListeners.forEach((moduleInit) =>
moduleInit(registerPersistentListener, errorHandler),
moduleInit(registerPersistentListener),
);
};

Expand All @@ -529,5 +508,4 @@ export type {
RegisterPersistentListener,
UnregisterListener,
ListenerOptions,
MessengerErrorHandler,
};
9 changes: 1 addition & 8 deletions src/define/create-advert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@ const createAdvert = (
log('commercial', errMsg);

if (!navigator.userAgent.includes('DuckDuckGo')) {
reportError(
new Error(errMsg),
{
feature: 'commercial',
},
false,
1 / 100,
);
reportError(new Error(errMsg), 'commercial');
}

return null;
Expand Down
4 changes: 1 addition & 3 deletions src/events/on-slot-render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@ const reportEmptyResponse = (
? event.slot.getTargeting('k')
: [];
const adKeywords = adTargetingKValues.join(', ');

reportError(
new Error('dfp returned an empty ad response'),
'commercial',
{
feature: 'commercial',
adUnit: adUnitPath,
adSlot: adSlotId,
adKeywords,
},
false,
);
}
};
Expand Down
10 changes: 1 addition & 9 deletions src/events/render-advert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ const renderAdvert = (
slotRenderEndedEvent: googletag.events.SlotRenderEndedEvent,
): Promise<boolean> => {
addContentClass(advert.node);

return getAdIframe(advert.node)
.then((isRendered) => {
const creativeTemplateId =
Expand Down Expand Up @@ -227,14 +226,7 @@ const renderAdvert = (
.then(() => isRendered);
})
.catch((err) => {
reportError(
err,
{
feature: 'commercial',
},
false,
);

reportError(err, 'commercial');
return Promise.resolve(false);
});
};
Expand Down
10 changes: 4 additions & 6 deletions src/init/consented.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@ import { init as prepareAdVerification } from 'lib/ad-verification/prepare-ad-ve
import { commercialFeatures } from 'lib/commercial-features';
import { adSlotIdPrefix } from 'lib/dfp/dfp-env-globals';
import { reportError } from 'utils/report-error';
import { catchErrorsWithContext } from 'utils/robust';
import { catchErrorsAndReport } from 'utils/robust';
import { initDfpListeners } from './consented/dfp-listeners';
import { initDynamicAdSlots } from './consented/dynamic-ad-slots';
import { init as initMessenger } from './consented/messenger';

type Modules = Array<[`${string}-${string}`, () => Promise<unknown>]>;

const tags: Record<string, string> = {
feature: 'commercial',
bundle: 'standalone',
};

// modules necessary to load the first ads on the page
const commercialBaseModules: Modules = [];

Expand Down Expand Up @@ -73,7 +71,7 @@ const loadModules = (modules: Modules, eventName: string) => {
modules.forEach((module) => {
const [moduleName, moduleInit] = module;

catchErrorsWithContext(
catchErrorsAndReport(
[
[
moduleName,
Expand Down Expand Up @@ -121,7 +119,7 @@ const bootCommercial = async (): Promise<void> => {
// Init Commercial event timers
EventTimer.init();

catchErrorsWithContext(
catchErrorsAndReport(
[
[
'ga-user-timing-commercial-start',
Expand Down Expand Up @@ -150,7 +148,7 @@ const bootCommercial = async (): Promise<void> => {
await Promise.all(promises).then(recordCommercialMetrics);
} catch (error) {
// report async errors in bootCommercial to Sentry with the commercial feature tag
reportError(error, tags, false);
reportError(error, 'commercial', tags);
}
};

Expand Down
6 changes: 3 additions & 3 deletions src/init/consented/dfp-listeners.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import raven from 'lib/raven';
import { wrapWithErrorReporting } from 'utils/report-error';
import { onSlotLoad } from '../../events/on-slot-load';
import { onSlotRender } from '../../events/on-slot-render';
import { onSlotViewableFunction } from '../../events/on-slot-viewable';
Expand All @@ -9,11 +9,11 @@ const initDfpListeners = (): Promise<void> => {

pubads.addEventListener(
'slotRenderEnded',
raven.wrap<typeof onSlotRender>(onSlotRender),
wrapWithErrorReporting(onSlotRender),
);
pubads.addEventListener(
'slotOnload',
raven.wrap<typeof onSlotLoad>(onSlotLoad),
wrapWithErrorReporting(onSlotLoad),
);
pubads.addEventListener('impressionViewable', onSlotViewableFunction());
});
Expand Down
5 changes: 1 addition & 4 deletions src/init/consented/dynamic-ad-slots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ export const initDynamicAdSlots = async (): Promise<void> => {
try {
await init();
} catch (error) {
reportError(error, {
feature: 'commercial',
tag: name,
});
reportError(error, 'commercial', { tag: name });
}
}),
);
Expand Down
2 changes: 0 additions & 2 deletions src/init/consented/messenger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { init as resize } from 'core/messenger/resize';
import { init as scroll } from 'core/messenger/scroll';
import { init as type } from 'core/messenger/type';
import { init as viewport } from 'core/messenger/viewport';
import { reportError } from 'utils/report-error';

/**
* Messenger gets to skip the promise chain and run immediately.
Expand All @@ -32,7 +31,6 @@ initMessenger(
passback,
],
[scroll, viewport],
reportError,
);

const init = () => Promise.resolve();
Expand Down
19 changes: 10 additions & 9 deletions src/init/consented/prepare-permutive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,24 +317,26 @@ describe('Generating Permutive payload utils', () => {
page: { ...testPageConfig, section: 'uk' },
};

it('catches errors and calls the logger correctly when no global permutive', () => {
const logger = jest.fn();
it('catches errors and calls reportError correctly when no global permutive', () => {
const reportError = window.guardian.modules.sentry?.reportError;
_.runPermutive(
{
page: testPageConfig,
},
undefined,
logger,
);
const err = (logger.mock.calls[0] as Error[])[0];
const err = (
(reportError as jest.Mock).mock.calls[0] as Error[]
)[0];
expect(err).toBeInstanceOf(Error);
expect(err?.message).toBe('Global Permutive setup error');
});

it('calls the permutive addon method with the correct payload', () => {
const mockPermutive = { addon: jest.fn(), identify: jest.fn() };
const logger = jest.fn();

_.runPermutive(validConfigForPayload, mockPermutive, logger);
_.runPermutive(validConfigForPayload, mockPermutive);
expect(mockPermutive.addon).toHaveBeenCalledWith('web', {
page: {
content: expect.objectContaining({ section: 'uk' }) as {
Expand All @@ -356,22 +358,21 @@ describe('Generating Permutive payload utils', () => {
...validConfigForPayload,
};

_.runPermutive(config, mockPermutive, logger);
_.runPermutive(config, mockPermutive);
expect(mockPermutive.identify).toHaveBeenCalledWith([
{ tag: 'ophan', id: bwid },
]);
expect(logger).not.toHaveBeenCalled();
});
it("calls permutive's identify before it calls addon, if the browser ID is present", () => {
const mockPermutive = { addon: jest.fn(), identify: jest.fn() };
const logger = jest.fn();
const bwid = '1234567890abcdef';
const config = {
ophan: { browserId: bwid, pageViewId: 'pvid' },
...validConfigForPayload,
};

_.runPermutive(config, mockPermutive, logger);
_.runPermutive(config, mockPermutive);
const [identifyCallOrder] =
mockPermutive.identify.mock.invocationCallOrder;
const [addonCallOrder] = mockPermutive.addon.mock
Expand All @@ -382,7 +383,7 @@ describe('Generating Permutive payload utils', () => {
const mockPermutive = { addon: jest.fn(), identify: jest.fn() };
const logger = jest.fn();

_.runPermutive(validConfigForPayload, mockPermutive, logger);
_.runPermutive(validConfigForPayload, mockPermutive);
expect(mockPermutive.identify).not.toHaveBeenCalled();
expect(logger).not.toHaveBeenCalled();
});
Expand Down
5 changes: 2 additions & 3 deletions src/init/consented/prepare-permutive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ const generatePermutiveIdentities = (
const runPermutive = (
pageConfig: PermutivePageConfig,
permutiveGlobal: Permutive | undefined,
logger: typeof reportError,
): void => {
try {
if (!permutiveGlobal?.addon) {
Expand All @@ -157,7 +156,7 @@ const runPermutive = (
page: payload,
});
} catch (err) {
logger(err, { feature: 'commercial' }, false);
reportError(err, 'commercial');
}
};

Expand Down Expand Up @@ -237,7 +236,7 @@ export const initPermutive = (): Promise<void> => {
page: window.guardian.config.page,
ophan: window.guardian.config.ophan,
};
runPermutive(permutiveConfig, window.permutive, reportError);
runPermutive(permutiveConfig, window.permutive);

return Promise.resolve();
};
Expand Down
3 changes: 1 addition & 2 deletions src/init/consentless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import { initConsentless } from 'init/consentless/prepare-ootag';
import { reloadPageOnConsentChange } from 'init/shared/reload-page-on-consent-change';
import { init as setAdTestCookie } from 'init/shared/set-adtest-cookie';
import { init as setAdTestInLabelsCookie } from 'init/shared/set-adtest-in-labels-cookie';
import { reportError } from 'utils/report-error';

const bootConsentless = async (consentState: ConsentState): Promise<void> => {
const consentlessModuleList = [
initMessenger([background, resize, type], [], reportError),
initMessenger([background, resize, type], []),
setAdTestCookie(),
setAdTestInLabelsCookie(),
initConsentless(consentState),
Expand Down
Loading

0 comments on commit 7056277

Please sign in to comment.