From 388e6d676ffd0bd76d4973b7f3e2c90c459daafb Mon Sep 17 00:00:00 2001 From: MK <53529533+magiziz@users.noreply.github.com> Date: Wed, 11 Dec 2024 12:29:18 +0000 Subject: [PATCH] fix: debug mode not working with universal provider (#3429) --- .changeset/bright-ads-relate.md | 23 +++++++++++ packages/adapters/solana/src/client.ts | 2 +- .../src/connectors/AuthConnectorExport.ts | 2 +- packages/appkit/src/client.ts | 41 +++++++++++++++++-- packages/appkit/src/tests/appkit.test.ts | 27 +++++++++++- .../core/src/controllers/OptionsController.ts | 2 +- 6 files changed, 90 insertions(+), 7 deletions(-) create mode 100644 .changeset/bright-ads-relate.md diff --git a/.changeset/bright-ads-relate.md b/.changeset/bright-ads-relate.md new file mode 100644 index 0000000000..488e6ebc5d --- /dev/null +++ b/.changeset/bright-ads-relate.md @@ -0,0 +1,23 @@ +--- +'@reown/appkit-adapter-solana': patch +'@reown/appkit-adapter-wagmi': patch +'@reown/appkit': patch +'@reown/appkit-core': patch +'@apps/builder': patch +'@reown/appkit-adapter-ethers': patch +'@reown/appkit-adapter-ethers5': patch +'@reown/appkit-utils': patch +'@reown/appkit-cdn': patch +'@reown/appkit-cli': patch +'@reown/appkit-common': patch +'@reown/appkit-experimental': patch +'@reown/appkit-polyfills': patch +'@reown/appkit-scaffold-ui': patch +'@reown/appkit-siwe': patch +'@reown/appkit-siwx': patch +'@reown/appkit-ui': patch +'@reown/appkit-wallet': patch +'@reown/appkit-wallet-button': patch +--- + +Debug mode is now set to true by default. Additionally fixed an issue where alerts and console errors were not working in debug mode. \ No newline at end of file diff --git a/packages/adapters/solana/src/client.ts b/packages/adapters/solana/src/client.ts index 4795eb4994..f150bf9fb4 100644 --- a/packages/adapters/solana/src/client.ts +++ b/packages/adapters/solana/src/client.ts @@ -85,7 +85,7 @@ export class SolanaAdapter extends AdapterBlueprint { projectId: options.projectId, chainId: withSolanaNamespace(appKit?.getCaipNetwork(this.namespace)?.id), onTimeout: () => { - AlertController.open(ErrorUtil.ALERT_ERRORS.INVALID_APP_CONFIGURATION, 'error') + AlertController.open(ErrorUtil.ALERT_ERRORS.SOCIALS_TIMEOUT, 'error') } }) diff --git a/packages/adapters/wagmi/src/connectors/AuthConnectorExport.ts b/packages/adapters/wagmi/src/connectors/AuthConnectorExport.ts index 684fe0f86a..279e382bc7 100644 --- a/packages/adapters/wagmi/src/connectors/AuthConnectorExport.ts +++ b/packages/adapters/wagmi/src/connectors/AuthConnectorExport.ts @@ -19,7 +19,7 @@ export function authConnector(parameters: AuthParameters) { provider: W3mFrameProviderSingleton.getInstance({ projectId: parameters.options.projectId, onTimeout: () => { - AlertController.open(ErrorUtil.ALERT_ERRORS.INVALID_APP_CONFIGURATION, 'error') + AlertController.open(ErrorUtil.ALERT_ERRORS.SOCIALS_TIMEOUT, 'error') } }) }) diff --git a/packages/appkit/src/client.ts b/packages/appkit/src/client.ts index ce9f210a97..e50daeee28 100644 --- a/packages/appkit/src/client.ts +++ b/packages/appkit/src/client.ts @@ -65,6 +65,7 @@ import { import { CaipNetworksUtil, ErrorUtil, + LoggerUtil, ConstantsUtil as UtilConstantsUtil } from '@reown/appkit-utils' import { @@ -175,6 +176,8 @@ export class AppKit { public adapter?: ChainAdapter + public reportedAlertErrors: Record = {} + private caipNetworks?: [CaipNetwork, ...CaipNetwork[]] private defaultCaipNetwork?: CaipNetwork @@ -645,7 +648,7 @@ export class AppKit { sdkVersion: SdkVersion } ) { - OptionsController.setDebug(options.debug) + OptionsController.setDebug(options.debug !== false) OptionsController.setProjectId(options.projectId) OptionsController.setSdkVersion(options.sdkVersion) OptionsController.setEnableEmbedded(options.enableEmbedded) @@ -1714,7 +1717,35 @@ export class AppKit { return this.universalProviderInitPromise } + private handleAlertError(error: Error) { + const matchedUniversalProviderError = Object.entries(ErrorUtil.UniversalProviderErrors).find( + ([, { message }]) => error.message.includes(message) + ) + + const [errorKey, errorValue] = matchedUniversalProviderError ?? [] + + const { message, alertErrorKey } = errorValue ?? {} + + if (errorKey && message && !this.reportedAlertErrors[errorKey]) { + const alertError = + ErrorUtil.ALERT_ERRORS[alertErrorKey as keyof typeof ErrorUtil.ALERT_ERRORS] + + if (alertError) { + AlertController.open(alertError, 'error') + this.reportedAlertErrors[errorKey] = true + } + } + } + private async initializeUniversalAdapter() { + const logger = LoggerUtil.createLogger((error, ...args) => { + if (error) { + this.handleAlertError(error) + } + // eslint-disable-next-line no-console + console.error(...args) + }) + const universalProviderOptions: UniversalProviderOpts = { projectId: this.options?.projectId, metadata: { @@ -1722,7 +1753,8 @@ export class AppKit { description: this.options?.metadata ? this.options?.metadata.description : '', url: this.options?.metadata ? this.options?.metadata.url : '', icons: this.options?.metadata ? this.options?.metadata.icons : [''] - } + }, + logger } this.universalProvider = await UniversalProvider.init(universalProviderOptions) @@ -1750,7 +1782,10 @@ export class AppKit { : CoreConstantsUtil.DEFAULT_FEATURES.socials if (this.options?.projectId && (emailEnabled || socialsEnabled)) { this.authProvider = W3mFrameProviderSingleton.getInstance({ - projectId: this.options.projectId + projectId: this.options.projectId, + onTimeout: () => { + AlertController.open(ErrorUtil.ALERT_ERRORS.SOCIALS_TIMEOUT, 'error') + } }) this.listenAuthConnector(this.authProvider) } diff --git a/packages/appkit/src/tests/appkit.test.ts b/packages/appkit/src/tests/appkit.test.ts index fd207e9cc1..49e32d7bd3 100644 --- a/packages/appkit/src/tests/appkit.test.ts +++ b/packages/appkit/src/tests/appkit.test.ts @@ -19,7 +19,8 @@ import { ChainController, type Connector, StorageUtil, - CoreHelperUtil + CoreHelperUtil, + AlertController } from '@reown/appkit-core' import { SafeLocalStorage, @@ -31,6 +32,7 @@ import { mockOptions } from './mocks/Options' import { UniversalAdapter } from '../universal-adapter/client' import type { AdapterBlueprint } from '../adapters/ChainAdapterBlueprint' import { ProviderUtil } from '../store' +import { ErrorUtil } from '@reown/appkit-utils' // Mock all controllers and UniversalAdapterClient vi.mock('@reown/appkit-core') @@ -864,4 +866,27 @@ describe('Base', () => { ) }) }) + + describe('Alert Errors', () => { + it('should handle alert errors based on error messages', () => { + const errors = [ + { + alert: ErrorUtil.ALERT_ERRORS.INVALID_APP_CONFIGURATION, + message: + 'Error: WebSocket connection closed abnormally with code: 3000 (Unauthorized: origin not allowed)' + }, + { + alert: ErrorUtil.ALERT_ERRORS.JWT_TOKEN_NOT_VALID, + message: + 'WebSocket connection closed abnormally with code: 3000 (JWT validation error: JWT Token is not yet valid:)' + } + ] + + for (const { alert, message } of errors) { + // @ts-expect-error + appKit.handleAlertError(new Error(message)) + expect(AlertController.open).toHaveBeenCalledWith(alert, 'error') + } + }) + }) }) diff --git a/packages/core/src/controllers/OptionsController.ts b/packages/core/src/controllers/OptionsController.ts index 69ccea4621..ed3dd2d6b8 100644 --- a/packages/core/src/controllers/OptionsController.ts +++ b/packages/core/src/controllers/OptionsController.ts @@ -110,7 +110,7 @@ export interface OptionsControllerStatePublic { enableWalletGuide?: boolean /** * Enable or disable debug mode in your AppKit. This is useful if you want to see UI alerts when debugging. - * @default false + * @default true */ debug?: boolean /**