Skip to content

Commit 7db09d7

Browse files
committed
Cleanup
1 parent 3a41bee commit 7db09d7

File tree

7 files changed

+36
-41
lines changed

7 files changed

+36
-41
lines changed

injected/src/features/duck-player.js

Lines changed: 3 additions & 3 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 { reportException } from '../../../special-pages/shared/report-metric.js';
40+
import { METRIC_NAME_GENERIC_ERROR, reportException } from '../../../special-pages/shared/report-metric.js';
4141

4242
/**
4343
* @typedef UserValues - A way to communicate user settings
@@ -110,8 +110,8 @@ export default class DuckPlayerFeature extends ContentFeature {
110110
comms.serpProxy();
111111
}
112112
} catch (e) {
113-
const message = e.message || 'Could not initialize duck player: ' + e.toString();
114-
const kind = e.name;
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;
115115
reportException(this.messaging, { message, kind });
116116
}
117117
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,23 +126,23 @@ export class DuckPlayerOverlayMessages {
126126
.then((updated) => respond(constants.MSG_NAME_PUSH_DATA, updated))
127127
.catch((e) => {
128128
console.error(e);
129-
reportException(this.messaging, { message: e.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
129+
reportException(this.messaging, { message: e?.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
130130
});
131131
}
132132
if (evt.detail.kind === constants.MSG_NAME_READ_VALUES_SERP) {
133133
return this.getUserValues()
134134
.then((updated) => respond(constants.MSG_NAME_PUSH_DATA, updated))
135135
.catch((e) => {
136136
console.error(e);
137-
reportException(this.messaging, { message: e.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
137+
reportException(this.messaging, { message: e?.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
138138
});
139139
}
140140
if (evt.detail.kind === constants.MSG_NAME_OPEN_INFO) {
141141
return this.openInfo();
142142
}
143143
console.warn('unhandled event', evt);
144144
} catch (e) {
145-
reportException(this.messaging, { message: e.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
145+
reportException(this.messaging, { message: e?.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
146146
console.warn('cannot handle this message', e);
147147
}
148148
});

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
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-
reportException(this.messages.messaging, { message: e.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
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-
reportException(this.messages.messaging, { message: e.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
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-
reportException(this.messages.messaging, { message: e.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
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-
reportException(this.messages.messaging, { message: e.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
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-
reportException(this.messages.messaging, { message: e.toString(), kind: METRIC_NAME_MESSAGING_ERROR });
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: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { METRIC_NAME_VIDEO_ID_ERROR, METRIC_NAME_TIMESTAMP_ERROR } from '../../../shared/report-metric.js';
21
export class EmbedSettings {
32
/**
43
* @param {object} params
@@ -104,17 +103,9 @@ class VideoId {
104103
* @throws {Error}
105104
*/
106105
constructor(input) {
107-
if (typeof input !== 'string') {
108-
const error = new Error('string required, got: ' + input);
109-
error.name = METRIC_NAME_VIDEO_ID_ERROR;
110-
throw error;
111-
}
106+
if (typeof input !== 'string') throw new Error('string required, got: ' + input);
112107
const sanitized = sanitizeYoutubeId(input);
113-
if (sanitized === null) {
114-
const error = new Error('invalid ID from: ' + input);
115-
error.name = METRIC_NAME_VIDEO_ID_ERROR;
116-
throw error;
117-
}
108+
if (sanitized === null) throw new Error('invalid ID from: ' + input);
118109
this.id = sanitized;
119110
}
120111

@@ -135,17 +126,9 @@ class Timestamp {
135126
* @throws {Error}
136127
*/
137128
constructor(input) {
138-
if (typeof input !== 'string') {
139-
const error = new Error('string required for timestamp, got: ' + input);
140-
error.name = METRIC_NAME_TIMESTAMP_ERROR;
141-
throw error;
142-
}
129+
if (typeof input !== 'string') throw new Error('string required for timestamp');
143130
const seconds = timestampInSeconds(input);
144-
if (seconds === null) {
145-
const error = new Error('invalid input for timestamp: ' + input);
146-
error.name = METRIC_NAME_TIMESTAMP_ERROR;
147-
throw error;
148-
}
131+
if (seconds === null) throw new Error('invalid input for timestamp: ' + input);
149132
this.seconds = seconds;
150133
}
151134

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

Lines changed: 16 additions & 4 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 } from '../../../shared/report-metric.js';
18+
import { reportException, METRIC_NAME_INITIAL_SETUP_ERROR, METRIC_NAME_INIT_ERROR } from '../../../shared/report-metric.js';
1919

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

@@ -35,7 +35,11 @@ export async function init(messaging, telemetry, baseEnvironment) {
3535
}
3636

3737
const init = result.value;
38-
console.log('INITIAL DATA');
38+
if (!init) {
39+
const error = new Error('missing initialSetup data');
40+
error.name = METRIC_NAME_INITIAL_SETUP_ERROR;
41+
throw error;
42+
}
3943

4044
// update the 'env' in case it was changed by native sides
4145
const environment = baseEnvironment
@@ -71,12 +75,16 @@ export async function init(messaging, telemetry, baseEnvironment) {
7175
console.log(settings);
7276

7377
const embed = createEmbedSettings(window.location.href, settings);
78+
if (!embed) {
79+
// TODO: Should we continue to render the page if embed is not found?
80+
reportException(messaging.messaging, { message: 'embed not found', kind: METRIC_NAME_INIT_ERROR });
81+
console.log('embed not found');
82+
}
7483

7584
const didCatch = (error) => {
7685
const message = error?.message;
7786
const kind = error?.error?.name;
7887
reportException(messaging.messaging, { message, kind });
79-
console.log('reportException', message, kind);
8088

8189
// TODO: Remove the following event once all native platforms are responding to 'reportMetric: exception'
8290
messaging.reportPageException({ message: message || 'unknown error' });
@@ -85,7 +93,11 @@ export async function init(messaging, telemetry, baseEnvironment) {
8593
document.body.dataset.layout = settings.layout;
8694

8795
const root = document.querySelector('body');
88-
if (!root) throw new Error('could not render, root element missing');
96+
if (!root) {
97+
const error = new Error('could not render, root element missing');
98+
error.name = METRIC_NAME_INIT_ERROR;
99+
throw error;
100+
}
89101

90102
if (environment.display === 'app') {
91103
render(

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

Lines changed: 3 additions & 2 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, METRIC_NAME_INIT_ERROR } from '../../../shared/report-metric.js';
7+
import { reportException, METRIC_NAME_GENERIC_ERROR } from '../../../shared/report-metric.js';
88

99
export class DuckplayerPage {
1010
/**
@@ -184,7 +184,8 @@ init(duckplayerPage, telemetry, baseEnvironment).catch((e) => {
184184
// messages.
185185
console.error(e);
186186
const message = typeof e?.message === 'string' ? e.message : 'unknown error';
187-
reportException(duckplayerPage.messaging, { message, kind: METRIC_NAME_INIT_ERROR });
187+
const kind = typeof e?.name === 'string' ? e.name : METRIC_NAME_GENERIC_ERROR;
188+
reportException(duckplayerPage.messaging, { message, kind });
188189

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

special-pages/shared/report-metric.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ export const METRIC_NAME_GENERIC_ERROR = 'Error';
44
export const METRIC_NAME_INIT_ERROR = 'InitError';
55
export const METRIC_NAME_INITIAL_SETUP_ERROR = 'InitialSetupError';
66
export const METRIC_NAME_MESSAGING_ERROR = 'MessagingError';
7-
export const METRIC_NAME_TIMESTAMP_ERROR = 'TimestampError';
8-
export const METRIC_NAME_VIDEO_ID_ERROR = 'VideoIdError';
97
export const METRIC_NAME_VIDEO_OVERLAY_ERROR = 'VideoOverlayError';
108

119
/**
@@ -38,6 +36,7 @@ export function reportMetric(messaging, metricEvent) {
3836
* @param {ExceptionMetric['params']} params
3937
*/
4038
export function reportException(messaging, params) {
39+
console.log('reportException', params);
4140
const message = typeof params?.message === 'string' ? params.message : 'unknown error';
4241
const kind = typeof params?.kind === 'string' ? params.kind : 'Error';
4342

0 commit comments

Comments
 (0)