Skip to content

Commit edd3f57

Browse files
authored
Follow prefer-promise-reject-errors lint rule (#14693)
This rule comes from `typescript-eslint`: https://typescript-eslint.io/rules/prefer-promise-reject-errors/ We don't have it enabled yet, but in v8 it's part of the "recommended-type-checked" preset: https://typescript-eslint.io/blog/announcing-typescript-eslint-v8/#updated-configuration-rules In order to keep the upgrade to that version as small as possible, this change pre-emptively fixes code considered incorrect by that rule. It creates `Error` objects for instances where we have control over the value the `Promise` is rejecting with, and disables the rule in cases where we don't have control over this value because it comes from a library. Another approach would be to create our own `Error` object and include the unknown library error using the `cause` option, but the browser support is not good enough for that yet: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Error#cause
1 parent d1b60df commit edd3f57

File tree

4 files changed

+8
-1
lines changed

4 files changed

+8
-1
lines changed

dotcom-rendering/src/client/userFeatures/user-features.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const refresh = async (): Promise<void> => {
3030
const requestNewData = async () => {
3131
const authStatus = await getAuthStatus();
3232
if (authStatus.kind !== 'SignedIn') {
33+
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
3334
return Promise.reject('The user is not signed in');
3435
}
3536
return syncDataFromUserBenefitsApi(authStatus).then(persistResponse);

dotcom-rendering/src/components/LightboxJavascript.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const timeout = <T,>(promise: Promise<T>, delay = 1200) =>
6666
promise,
6767
new Promise<void>((_, reject) => {
6868
const timer = setTimeout(() => {
69-
reject();
69+
reject(new Error());
7070
clearTimeout(timer);
7171
}, delay);
7272
}),

dotcom-rendering/src/components/YoutubeAtom/YoutubePlayer.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ class YouTubePlayer {
143143
}
144144
} catch (e) {
145145
this.logError(e as Error);
146+
/* eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors --
147+
* This object comes from the YT API; we can't enforce that
148+
* it's an instance of Error. */
146149
reject(e);
147150
}
148151
},

dotcom-rendering/src/lib/braze/hasRequiredConsents.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ const hasRequiredConsents = (): Promise<boolean> =>
66
try {
77
resolve(getConsentFor('braze', state));
88
} catch (e) {
9+
/* eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors --
10+
* This object comes from guardian/libs; we can't enforce in
11+
* DCAR that it's an instance of Error. */
912
reject(e);
1013
}
1114
});

0 commit comments

Comments
 (0)