Skip to content

Commit

Permalink
fix: debug mode not working with universal provider (#3429)
Browse files Browse the repository at this point in the history
  • Loading branch information
magiziz authored Dec 11, 2024
1 parent b795289 commit 388e6d6
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 7 deletions.
23 changes: 23 additions & 0 deletions .changeset/bright-ads-relate.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion packages/adapters/solana/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
})
})
Expand Down
41 changes: 38 additions & 3 deletions packages/appkit/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import {
import {
CaipNetworksUtil,
ErrorUtil,
LoggerUtil,
ConstantsUtil as UtilConstantsUtil
} from '@reown/appkit-utils'
import {
Expand Down Expand Up @@ -175,6 +176,8 @@ export class AppKit {

public adapter?: ChainAdapter

public reportedAlertErrors: Record<string, boolean> = {}

private caipNetworks?: [CaipNetwork, ...CaipNetwork[]]

private defaultCaipNetwork?: CaipNetwork
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -1714,15 +1717,44 @@ 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: {
name: this.options?.metadata ? this.options?.metadata.name : '',
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)
Expand Down Expand Up @@ -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)
}
Expand Down
27 changes: 26 additions & 1 deletion packages/appkit/src/tests/appkit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import {
ChainController,
type Connector,
StorageUtil,
CoreHelperUtil
CoreHelperUtil,
AlertController
} from '@reown/appkit-core'
import {
SafeLocalStorage,
Expand All @@ -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')
Expand Down Expand Up @@ -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')
}
})
})
})
2 changes: 1 addition & 1 deletion packages/core/src/controllers/OptionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
/**
Expand Down

0 comments on commit 388e6d6

Please sign in to comment.