Skip to content

Commit 2cd4da9

Browse files
authored
refactor(shared): Update exported error typeguard functions from error.ts, align error class naming (#7490)
1 parent b3b02b4 commit 2cd4da9

File tree

6 files changed

+22
-45
lines changed

6 files changed

+22
-45
lines changed

.changeset/sweet-singers-beg.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/shared': patch
3+
---
4+
5+
Refactor internal Clerk error handling functions

packages/shared/src/error.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
export { errorToJSON, parseError, parseErrors } from './errors/parseError';
22

3-
export { ClerkAPIError } from './errors/clerkApiError';
4-
export { ClerkAPIResponseError } from './errors/clerkApiResponseError';
5-
export { ClerkError } from './errors/clerkError';
3+
export { ClerkAPIError, isClerkAPIError } from './errors/clerkApiError';
4+
export { ClerkAPIResponseError, isClerkAPIResponseError } from './errors/clerkApiResponseError';
5+
export { ClerkError, isClerkError } from './errors/clerkError';
66

77
export { buildErrorThrower, type ErrorThrower, type ErrorThrowerOptions } from './errors/errorThrower';
88

99
export { EmailLinkError, EmailLinkErrorCode, EmailLinkErrorCodeStatus } from './errors/emailLinkError';
1010

1111
export type { MetamaskError } from './errors/metamaskError';
1212

13-
export { ClerkRuntimeError } from './errors/clerkRuntimeError';
13+
export { ClerkRuntimeError, isClerkRuntimeError } from './errors/clerkRuntimeError';
1414

1515
export { ClerkWebAuthnError } from './errors/webAuthNError';
1616

1717
export {
1818
is4xxError,
1919
isCaptchaError,
20-
isClerkAPIResponseError,
21-
isClerkRuntimeError,
2220
isEmailLinkError,
2321
isKnownError,
2422
isMetamaskError,

packages/shared/src/errors/clerkApiError.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import type { ClerkAPIError as ClerkAPIErrorInterface, ClerkAPIErrorJSON } from '../types';
22
import { createErrorTypeGuard } from './createErrorTypeGuard';
33

4-
export type ClerkApiErrorMeta = Record<string, unknown>;
4+
export type ClerkAPIErrorMeta = Record<string, unknown>;
55

66
/**
77
* This error contains the specific error message, code, and any additional metadata that was returned by the Clerk API.
88
*/
9-
export class ClerkAPIError<Meta extends ClerkApiErrorMeta = any> implements ClerkAPIErrorInterface {
9+
export class ClerkAPIError<Meta extends ClerkAPIErrorMeta = any> implements ClerkAPIErrorInterface {
10+
// TODO: Update kind to match class name in Core 3
1011
static kind = 'ClerkApiError';
1112
readonly code: string;
1213
readonly message: string;
@@ -36,6 +37,6 @@ export class ClerkAPIError<Meta extends ClerkApiErrorMeta = any> implements Cler
3637
}
3738

3839
/**
39-
* Type guard to check if a value is a ClerkApiError instance.
40+
* Type guard to check if a value is a ClerkAPIError instance.
4041
*/
41-
export const isClerkApiError = createErrorTypeGuard(ClerkAPIError);
42+
export const isClerkAPIError = createErrorTypeGuard(ClerkAPIError);

packages/shared/src/errors/clerkApiResponseError.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ export class ClerkAPIResponseError extends ClerkError implements ClerkAPIRespons
4747
}
4848

4949
/**
50-
* Type guard to check if an error is a ClerkApiResponseError.
50+
* Type guard to check if an error is a ClerkAPIResponseError.
5151
* Can be called as a standalone function or as a method on an error object.
5252
*
5353
* @example
5454
* // As a standalone function
55-
* if (isClerkApiResponseError(error)) { ... }
55+
* if (isClerkAPIResponseError(error)) { ... }
5656
*
5757
* // As a method (when attached to error object)
58-
* if (error.isClerkApiResponseError()) { ... }
58+
* if (error.isClerkAPIResponseError()) { ... }
5959
*/
60-
export const isClerkApiResponseError = createErrorTypeGuard(ClerkAPIResponseError);
60+
export const isClerkAPIResponseError = createErrorTypeGuard(ClerkAPIResponseError);

packages/shared/src/errors/globalHookError.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isClerkApiResponseError } from './clerkApiResponseError';
1+
import { isClerkAPIResponseError } from './clerkApiResponseError';
22
import type { ClerkError } from './clerkError';
33
import { isClerkRuntimeError } from './clerkRuntimeError';
44

@@ -9,7 +9,7 @@ import { isClerkRuntimeError } from './clerkRuntimeError';
99
*/
1010
export function createClerkGlobalHookError(error: ClerkError) {
1111
const predicates = {
12-
isClerkApiResponseError,
12+
isClerkAPIResponseError,
1313
isClerkRuntimeError,
1414
} as const;
1515

packages/shared/src/errors/helpers.ts

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { ClerkAPIResponseError } from './clerkApiResponseError';
2+
import { isClerkAPIResponseError } from './clerkApiResponseError';
23
import type { ClerkRuntimeError } from './clerkRuntimeError';
4+
import { isClerkRuntimeError } from './clerkRuntimeError';
35
import type { EmailLinkError } from './emailLinkError';
46
import type { MetamaskError } from './metamaskError';
57

@@ -55,35 +57,6 @@ export function isKnownError(error: any): error is ClerkAPIResponseError | Clerk
5557
return isClerkAPIResponseError(error) || isMetamaskError(error) || isClerkRuntimeError(error);
5658
}
5759

58-
/**
59-
* Checks if the provided error is a ClerkAPIResponseError.
60-
*
61-
* @internal
62-
*/
63-
export function isClerkAPIResponseError(err: any): err is ClerkAPIResponseError {
64-
return err && 'clerkError' in err;
65-
}
66-
67-
/**
68-
* Checks if the provided error object is an instance of ClerkRuntimeError.
69-
*
70-
* @param err - The error object to check.
71-
* @returns True if the error is a ClerkRuntimeError, false otherwise.
72-
*
73-
* @example
74-
* const error = new ClerkRuntimeError('An error occurred');
75-
* if (isClerkRuntimeError(error)) {
76-
* // Handle ClerkRuntimeError
77-
* console.error('ClerkRuntimeError:', error.message);
78-
* } else {
79-
* // Handle other errors
80-
* console.error('Other error:', error.message);
81-
* }
82-
*/
83-
export function isClerkRuntimeError(err: any): err is ClerkRuntimeError {
84-
return 'clerkRuntimeError' in err;
85-
}
86-
8760
/**
8861
* Checks if the provided error is a Clerk runtime error indicating a reverification was cancelled.
8962
*

0 commit comments

Comments
 (0)