Skip to content

Commit 4cebe1c

Browse files
authored
refactor(shared): Split errors into modules (#6838)
1 parent f5cd73a commit 4cebe1c

18 files changed

Lines changed: 488 additions & 447 deletions

.changeset/wild-fans-love.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

packages/clerk-js/src/core/clerk.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2368,6 +2368,7 @@ export class Clerk implements ClerkInterface {
23682368
this.#fapiClient.onAfterResponse(callback);
23692369
};
23702370

2371+
// TODO @userland-errors:
23712372
__unstable__updateProps = (_props: any) => {
23722373
// We need to re-init the options here in order to keep the options passed to ClerkProvider
23732374
// in sync with the state of clerk-js. If we don't init the options here again, the following scenario is possible:

packages/clerk-js/src/core/fapiClient.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export function createFapiClient(options: FapiClientOptions): FapiClient {
104104
return true;
105105
}
106106

107+
// TODO @userland-errors:
107108
function buildQueryString({ method, path, sessionId, search, rotatingTokenNonce }: FapiRequestInit): string {
108109
const searchParams = new URLSearchParams(search as any);
109110
// the above will parse {key: ['val1','val2']} as key: 'val1,val2' and we need to recreate the array bellow
@@ -148,6 +149,7 @@ export function createFapiClient(options: FapiClientOptions): FapiClient {
148149
const { path, pathPrefix = 'v1' } = requestInit;
149150

150151
if (options.proxyUrl) {
152+
// TODO @userland-errors:
151153
const proxyBase = new URL(options.proxyUrl);
152154
let proxyPath = proxyBase.pathname.slice(1);
153155
if (proxyPath.endsWith('/')) {
@@ -185,6 +187,7 @@ export function createFapiClient(options: FapiClientOptions): FapiClient {
185187
});
186188
}
187189

190+
// TODO @userland-errors:
188191
async function request<T>(
189192
_requestInit: FapiRequestInit,
190193
requestOptions?: FapiRequestOptions,
@@ -221,6 +224,7 @@ export function createFapiClient(options: FapiClientOptions): FapiClient {
221224
: body;
222225
}
223226

227+
// TODO @userland-errors:
224228
const beforeRequestCallbacksResult = await runBeforeRequestCallbacks(requestInit);
225229
// Due to a known Safari bug regarding CORS requests, we are forced to always use GET or POST method.
226230
// The original HTTP method is used as a query string parameter instead of as an actual method to
@@ -237,6 +241,7 @@ export function createFapiClient(options: FapiClientOptions): FapiClient {
237241
try {
238242
if (beforeRequestCallbacksResult) {
239243
const maxTries = requestOptions?.fetchMaxTries ?? (isBrowserOnline() ? 4 : 11);
244+
// TODO @userland-errors:
240245
response = await retry(() => fetch(url, fetchOpts), {
241246
// This retry handles only network errors, not 4xx or 5xx responses,
242247
// so we want to try once immediately to handle simple network blips.
@@ -271,6 +276,7 @@ export function createFapiClient(options: FapiClientOptions): FapiClient {
271276
if (!response.ok) {
272277
debugLogger.error('request failed', { method, path: requestInit.path, status: response.status }, 'fapiClient');
273278
}
279+
// TODO @userland-errors:
274280
await runAfterResponseCallbacks(requestInit, fapiResponse);
275281
return fapiResponse;
276282
}

packages/clerk-js/src/core/fraudProtection.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ export class FraudProtection {
2222
private CaptchaChallengeImpl: typeof CaptchaChallenge,
2323
) {}
2424

25+
// TODO @userland-errors:
2526
public async execute<T extends () => Promise<any>, R = Awaited<ReturnType<T>>>(clerk: Clerk, cb: T): Promise<R> {
27+
// TODO @userland-errors:
2628
if (this.captchaAttemptsExceeded()) {
2729
throw new ClerkRuntimeError(
2830
'Security verification failed. Please try again by refreshing the page, clearing your browser cookies, or using a different web browser.',

packages/clerk-js/src/core/resources/Base.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,15 @@ export abstract class BaseResource {
6262
return !this.id;
6363
}
6464

65+
// TODO @userland-errors:
6566
static async _fetch<J extends ClerkResourceJSON | DeletedObjectJSON | null>(
6667
requestInit: FapiRequestInit,
6768
opts: BaseFetchOptions = {},
6869
): Promise<FapiResponseJSON<J> | null> {
6970
return FraudProtection.getInstance().execute(this.clerk, () => this._baseFetch<J>(requestInit, opts));
7071
}
7172

73+
// TODO @userland-errors:
7274
protected static async _baseFetch<J extends ClerkResourceJSON | DeletedObjectJSON | null>(
7375
requestInit: FapiRequestInit,
7476
opts: BaseFetchOptions = {},
@@ -85,6 +87,7 @@ export abstract class BaseResource {
8587
} catch (e) {
8688
// TODO: This should be the default behavior in the next major version, as long as we have a way to handle the requests more gracefully when offline
8789
if (this.shouldRethrowOfflineNetworkErrors()) {
90+
// TODO @userland-errors:
8891
throw new ClerkRuntimeError(e?.message || e, {
8992
code: 'network_error',
9093
});
@@ -201,6 +204,7 @@ export abstract class BaseResource {
201204

202205
protected async _baseMutate<J extends ClerkResourceJSON | null>(params: BaseMutateParams): Promise<this> {
203206
const { action, body, method, path } = params;
207+
// TODO @userland-errors:
204208
const json = await BaseResource._fetch<J>({ method, path: path || this.path(action), body });
205209
return this.fromJSON((json?.response || json) as J);
206210
}

packages/clerk-js/src/core/resources/SignIn.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,7 @@ class SignInFuture implements SignInFutureResource {
692692
}
693693

694694
return runAsyncResourceTask(this.resource, async () => {
695+
// TODO @userland-errors:
695696
const identifier = params.identifier || params.email || params.phoneNumber;
696697
const previousIdentifier = this.resource.identifier;
697698
await this.resource.__internal_basePost({

packages/clerk-js/src/utils/querystring.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type StringifyQueryParamsOptions = {
2121
keyEncoder?: (key: string) => string;
2222
};
2323

24+
// TODO @userland-errors:
2425
export const stringifyQueryParams = (
2526
params:
2627
| Record<string, string | undefined | null | object | boolean | Array<string | undefined | null>>

packages/clerk-js/src/utils/runAsyncResourceTask.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export async function runAsyncResourceTask<T>(
2020
return { result, error: null };
2121
} catch (err) {
2222
eventBus.emit('resource:error', { resource, error: err });
23+
// TODO @userland-errors:
2324
return { error: err };
2425
} finally {
2526
eventBus.emit('resource:fetch', {

packages/react/src/isomorphicClerk.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@ export class IsomorphicClerk implements IsomorphicLoadedClerk {
746746
}
747747
}
748748

749+
// TODO @userland-errors:
749750
__unstable__updateProps = async (props: any): Promise<void> => {
750751
const clerkjs = await this.#waitForClerkJS();
751752
// Handle case where accounts has clerk-react@4 installed, but clerk-js@3 is manually loaded

0 commit comments

Comments
 (0)