-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(repo): Move error helpers to shared package (#1308)
test(chrome-extension): Update shapshots
- Loading branch information
Showing
13 changed files
with
145 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
'@clerk/nextjs': minor | ||
'@clerk/shared': minor | ||
'@clerk/clerk-react': minor | ||
'@clerk/clerk-expo': minor | ||
--- | ||
|
||
Export error helpers from the shared package to the framework specific packages |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,12 @@ | ||
import type { ClerkAPIError, ClerkAPIErrorJSON } from '@clerk/types'; | ||
|
||
interface ClerkAPIResponseOptions { | ||
data: ClerkAPIErrorJSON[]; | ||
status: number; | ||
} | ||
|
||
// For a comprehensive Metamask error list, please see | ||
// https://docs.metamask.io/guide/ethereum-provider.html#errors | ||
export interface MetamaskError extends Error { | ||
code: 4001 | 32602 | 32603; | ||
message: string; | ||
data?: unknown; | ||
} | ||
|
||
export function isKnownError(error: any) { | ||
return isClerkAPIResponseError(error) || isMetamaskError(error); | ||
} | ||
|
||
export function isClerkAPIResponseError(err: any): err is ClerkAPIResponseError { | ||
return 'clerkError' in err; | ||
} | ||
|
||
export function isMetamaskError(err: any): err is MetamaskError { | ||
return 'code' in err && [4001, 32602, 32603].includes(err.code) && 'message' in err; | ||
} | ||
|
||
export function parseErrors(data: ClerkAPIErrorJSON[] = []): ClerkAPIError[] { | ||
return data.length > 0 ? data.map(parseError) : []; | ||
} | ||
|
||
export function parseError(error: ClerkAPIErrorJSON): ClerkAPIError { | ||
return { | ||
code: error.code, | ||
message: error.message, | ||
longMessage: error.long_message, | ||
meta: { | ||
paramName: error?.meta?.param_name, | ||
sessionId: error?.meta?.session_id, | ||
emailAddresses: error?.meta?.email_addresses, | ||
}, | ||
}; | ||
} | ||
|
||
export class ClerkAPIResponseError extends Error { | ||
clerkError: true; | ||
|
||
status: number; | ||
message: string; | ||
|
||
errors: ClerkAPIError[]; | ||
|
||
constructor(message: string, { data, status }: ClerkAPIResponseOptions) { | ||
super(message); | ||
|
||
Object.setPrototypeOf(this, ClerkAPIResponseError.prototype); | ||
|
||
this.status = status; | ||
this.message = message; | ||
this.clerkError = true; | ||
this.errors = parseErrors(data); | ||
} | ||
|
||
public toString = () => { | ||
return `[${this.name}]\nMessage:${this.message}\nStatus:${this.status}\nSerialized errors: ${this.errors.map(e => | ||
JSON.stringify(e), | ||
)}`; | ||
}; | ||
} | ||
|
||
export class MagicLinkError extends Error { | ||
code: string; | ||
|
||
constructor(code: string) { | ||
super(code); | ||
this.code = code; | ||
Object.setPrototypeOf(this, MagicLinkError.prototype); | ||
} | ||
} | ||
// Check if the error is a MagicLinkError. | ||
|
||
export function isMagicLinkError(err: Error): err is MagicLinkError { | ||
return err instanceof MagicLinkError; | ||
} | ||
|
||
export const MagicLinkErrorCode = { | ||
Expired: 'expired', | ||
Failed: 'failed', | ||
}; | ||
export { | ||
isKnownError, | ||
isMagicLinkError, | ||
isMetamaskError, | ||
isClerkAPIResponseError, | ||
MagicLinkErrorCode, | ||
parseError, | ||
parseErrors, | ||
MagicLinkError, | ||
ClerkAPIResponseError, | ||
} from '@clerk/shared'; | ||
export type { MetamaskError } from '@clerk/shared'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
packages/clerk-js/src/ui/common/__tests__/verification.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/clerk-js/src/ui/components/OrganizationProfile/InviteMembersForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import type { ClerkAPIError, ClerkAPIErrorJSON } from '@clerk/types'; | ||
|
||
interface ClerkAPIResponseOptions { | ||
data: ClerkAPIErrorJSON[]; | ||
status: number; | ||
} | ||
|
||
// For a comprehensive Metamask error list, please see | ||
// https://docs.metamask.io/guide/ethereum-provider.html#errors | ||
export interface MetamaskError extends Error { | ||
code: 4001 | 32602 | 32603; | ||
message: string; | ||
data?: unknown; | ||
} | ||
|
||
export function isKnownError(error: any) { | ||
return isClerkAPIResponseError(error) || isMetamaskError(error); | ||
} | ||
|
||
export function isClerkAPIResponseError(err: any): err is ClerkAPIResponseError { | ||
return 'clerkError' in err; | ||
} | ||
|
||
export function isMetamaskError(err: any): err is MetamaskError { | ||
return 'code' in err && [4001, 32602, 32603].includes(err.code) && 'message' in err; | ||
} | ||
|
||
export function parseErrors(data: ClerkAPIErrorJSON[] = []): ClerkAPIError[] { | ||
return data.length > 0 ? data.map(parseError) : []; | ||
} | ||
|
||
export function parseError(error: ClerkAPIErrorJSON): ClerkAPIError { | ||
return { | ||
code: error.code, | ||
message: error.message, | ||
longMessage: error.long_message, | ||
meta: { | ||
paramName: error?.meta?.param_name, | ||
sessionId: error?.meta?.session_id, | ||
emailAddresses: error?.meta?.email_addresses, | ||
}, | ||
}; | ||
} | ||
|
||
export class ClerkAPIResponseError extends Error { | ||
clerkError: true; | ||
|
||
status: number; | ||
message: string; | ||
|
||
errors: ClerkAPIError[]; | ||
|
||
constructor(message: string, { data, status }: ClerkAPIResponseOptions) { | ||
super(message); | ||
|
||
Object.setPrototypeOf(this, ClerkAPIResponseError.prototype); | ||
|
||
this.status = status; | ||
this.message = message; | ||
this.clerkError = true; | ||
this.errors = parseErrors(data); | ||
} | ||
|
||
public toString = () => { | ||
return `[${this.name}]\nMessage:${this.message}\nStatus:${this.status}\nSerialized errors: ${this.errors.map(e => | ||
JSON.stringify(e), | ||
)}`; | ||
}; | ||
} | ||
|
||
export class MagicLinkError extends Error { | ||
code: string; | ||
|
||
constructor(code: string) { | ||
super(code); | ||
this.code = code; | ||
Object.setPrototypeOf(this, MagicLinkError.prototype); | ||
} | ||
} | ||
// Check if the error is a MagicLinkError. | ||
|
||
export function isMagicLinkError(err: Error): err is MagicLinkError { | ||
return err instanceof MagicLinkError; | ||
} | ||
|
||
export const MagicLinkErrorCode = { | ||
Expired: 'expired', | ||
Failed: 'failed', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters