Skip to content

Commit c2cf401

Browse files
committed
Refactor
1 parent e6459c1 commit c2cf401

File tree

10 files changed

+303
-151
lines changed

10 files changed

+303
-151
lines changed

injected/src/features/duck-player.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { DuckPlayerOverlayMessages, OpenInDuckPlayerMsg, Pixel } from './duckpla
3737
import { isBeingFramed } from '../utils.js';
3838
import { initOverlays } from './duckplayer/overlays.js';
3939
import { Environment } from './duckplayer/environment.js';
40-
import { METRIC_NAME_GENERIC_ERROR, reportException } from '../../../special-pages/shared/report-metric.js';
40+
import { ReportMetric } from '../../../special-pages/shared/report-metric.js';
4141

4242
/**
4343
* @typedef UserValues - A way to communicate user settings
@@ -110,9 +110,8 @@ export default class DuckPlayerFeature extends ContentFeature {
110110
comms.serpProxy();
111111
}
112112
} catch (e) {
113-
const message = typeof e?.message === 'string' ? e.message : 'Could not initialize duck player: ' + e?.toString();
114-
const kind = typeof e?.name === 'string' ? e.name : METRIC_NAME_GENERIC_ERROR;
115-
reportException(this.messaging, { message, kind });
113+
const metrics = new ReportMetric(this.messaging);
114+
metrics.reportExceptionWithError(e);
116115
}
117116
}
118117
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ 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';
7+
8+
const EXCEPTION_KIND_VIDEO_OVERLAY_ERROR = 'VideoOverlayError';
89

910
/**
1011
* The custom element that we use to present our UI elements
@@ -25,7 +26,6 @@ export class DDGVideoOverlay extends HTMLElement {
2526
super();
2627
if (!(manager instanceof VideoOverlay)) {
2728
const error = new Error('Invalid VideoOverlay manager');
28-
error.name = METRIC_NAME_VIDEO_OVERLAY_ERROR;
2929
throw error;
3030
}
3131
this.environment = environment;
@@ -128,7 +128,7 @@ export class DDGVideoOverlay extends HTMLElement {
128128
const remember = containerElement.querySelector('input[name="ddg-remember"]');
129129
if (!(remember instanceof HTMLInputElement)) {
130130
const error = new Error('Cannot find remember checkbox');
131-
error.name = METRIC_NAME_VIDEO_OVERLAY_ERROR;
131+
error.name = EXCEPTION_KIND_VIDEO_OVERLAY_ERROR;
132132
throw error;
133133
}
134134
this.manager.userOptOut(remember.checked, params);
@@ -140,7 +140,7 @@ export class DDGVideoOverlay extends HTMLElement {
140140
const remember = containerElement.querySelector('input[name="ddg-remember"]');
141141
if (!(remember instanceof HTMLInputElement)) {
142142
const error = new Error('Cannot find remember checkbox');
143-
error.name = METRIC_NAME_VIDEO_OVERLAY_ERROR;
143+
error.name = EXCEPTION_KIND_VIDEO_OVERLAY_ERROR;
144144
throw error;
145145
}
146146
this.manager.userOptIn(remember.checked, params);

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

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +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';
3+
import { ReportMetric, EXCEPTION_KIND_MESSAGING_ERROR } from '../../../../special-pages/shared/report-metric.js';
44

55
/**
66
* @typedef {import("@duckduckgo/messaging").Messaging} Messaging
@@ -22,12 +22,13 @@ export class DuckPlayerOverlayMessages {
2222
*/
2323
this.messaging = messaging;
2424
this.environment = environment;
25+
this.metrics = new ReportMetric(messaging);
2526
}
2627

2728
/**
2829
* @returns {Promise<import("../duck-player.js").OverlaysInitialSettings>}
2930
*/
30-
initialSetup() {
31+
async initialSetup() {
3132
if (this.environment.isIntegrationMode()) {
3233
return Promise.resolve({
3334
userValues: {
@@ -37,23 +38,38 @@ export class DuckPlayerOverlayMessages {
3738
ui: {},
3839
});
3940
}
40-
return this.messaging.request(constants.MSG_NAME_INITIAL_SETUP);
41+
try {
42+
return await this.messaging.request(constants.MSG_NAME_INITIAL_SETUP);
43+
} catch (e) {
44+
this.metrics.reportException({ message: e?.message, kind: EXCEPTION_KIND_MESSAGING_ERROR });
45+
throw e;
46+
}
4147
}
4248

4349
/**
4450
* Inform the native layer that an interaction occurred
4551
* @param {import("../duck-player.js").UserValues} userValues
4652
* @returns {Promise<import("../duck-player.js").UserValues>}
4753
*/
48-
setUserValues(userValues) {
49-
return this.messaging.request(constants.MSG_NAME_SET_VALUES, userValues);
54+
async setUserValues(userValues) {
55+
try {
56+
return await this.messaging.request(constants.MSG_NAME_SET_VALUES, userValues);
57+
} catch (e) {
58+
this.metrics.reportException({ message: e?.message, kind: EXCEPTION_KIND_MESSAGING_ERROR });
59+
throw e;
60+
}
5061
}
5162

5263
/**
5364
* @returns {Promise<import("../duck-player.js").UserValues>}
5465
*/
55-
getUserValues() {
56-
return this.messaging.request(constants.MSG_NAME_READ_VALUES, {});
66+
async getUserValues() {
67+
try {
68+
return await this.messaging.request(constants.MSG_NAME_READ_VALUES, {});
69+
} catch (e) {
70+
this.metrics.reportException({ message: e?.message, kind: EXCEPTION_KIND_MESSAGING_ERROR });
71+
throw e;
72+
}
5773
}
5874

5975
/**
@@ -126,23 +142,20 @@ export class DuckPlayerOverlayMessages {
126142
.then((updated) => respond(constants.MSG_NAME_PUSH_DATA, updated))
127143
.catch((e) => {
128144
console.error(e);
129-
reportException(this.messaging, { message: e?.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
130145
});
131146
}
132147
if (evt.detail.kind === constants.MSG_NAME_READ_VALUES_SERP) {
133148
return this.getUserValues()
134149
.then((updated) => respond(constants.MSG_NAME_PUSH_DATA, updated))
135150
.catch((e) => {
136151
console.error(e);
137-
reportException(this.messaging, { message: e?.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
138152
});
139153
}
140154
if (evt.detail.kind === constants.MSG_NAME_OPEN_INFO) {
141155
return this.openInfo();
142156
}
143157
console.warn('unhandled event', evt);
144158
} catch (e) {
145-
reportException(this.messaging, { message: e?.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
146159
console.warn('cannot handle this message', e);
147160
}
148161
});

injected/src/features/duckplayer/overlays.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { DomState } from './util.js';
22
import { ClickInterception, Thumbnails } from './thumbnails.js';
33
import { VideoOverlay } from './video-overlay.js';
44
import { registerCustomElements } from './components/index.js';
5-
import { reportException, METRIC_NAME_INITIAL_SETUP_ERROR } from '../../../../special-pages/shared/report-metric.js';
5+
import { EXCEPTION_KIND_INITIAL_SETUP_ERROR } from '../../../../special-pages/shared/report-metric.js';
66

77
/**
88
* @typedef {object} OverlayOptions
@@ -28,14 +28,13 @@ export async function initOverlays(settings, environment, messages) {
2828
initialSetup = await messages.initialSetup();
2929
} catch (e) {
3030
console.warn(e);
31-
reportException(messages.messaging, { message: e?.toString(), kind: METRIC_NAME_INITIAL_SETUP_ERROR });
3231
return;
3332
}
3433

3534
if (!initialSetup) {
36-
const message = 'cannot continue without user settings';
35+
const message = 'InitialSetup data is missing';
3736
console.warn(message);
38-
reportException(messages.messaging, { message, kind: METRIC_NAME_INITIAL_SETUP_ERROR });
37+
messages.metrics.reportException({ message, kind: EXCEPTION_KIND_INITIAL_SETUP_ERROR });
3938
return;
4039
}
4140

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

Lines changed: 7 additions & 8 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-
import { reportException, METRIC_NAME_MESSAGING_ERROR } from '../../../../special-pages/shared/report-metric.js';
36+
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-
reportException(this.messages.messaging, { message: e?.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
259+
this.messages.metrics.reportExceptionWithError(e);
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-
reportException(this.messages.messaging, { message: e?.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
265+
this.messages.metrics.reportExceptionWithError(e);
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-
reportException(this.messages.messaging, { message: e?.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
300+
this.messages.metrics.reportExceptionWithError(e);
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-
reportException(this.messages.messaging, { message: e?.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
312+
this.messages.metrics.reportExceptionWithError(e);
313313
});
314314
});
315315
drawerTargetElement.appendChild(drawer);
@@ -425,8 +425,7 @@ export class VideoOverlay {
425425
return this.environment.setHref(params.toPrivatePlayerUrl());
426426
})
427427
.catch((e) => {
428-
console.error('error setting user choice for opt-in', e);
429-
reportException(this.messages.messaging, { message: e?.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
428+
this.messages.metrics.reportExceptionWithError(e);
430429
});
431430
}
432431

@@ -457,7 +456,7 @@ export class VideoOverlay {
457456
.then(() => this.watchForVideoBeingAdded({ ignoreCache: true, via: 'userOptOut' }))
458457
.catch((e) => {
459458
console.error('could not set userChoice for opt-out', e);
460-
reportException(this.messages.messaging, { message: e?.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
459+
this.messages.metrics.reportExceptionWithError(e);
461460
});
462461
} else {
463462
this.messages.sendPixel(new Pixel({ name: 'play.do_not_use', remember: '0' }));

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +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, METRIC_NAME_INIT_ERROR } from '../../../shared/report-metric.js';
18+
import { EXCEPTION_KIND_INIT_ERROR, EXCEPTION_KIND_INITIAL_SETUP_ERROR } from '../../../shared/report-metric';
1919

2020
/** @typedef {import('../types/duckplayer').YouTubeError} YouTubeError */
2121

@@ -29,14 +29,14 @@ export async function init(messaging, telemetry, baseEnvironment) {
2929
const result = await callWithRetry(() => messaging.initialSetup());
3030
if ('error' in result) {
3131
const error = new Error(result.error);
32-
error.name = METRIC_NAME_INITIAL_SETUP_ERROR;
32+
error.name = EXCEPTION_KIND_INITIAL_SETUP_ERROR;
3333
throw error;
3434
}
3535

3636
const init = result.value;
3737
if (!init) {
3838
const error = new Error('missing initialSetup data');
39-
error.name = METRIC_NAME_INITIAL_SETUP_ERROR;
39+
error.name = EXCEPTION_KIND_INITIAL_SETUP_ERROR;
4040
throw error;
4141
}
4242

@@ -75,15 +75,14 @@ export async function init(messaging, telemetry, baseEnvironment) {
7575

7676
const embed = createEmbedSettings(window.location.href, settings);
7777
if (!embed) {
78-
// TODO: Should we continue to render the page if embed is not found?
79-
reportException(messaging.messaging, { message: 'embed not found', kind: METRIC_NAME_INIT_ERROR });
78+
messaging.metrics.reportException({ message: 'embed not found', kind: EXCEPTION_KIND_INIT_ERROR });
8079
console.log('embed not found');
8180
}
8281

8382
const didCatch = (error) => {
8483
const message = error?.message;
8584
const kind = error?.error?.name;
86-
reportException(messaging.messaging, { message, kind });
85+
messaging.metrics.reportException({ message, kind });
8786

8887
// TODO: Remove the following event once all native platforms are responding to 'reportMetric: exception'
8988
messaging.reportPageException({ message: message || 'unknown error' });
@@ -94,7 +93,7 @@ export async function init(messaging, telemetry, baseEnvironment) {
9493
const root = document.querySelector('body');
9594
if (!root) {
9695
const error = new Error('could not render, root element missing');
97-
error.name = METRIC_NAME_INIT_ERROR;
96+
error.name = EXCEPTION_KIND_INIT_ERROR;
9897
throw error;
9998
}
10099

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ 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';
65

76
/**
87
* @typedef {import("../../types/duckplayer.js").UserValues} UserValues
@@ -61,11 +60,6 @@ export function UserValuesProvider({ initial, children }) {
6160
})
6261
.catch((err) => {
6362
console.error('could not set the enabled flag', err);
64-
const message = 'could not set the enabled flag: ' + err.toString();
65-
reportException(messaging.messaging, { message, kind: METRIC_NAME_MESSAGING_ERROR });
66-
67-
// TODO: Remove the following event once all native platforms are responding to 'reportMetric: exception'
68-
messaging.reportPageException({ message });
6963
});
7064
}
7165

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ 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, METRIC_NAME_GENERIC_ERROR } from '../../../shared/report-metric.js';
7+
import {
8+
ReportMetric,
9+
EXCEPTION_KIND_GENERIC_ERROR,
10+
EXCEPTION_KIND_INITIAL_SETUP_ERROR,
11+
EXCEPTION_KIND_MESSAGING_ERROR,
12+
} from '../../../shared/report-metric.js';
813

914
export class DuckplayerPage {
1015
/**
@@ -13,14 +18,15 @@ export class DuckplayerPage {
1318
constructor(messaging, injectName) {
1419
this.messaging = createTypedMessages(this, messaging);
1520
this.injectName = injectName;
21+
this.metrics = new ReportMetric(messaging);
1622
}
1723

1824
/**
1925
* This will be sent if the application has loaded, but a client-side error
2026
* has occurred that cannot be recovered from
2127
* @returns {Promise<import("../types/duckplayer.ts").InitialSetupResponse>}
2228
*/
23-
initialSetup() {
29+
async initialSetup() {
2430
if (this.injectName === 'integration') {
2531
return Promise.resolve({
2632
platform: { name: 'ios' },
@@ -37,16 +43,26 @@ export class DuckplayerPage {
3743
locale: 'en',
3844
});
3945
}
40-
return this.messaging.request('initialSetup');
46+
try {
47+
return await this.messaging.request('initialSetup');
48+
} catch (e) {
49+
this.metrics.reportException({ message: e?.message, kind: EXCEPTION_KIND_INITIAL_SETUP_ERROR });
50+
throw e;
51+
}
4152
}
4253

4354
/**
4455
* This is sent when the user wants to set Duck Player as the default.
4556
*
4657
* @param {import("../types/duckplayer.ts").UserValues} userValues
4758
*/
48-
setUserValues(userValues) {
49-
return this.messaging.request('setUserValues', userValues);
59+
async setUserValues(userValues) {
60+
try {
61+
return await this.messaging.request('setUserValues', userValues);
62+
} catch (e) {
63+
this.metrics.reportException({ message: e?.message, kind: EXCEPTION_KIND_MESSAGING_ERROR });
64+
throw e;
65+
}
5066
}
5167

5268
/**
@@ -184,8 +200,8 @@ init(duckplayerPage, telemetry, baseEnvironment).catch((e) => {
184200
// messages.
185201
console.error(e);
186202
const message = typeof e?.message === 'string' ? e.message : 'unknown error';
187-
const kind = typeof e?.name === 'string' ? e.name : METRIC_NAME_GENERIC_ERROR;
188-
reportException(duckplayerPage.messaging, { message, kind });
203+
const kind = typeof e?.name === 'string' ? e.name : EXCEPTION_KIND_GENERIC_ERROR;
204+
duckplayerPage.metrics.reportException({ message, kind });
189205

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

0 commit comments

Comments
 (0)