Skip to content

Commit 80e1117

Browse files
authoredJan 8, 2025··
chore(clerk-js,types,localizations): Improve error handling when web3 wallet is not installed (#4845)
1 parent d37115d commit 80e1117

File tree

8 files changed

+41
-4
lines changed

8 files changed

+41
-4
lines changed
 

‎.changeset/nasty-adults-shave.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@clerk/localizations': patch
3+
'@clerk/clerk-js': patch
4+
'@clerk/types': patch
5+
---
6+
7+
Improve error handling when trying to sign-in/sign-up with web3 wallet and wallet is not installed in the browser

‎packages/clerk-js/src/ui/components/SignIn/SignInSocialButtons.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { useCardState } from '../../elements/contexts';
88
import type { SocialButtonsProps } from '../../elements/SocialButtons';
99
import { SocialButtons } from '../../elements/SocialButtons';
1010
import { useRouter } from '../../router';
11-
import { handleError } from '../../utils';
11+
import { handleError, web3CallbackErrorHandler } from '../../utils';
1212

1313
export const SignInSocialButtons = React.memo((props: SocialButtonsProps) => {
1414
const clerk = useClerk();
@@ -36,7 +36,7 @@ export const SignInSocialButtons = React.memo((props: SocialButtonsProps) => {
3636
signUpContinueUrl: ctx.signUpContinueUrl,
3737
strategy,
3838
})
39-
.catch(err => handleError(err, [], card.setError));
39+
.catch(err => web3CallbackErrorHandler(err, card.setError));
4040
}}
4141
/>
4242
);

‎packages/clerk-js/src/ui/components/SignUp/SignUpSocialButtons.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { useCardState } from '../../elements';
77
import type { SocialButtonsProps } from '../../elements/SocialButtons';
88
import { SocialButtons } from '../../elements/SocialButtons';
99
import { useRouter } from '../../router';
10-
import { handleError } from '../../utils';
10+
import { handleError, web3CallbackErrorHandler } from '../../utils';
1111

1212
export type SignUpSocialButtonsProps = SocialButtonsProps & { continueSignUp?: boolean; legalAccepted?: boolean };
1313

@@ -46,7 +46,7 @@ export const SignUpSocialButtons = React.memo((props: SignUpSocialButtonsProps)
4646
strategy,
4747
legalAccepted: props.legalAccepted,
4848
})
49-
.catch(err => handleError(err, [], card.setError));
49+
.catch(err => web3CallbackErrorHandler(err, card.setError));
5050
}}
5151
/>
5252
);

‎packages/clerk-js/src/ui/utils/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ export * from './ExternalElementMounter';
2424
export * from './colorOptionToHslaScale';
2525
export * from './createCustomMenuItems';
2626
export * from './usernameUtils';
27+
export * from './web3CallbackErrorHandler';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ClerkRuntimeError, isClerkAPIResponseError } from '@clerk/shared/error';
2+
import type { ClerkAPIError, ClerkRuntimeError as ClerkRuntimeErrorType } from '@clerk/types';
3+
4+
import { handleError } from './errorHandler';
5+
6+
type Web3CallbackErrorHandler = {
7+
(err: any, setError?: (err: ClerkRuntimeErrorType | ClerkAPIError | string | undefined) => void): void;
8+
};
9+
10+
export const web3CallbackErrorHandler: Web3CallbackErrorHandler = (err, setError) => {
11+
if (
12+
isClerkAPIResponseError(err) &&
13+
err.errors[0].meta?.paramName === 'identifier' &&
14+
err.errors[0].code === 'form_param_nil'
15+
) {
16+
const error = new ClerkRuntimeError('A Web3 Wallet extension cannot be found. Please install one to continue.', {
17+
code: 'web3_missing_identifier',
18+
});
19+
20+
return handleError(error, [], setError);
21+
}
22+
return handleError(err, [], setError);
23+
};

‎packages/clerk-js/src/utils/web3.ts

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ type GetWeb3IdentifierParams = {
1010
export async function getWeb3Identifier(params: GetWeb3IdentifierParams): Promise<string> {
1111
const { provider } = params;
1212
const ethereum = await getEthereumProvider(provider);
13+
14+
// TODO - core-3: Improve error handling for the case when the provider is not found
1315
if (!ethereum) {
1416
// If a plugin for the requested provider is not found,
1517
// the flow will fail as it has been the expected behavior so far.
@@ -28,6 +30,8 @@ type GenerateWeb3SignatureParams = GenerateSignatureParams & {
2830
export async function generateWeb3Signature(params: GenerateWeb3SignatureParams): Promise<string> {
2931
const { identifier, nonce, provider } = params;
3032
const ethereum = await getEthereumProvider(provider);
33+
34+
// TODO - core-3: Improve error handling for the case when the provider is not found
3135
if (!ethereum) {
3236
// If a plugin for the requested provider is not found,
3337
// the flow will fail as it has been the expected behavior so far.

‎packages/localizations/src/en-US.ts

+1
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ export const enUS: LocalizationResource = {
582582
passkey_pa_not_supported: 'Registration requires a platform authenticator but the device does not support it.',
583583
passkey_registration_cancelled: 'Passkey registration was cancelled or timed out.',
584584
passkey_retrieval_cancelled: 'Passkey verification was cancelled or timed out.',
585+
web3_missing_identifier: 'A Web3 Wallet extension cannot be found. Please install one to continue.',
585586
passwordComplexity: {
586587
maximumLength: 'less than {{length}} characters',
587588
minimumLength: '{{length}} or more characters',

‎packages/types/src/localization.ts

+1
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,7 @@ type UnstableErrors = WithParamName<{
835835
passkey_retrieval_cancelled: LocalizationValue;
836836
passkey_registration_cancelled: LocalizationValue;
837837
passkey_already_exists: LocalizationValue;
838+
web3_missing_identifier: LocalizationValue;
838839
form_password_pwned: LocalizationValue;
839840
form_password_pwned__sign_in: LocalizationValue;
840841
form_username_invalid_length: LocalizationValue;

0 commit comments

Comments
 (0)
Please sign in to comment.