Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sweet-singers-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/shared': patch
---

Refactor internal Clerk error handling functions
10 changes: 4 additions & 6 deletions packages/shared/src/error.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
export { errorToJSON, parseError, parseErrors } from './errors/parseError';

export { ClerkAPIError } from './errors/clerkApiError';
export { ClerkAPIResponseError } from './errors/clerkApiResponseError';
export { ClerkError } from './errors/clerkError';
export { ClerkAPIError, isClerkAPIError } from './errors/clerkApiError';
export { ClerkAPIResponseError, isClerkAPIResponseError } from './errors/clerkApiResponseError';
export { ClerkError, isClerkError } from './errors/clerkError';

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

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

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

export { ClerkRuntimeError } from './errors/clerkRuntimeError';
export { ClerkRuntimeError, isClerkRuntimeError } from './errors/clerkRuntimeError';

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

export {
is4xxError,
isCaptchaError,
isClerkAPIResponseError,
isClerkRuntimeError,
isEmailLinkError,
isKnownError,
isMetamaskError,
Expand Down
9 changes: 5 additions & 4 deletions packages/shared/src/errors/clerkApiError.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { ClerkAPIError as ClerkAPIErrorInterface, ClerkAPIErrorJSON } from '../types';
import { createErrorTypeGuard } from './createErrorTypeGuard';

export type ClerkApiErrorMeta = Record<string, unknown>;
export type ClerkAPIErrorMeta = Record<string, unknown>;

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

/**
* Type guard to check if a value is a ClerkApiError instance.
* Type guard to check if a value is a ClerkAPIError instance.
*/
export const isClerkApiError = createErrorTypeGuard(ClerkAPIError);
export const isClerkAPIError = createErrorTypeGuard(ClerkAPIError);
8 changes: 4 additions & 4 deletions packages/shared/src/errors/clerkApiResponseError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ export class ClerkAPIResponseError extends ClerkError implements ClerkAPIRespons
}

/**
* Type guard to check if an error is a ClerkApiResponseError.
* Type guard to check if an error is a ClerkAPIResponseError.
* Can be called as a standalone function or as a method on an error object.
*
* @example
* // As a standalone function
* if (isClerkApiResponseError(error)) { ... }
* if (isClerkAPIResponseError(error)) { ... }
*
* // As a method (when attached to error object)
* if (error.isClerkApiResponseError()) { ... }
* if (error.isClerkAPIResponseError()) { ... }
*/
export const isClerkApiResponseError = createErrorTypeGuard(ClerkAPIResponseError);
export const isClerkAPIResponseError = createErrorTypeGuard(ClerkAPIResponseError);
4 changes: 2 additions & 2 deletions packages/shared/src/errors/globalHookError.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isClerkApiResponseError } from './clerkApiResponseError';
import { isClerkAPIResponseError } from './clerkApiResponseError';
import type { ClerkError } from './clerkError';
import { isClerkRuntimeError } from './clerkRuntimeError';

Expand All @@ -9,7 +9,7 @@ import { isClerkRuntimeError } from './clerkRuntimeError';
*/
export function createClerkGlobalHookError(error: ClerkError) {
const predicates = {
isClerkApiResponseError,
isClerkAPIResponseError,
isClerkRuntimeError,
} as const;

Expand Down
31 changes: 2 additions & 29 deletions packages/shared/src/errors/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { ClerkAPIResponseError } from './clerkApiResponseError';
import { isClerkAPIResponseError } from './clerkApiResponseError';
import type { ClerkRuntimeError } from './clerkRuntimeError';
import { isClerkRuntimeError } from './clerkRuntimeError';
import type { EmailLinkError } from './emailLinkError';
import type { MetamaskError } from './metamaskError';

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

/**
* Checks if the provided error is a ClerkAPIResponseError.
*
* @internal
*/
export function isClerkAPIResponseError(err: any): err is ClerkAPIResponseError {
return err && 'clerkError' in err;
}

/**
* Checks if the provided error object is an instance of ClerkRuntimeError.
*
* @param err - The error object to check.
* @returns True if the error is a ClerkRuntimeError, false otherwise.
*
* @example
* const error = new ClerkRuntimeError('An error occurred');
* if (isClerkRuntimeError(error)) {
* // Handle ClerkRuntimeError
* console.error('ClerkRuntimeError:', error.message);
* } else {
* // Handle other errors
* console.error('Other error:', error.message);
* }
*/
export function isClerkRuntimeError(err: any): err is ClerkRuntimeError {
return 'clerkRuntimeError' in err;
}

/**
* Checks if the provided error is a Clerk runtime error indicating a reverification was cancelled.
*
Expand Down
Loading