Skip to content

Commit 701d3af

Browse files
committed
Constants refactor
1 parent 6454863 commit 701d3af

File tree

11 files changed

+51
-51
lines changed

11 files changed

+51
-51
lines changed

injected/integration-test/page-objects/duckplayer-overlays.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,8 @@ export class DuckplayerOverlays {
488488
* @param {string} message
489489
*/
490490
async didSendException(kind, message) {
491+
console.log('messages', await this.collector.outgoingMessages());
492+
491493
const messages = await this.collector.waitForMessage('reportMetric');
492494
expect(messages).toMatchObject([{ payload: { params: { metricName: 'exception', params: { kind, message } } } }]);
493495
}

injected/src/features/duckplayer/components/ddg-video-overlay.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { overlayCopyVariants } from '../text.js';
44
import { appendImageAsBackground } from '../util.js';
55
import { VideoOverlay } from '../video-overlay.js';
66
import { createPolicy, html, trustedUnsafe } from '../../../dom-utils.js';
7+
import { METRIC_NAME_VIDEO_OVERLAY_ERROR } from '../../../../../special-pages/shared/report-metric.js';
78

89
/**
910
* The custom element that we use to present our UI elements
@@ -24,7 +25,7 @@ export class DDGVideoOverlay extends HTMLElement {
2425
super();
2526
if (!(manager instanceof VideoOverlay)) {
2627
const error = new Error('Invalid VideoOverlay manager');
27-
error.name = 'VideoOverlayError';
28+
error.name = METRIC_NAME_VIDEO_OVERLAY_ERROR;
2829
throw error;
2930
}
3031
this.environment = environment;
@@ -127,7 +128,7 @@ export class DDGVideoOverlay extends HTMLElement {
127128
const remember = containerElement.querySelector('input[name="ddg-remember"]');
128129
if (!(remember instanceof HTMLInputElement)) {
129130
const error = new Error('Cannot find remember checkbox');
130-
error.name = 'VideoOverlayError';
131+
error.name = METRIC_NAME_VIDEO_OVERLAY_ERROR;
131132
throw error;
132133
}
133134
this.manager.userOptOut(remember.checked, params);
@@ -139,7 +140,7 @@ export class DDGVideoOverlay extends HTMLElement {
139140
const remember = containerElement.querySelector('input[name="ddg-remember"]');
140141
if (!(remember instanceof HTMLInputElement)) {
141142
const error = new Error('Cannot find remember checkbox');
142-
error.name = 'VideoOverlayError';
143+
error.name = METRIC_NAME_VIDEO_OVERLAY_ERROR;
143144
throw error;
144145
}
145146
this.manager.userOptIn(remember.checked, params);

injected/src/features/duckplayer/overlay-messages.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable promise/prefer-await-to-then */
22
import * as constants from './constants.js';
3+
import { reportException, METRIC_NAME_MESSAGING_ERROR } from '../../../../special-pages/shared/report-metric.js';
34

45
/**
56
* @typedef {import("@duckduckgo/messaging").Messaging} Messaging
@@ -125,34 +126,27 @@ export class DuckPlayerOverlayMessages {
125126
.then((updated) => respond(constants.MSG_NAME_PUSH_DATA, updated))
126127
.catch((e) => {
127128
console.error(e);
128-
this.reportException({ message: e.toString(), kind: 'MessagingError' });
129+
reportException(this.messaging, { message: e.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
129130
});
130131
}
131132
if (evt.detail.kind === constants.MSG_NAME_READ_VALUES_SERP) {
132133
return this.getUserValues()
133134
.then((updated) => respond(constants.MSG_NAME_PUSH_DATA, updated))
134135
.catch((e) => {
135136
console.error(e);
136-
this.reportException({ message: e.toString(), kind: 'MessagingError' });
137+
reportException(this.messaging, { message: e.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
137138
});
138139
}
139140
if (evt.detail.kind === constants.MSG_NAME_OPEN_INFO) {
140141
return this.openInfo();
141142
}
142143
console.warn('unhandled event', evt);
143144
} catch (e) {
144-
this.reportException({ message: e.toString(), kind: 'MessagingError' });
145+
reportException(this.messaging, { message: e.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
145146
console.warn('cannot handle this message', e);
146147
}
147148
});
148149
}
149-
150-
/**
151-
* @param {import('../../../../special-pages/shared/types/shared.ts').ExceptionMetric['params']} params
152-
*/
153-
reportException(params) {
154-
return this.messaging.notify('reportMetric', { metricName: 'exception', params });
155-
}
156150
}
157151

158152
/**

injected/src/features/duckplayer/video-overlay.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { mobileStrings } from './text.js';
3333
import { DDGVideoOverlayMobile } from './components/ddg-video-overlay-mobile.js';
3434
import { DDGVideoThumbnailOverlay } from './components/ddg-video-thumbnail-overlay-mobile.js';
3535
import { DDGVideoDrawerMobile } from './components/ddg-video-drawer-mobile.js';
36-
36+
import { reportException, METRIC_NAME_MESSAGING_ERROR } from '../../../../special-pages/shared/report-metric.js';
3737
/**
3838
* Handle the switch between small & large overlays
3939
* + conduct any communications
@@ -256,13 +256,13 @@ export class VideoOverlay {
256256
elem.addEventListener(DDGVideoOverlayMobile.OPT_OUT, (/** @type {CustomEvent<{remember: boolean}>} */ e) => {
257257
return this.mobileOptOut(e.detail.remember).catch((e) => {
258258
console.error(e);
259-
this.messages.reportException({ message: e.toString(), kind: 'MessagingError' });
259+
reportException(this.messages.messaging, { message: e.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
260260
});
261261
});
262262
elem.addEventListener(DDGVideoOverlayMobile.OPT_IN, (/** @type {CustomEvent<{remember: boolean}>} */ e) => {
263263
return this.mobileOptIn(e.detail.remember, params).catch((e) => {
264264
console.error(e);
265-
this.messages.reportException({ message: e.toString(), kind: 'MessagingError' });
265+
reportException(this.messages.messaging, { message: e.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
266266
});
267267
});
268268
targetElement.appendChild(elem);
@@ -297,7 +297,7 @@ export class VideoOverlay {
297297
drawer.addEventListener(DDGVideoDrawerMobile.OPT_OUT, (/** @type {CustomEvent<{remember: boolean}>} */ e) => {
298298
return this.mobileOptOut(e.detail.remember).catch((e) => {
299299
console.error(e);
300-
this.messages.reportException({ message: e.toString(), kind: 'MessagingError' });
300+
reportException(this.messages.messaging, { message: e.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
301301
});
302302
});
303303
drawer.addEventListener(DDGVideoDrawerMobile.DISMISS, () => {
@@ -309,7 +309,7 @@ export class VideoOverlay {
309309
drawer.addEventListener(DDGVideoDrawerMobile.OPT_IN, (/** @type {CustomEvent<{remember: boolean}>} */ e) => {
310310
return this.mobileOptIn(e.detail.remember, params).catch((e) => {
311311
console.error(e);
312-
this.messages.reportException({ message: e.toString(), kind: 'MessagingError' });
312+
reportException(this.messages.messaging, { message: e.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
313313
});
314314
});
315315
drawerTargetElement.appendChild(drawer);
@@ -426,7 +426,7 @@ export class VideoOverlay {
426426
})
427427
.catch((e) => {
428428
console.error('error setting user choice for opt-in', e);
429-
this.messages.reportException({ message: e.toString(), kind: 'MessagingError' });
429+
reportException(this.messages.messaging, { message: e.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
430430
});
431431
}
432432

@@ -457,7 +457,7 @@ export class VideoOverlay {
457457
.then(() => this.watchForVideoBeingAdded({ ignoreCache: true, via: 'userOptOut' }))
458458
.catch((e) => {
459459
console.error('could not set userChoice for opt-out', e);
460-
this.messages.reportException({ message: e.toString(), kind: 'MessagingError' });
460+
reportException(this.messages.messaging, { message: e.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
461461
});
462462
} else {
463463
this.messages.sendPixel(new Pixel({ name: 'play.do_not_use', remember: '0' }));

special-pages/pages/duckplayer/app/embed-settings.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { METRIC_NAME_VIDEO_ID_ERROR, METRIC_NAME_TIMESTAMP_ERROR } from '../../../shared/report-metric.js';
12
export class EmbedSettings {
23
/**
34
* @param {object} params
@@ -105,13 +106,13 @@ class VideoId {
105106
constructor(input) {
106107
if (typeof input !== 'string') {
107108
const error = new Error('string required, got: ' + input);
108-
error.name = 'VideoIdError';
109+
error.name = METRIC_NAME_VIDEO_ID_ERROR;
109110
throw error;
110111
}
111112
const sanitized = sanitizeYoutubeId(input);
112113
if (sanitized === null) {
113114
const error = new Error('invalid ID from: ' + input);
114-
error.name = 'VideoIdError';
115+
error.name = METRIC_NAME_VIDEO_ID_ERROR;
115116
throw error;
116117
}
117118
this.id = sanitized;
@@ -136,13 +137,13 @@ class Timestamp {
136137
constructor(input) {
137138
if (typeof input !== 'string') {
138139
const error = new Error('string required for timestamp, got: ' + input);
139-
error.name = 'TimestampError';
140+
error.name = METRIC_NAME_TIMESTAMP_ERROR;
140141
throw error;
141142
}
142143
const seconds = timestampInSeconds(input);
143144
if (seconds === null) {
144145
const error = new Error('invalid input for timestamp: ' + input);
145-
error.name = 'TimestampError';
146+
error.name = METRIC_NAME_TIMESTAMP_ERROR;
146147
throw error;
147148
}
148149
this.seconds = seconds;

special-pages/pages/duckplayer/app/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { Components } from './components/Components.jsx';
1515
import { MobileApp } from './components/MobileApp.jsx';
1616
import { DesktopApp } from './components/DesktopApp.jsx';
1717
import { YouTubeErrorProvider } from './providers/YouTubeErrorProvider';
18+
import { reportException, METRIC_NAME_INITIAL_SETUP_ERROR } from '../../../shared/report-metric.js';
1819

1920
/** @typedef {import('../types/duckplayer').YouTubeError} YouTubeError */
2021

@@ -29,7 +30,7 @@ export async function init(messaging, telemetry, baseEnvironment) {
2930
if ('error' in result) {
3031
console.error('INITIAL SETUP ERROR', result.error);
3132
const error = new Error(result.error);
32-
error.name = 'InitialSetupError';
33+
error.name = METRIC_NAME_INITIAL_SETUP_ERROR;
3334
throw error;
3435
}
3536

@@ -74,7 +75,7 @@ export async function init(messaging, telemetry, baseEnvironment) {
7475
const didCatch = (error) => {
7576
const message = error?.message;
7677
const kind = error?.error?.name;
77-
messaging.reportException({ message, kind });
78+
reportException(messaging.messaging, { message, kind });
7879
console.log('reportException', message, kind);
7980

8081
// TODO: Remove the following event once all native platforms are responding to 'reportMetric: exception'

special-pages/pages/duckplayer/app/providers/UserValuesProvider.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useContext, useState } from 'preact/hooks';
22
import { h, createContext } from 'preact';
33
import { useMessaging } from '../types.js';
44
import { useEffect } from 'preact/hooks';
5+
import { reportException, METRIC_NAME_MESSAGING_ERROR } from '../../../../shared/report-metric.js';
56

67
/**
78
* @typedef {import("../../types/duckplayer.js").UserValues} UserValues
@@ -61,8 +62,7 @@ export function UserValuesProvider({ initial, children }) {
6162
.catch((err) => {
6263
console.error('could not set the enabled flag', err);
6364
const message = 'could not set the enabled flag: ' + err.toString();
64-
const kind = 'MessagingError';
65-
messaging.reportException({ message, kind });
65+
reportException(messaging.messaging, { message, kind: METRIC_NAME_MESSAGING_ERROR });
6666

6767
// TODO: Remove the following event once all native platforms are responding to 'reportMetric: exception'
6868
messaging.reportPageException({ message });

special-pages/pages/duckplayer/integration-tests/duck-player.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,20 @@ export class DuckPlayerPage {
573573
return this.didSendReportMetric({ metricName: 'exception', params: { kind, message } });
574574
}
575575

576+
async didSendInitErrorException() {
577+
await this.build.switch({
578+
android: async () => {
579+
await this.didSendException('InitError', "undefined is not an object (evaluating 'init2.settings.pip')");
580+
},
581+
apple: async () => {
582+
await this.didSendException('InitError', "undefined is not an object (evaluating 'init2.settings.pip')");
583+
},
584+
windows: async () => {
585+
await this.didSendException('InitError', "Cannot read properties of undefined (reading 'pip')");
586+
},
587+
});
588+
}
589+
576590
async withStorageValues() {
577591
await this.page.evaluate(() => {
578592
localStorage.setItem('foo', 'bar');

special-pages/pages/duckplayer/integration-tests/duckplayer.spec.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -340,21 +340,10 @@ test.describe('reporting exceptions', () => {
340340
// load as normal
341341
duckplayer.initError();
342342
await duckplayer.openWithVideoID();
343-
const message = isWindows(workerInfo)
344-
? "Cannot read properties of undefined (reading 'pip')"
345-
: "undefined is not an object (evaluating 'init2.settings.pip')";
346-
await duckplayer.didSendException('InitError', message);
343+
await duckplayer.didSendInitErrorException();
347344
});
348345
});
349346

350-
/**
351-
* @param {import("@playwright/test").TestInfo} testInfo
352-
*/
353-
function isWindows(testInfo) {
354-
const u = /** @type {any} */ (testInfo.project.use);
355-
return u?.platform === 'windows';
356-
}
357-
358347
/**
359348
* @param {import("@playwright/test").TestInfo} testInfo
360349
*/

special-pages/pages/duckplayer/src/index.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { createSpecialPageMessaging } from '../../../shared/create-special-page-
44
import { init } from '../app/index.js';
55
import { initStorage } from './storage.js';
66
import '../../../shared/live-reload.js';
7-
import { reportException } from '../../../shared/report-metric.js';
7+
import { reportException, METRIC_NAME_INIT_ERROR } from '../../../shared/report-metric.js';
88

99
export class DuckplayerPage {
1010
/**
@@ -116,14 +116,6 @@ export class DuckplayerPage {
116116
reportInitException(params) {
117117
this.messaging.notify('reportInitException', params);
118118
}
119-
120-
/**
121-
* This will be sent to report metrics to the native layer
122-
* @param {import('../../../shared/types/shared.ts').ReportMetricEvent['params']} params
123-
*/
124-
reportException(params) {
125-
reportException(this.messaging, params);
126-
}
127119
}
128120

129121
// TODO: Remove telemetry
@@ -192,8 +184,7 @@ init(duckplayerPage, telemetry, baseEnvironment).catch((e) => {
192184
// messages.
193185
console.error(e);
194186
const message = typeof e?.message === 'string' ? e.message : 'unknown error';
195-
const kind = 'InitError';
196-
duckplayerPage.reportException({ message, kind });
187+
reportException(duckplayerPage.messaging, { message, kind: METRIC_NAME_INIT_ERROR });
197188

198189
// TODO: Remove this event once all native platforms are responding to 'reportMetric: exception'
199190
duckplayerPage.reportInitException({ message });

0 commit comments

Comments
 (0)