diff --git a/src/helpers/error-handler.ts b/src/helpers/error-handler.ts index 3ea58e5..3d04dbd 100644 --- a/src/helpers/error-handler.ts +++ b/src/helpers/error-handler.ts @@ -1,6 +1,7 @@ -import log from 'loglevel' +import { errors } from '@/errors' import { bus, BUS_EVENTS } from '@/helpers' import { i18n } from '@/localization' +import log from 'loglevel' export class ErrorHandler { static process(error: Error | unknown, errorMessage = ''): void { @@ -15,14 +16,70 @@ export class ErrorHandler { } static _getErrorMessage(error: Error | unknown): string { + const { t } = i18n.global let errorMessage = '' - if (error instanceof Error) + if (error instanceof Error) { switch (error.constructor) { + case errors.ProviderChainNotFoundError: + errorMessage = t('errors.provider-chain-not-found-error') + break + case errors.ProviderUserRejectedRequest: + errorMessage = t('errors.provider-user-rejected-request') + break + case errors.ProviderUnauthorized: + errorMessage = t('errors.provider-unauthorized') + break + case errors.ProviderUnsupportedMethod: + errorMessage = t('errors.provider-unsupported-method') + break + case errors.ProviderDisconnected: + errorMessage = t('errors.provider-disconnected') + break + case errors.ProviderChainDisconnected: + errorMessage = t('errors.provider-chain-disconnected') + break + case errors.ProviderParseError: + errorMessage = t('errors.provider-parse-error') + break + case errors.ProviderInvalidRequest: + errorMessage = t('errors.provider-invalid-request') + break + case errors.ProviderMethodNotFound: + errorMessage = t('errors.provider-method-not-found') + break + case errors.ProviderInvalidParams: + errorMessage = t('errors.provider-invalid-params') + break + case errors.ProviderInternalError: + errorMessage = t('errors.provider-internal-error') + break + case errors.ProviderInvalidInput: + errorMessage = t('errors.provider-invalid-input') + break + case errors.ProviderResourceNotFound: + errorMessage = t('errors.provider-resource-not-found') + break + case errors.ProviderResourceUnavailable: + errorMessage = t('errors.provider-resource-unavailable') + break + case errors.ProviderTransactionRejected: + errorMessage = t('errors.provider-transaction-rejected') + break + case errors.ProviderMethodNotSupported: + errorMessage = t('errors.provider-method-not-supported') + break + case errors.ProviderLimitExceeded: + errorMessage = t('errors.provider-limit-exceeded') + break + case errors.ProviderJsonRpcVersionNotSupported: + errorMessage = t('errors.provider-json-rpc-version-not-supported') + break default: { - errorMessage = i18n.global.t('errors.default') + errorMessage = t('errors.default') } } + } return errorMessage } diff --git a/src/localization/resources/en.json b/src/localization/resources/en.json index 1fa3f00..dd91283 100644 --- a/src/localization/resources/en.json +++ b/src/localization/resources/en.json @@ -105,7 +105,25 @@ "available-to-withdraw-title": "Available to withdraw" }, "errors": { - "default": "Something went wrong" + "default": "Something went wrong", + "provider-user-rejected-request": "User rejected request", + "provider-unauthorized": "The requested method and/or account has not been authorized by the user", + "provider-unsupported-method": "The Provider does not support the requested method", + "provider-disconnected": "The Provider is disconnected from all chains", + "provider-chain-disconnected": "The Provider is not connected to the requested chain", + "provider-parse-error": "Invalid JSON", + "provider-invalid-request": "JSON is not a valid request object", + "provider-method-not-found": "Method does not exist", + "provider-invalid-params": "Invalid method parameters", + "provider-internal-error": "Internal JSON-RPC error", + "provider-invalid-input": "Missing or invalid parameters", + "provider-resource-not-found": "Requested resource not found", + "provider-resource-unavailable": "MetaMask is busy", + "provider-transaction-rejected": "Transaction creation failed", + "provider-method-not-supported": "Method is not implemented", + "provider-limit-exceeded": "Request exceeds defined limit", + "provider-json-rpc-version-not-supported": "Version of JSON-RPC protocol is not supported", + "provider-wrapper-method-not-found": "Provider does not exist or method not supported" }, "notification": { "default-title-success": "Success", diff --git a/src/store/modules/web3-providers.module.ts b/src/store/modules/web3-providers.module.ts index 72a9a27..ed99d70 100644 --- a/src/store/modules/web3-providers.module.ts +++ b/src/store/modules/web3-providers.module.ts @@ -3,6 +3,7 @@ import { ETHEREUM_CHAINS, SUPPORTED_PROVIDERS } from '@/enums' import { type BigNumber } from '@/types' import { config } from '@config' import { defineStore } from 'pinia' +import { ProviderDetector } from '@distributedlab/w3p' const STORE_NAME = 'web3-providers-store' @@ -29,7 +30,11 @@ export const useWeb3ProvidersStore = defineStore(STORE_NAME, { actions: { async init() { - await this.provider.selectProvider(SUPPORTED_PROVIDERS.Metamask) + const providerDetector = new ProviderDetector() + await providerDetector.init() + + if (providerDetector.providers.metamask) + await this.provider.selectProvider(SUPPORTED_PROVIDERS.Metamask) }, }, })