@@ -14,13 +14,37 @@ export interface MetamaskError extends Error {
1414}
1515
1616export function isKnownError ( error : any ) {
17- return isClerkAPIResponseError ( error ) || isMetamaskError ( error ) ;
17+ return isClerkAPIResponseError ( error ) || isMetamaskError ( error ) || isClerkRuntimeError ( error ) ;
1818}
1919
2020export function isClerkAPIResponseError ( err : any ) : err is ClerkAPIResponseError {
21+ if ( err instanceof ClerkAPIResponseError ) {
22+ return true ;
23+ }
24+
2125 return 'clerkError' in err ;
2226}
2327
28+ /**
29+ * Checks if the provided error object is an instance of ClerkRuntimeError.
30+ *
31+ * @param {any } err - The error object to check.
32+ * @returns {boolean } True if the error is a ClerkRuntimeError, false otherwise.
33+ *
34+ * @example
35+ * const error = new ClerkRuntimeError('An error occurred');
36+ * if (isClerkRuntimeError(error)) {
37+ * // Handle ClerkRuntimeError
38+ * console.error('ClerkRuntimeError:', error.message);
39+ * } else {
40+ * // Handle other errors
41+ * console.error('Other error:', error.message);
42+ * }
43+ */
44+ export function isClerkRuntimeError ( err : any ) : err is ClerkRuntimeError {
45+ return err instanceof ClerkRuntimeError ;
46+ }
47+
2448export function isMetamaskError ( err : any ) : err is MetamaskError {
2549 return 'code' in err && [ 4001 , 32602 , 32603 ] . includes ( err . code ) && 'message' in err ;
2650}
@@ -44,8 +68,6 @@ export function parseError(error: ClerkAPIErrorJSON): ClerkAPIError {
4468}
4569
4670export class ClerkAPIResponseError extends Error {
47- clerkError : true ;
48-
4971 status : number ;
5072 message : string ;
5173
@@ -58,7 +80,6 @@ export class ClerkAPIResponseError extends Error {
5880
5981 this . status = status ;
6082 this . message = message ;
61- this . clerkError = true ;
6283 this . errors = parseErrors ( data ) ;
6384 }
6485
@@ -69,6 +90,49 @@ export class ClerkAPIResponseError extends Error {
6990 } ;
7091}
7192
93+ /**
94+ * Custom error class for representing Clerk runtime errors.
95+ *
96+ * @class ClerkRuntimeError
97+ * @example
98+ * throw new ClerkRuntimeError('An error occurred', { code: 'password_invalid' });
99+ */
100+ export class ClerkRuntimeError extends Error {
101+ /**
102+ * The error message.
103+ *
104+ * @type {string }
105+ * @memberof ClerkRuntimeError
106+ */
107+ message : string ;
108+
109+ /**
110+ * A unique code identifying the error, used for localization
111+ *
112+ * @type {string }
113+ * @memberof ClerkRuntimeError
114+ */
115+ code : string ;
116+ constructor ( message : string , { code } : { code : string } ) {
117+ super ( message ) ;
118+
119+ Object . setPrototypeOf ( this , ClerkRuntimeError . prototype ) ;
120+
121+ this . code = code ;
122+ this . message = message ;
123+ }
124+
125+ /**
126+ * Returns a string representation of the error.
127+ *
128+ * @returns {string } A formatted string with the error name and message.
129+ * @memberof ClerkRuntimeError
130+ */
131+ public toString = ( ) => {
132+ return `[${ this . name } ]\nMessage:${ this . message } ` ;
133+ } ;
134+ }
135+
72136export class MagicLinkError extends Error {
73137 code : string ;
74138
0 commit comments