From b6edeca9a207fa0b3ce844a0fecab8e6f805dbd8 Mon Sep 17 00:00:00 2001 From: gantunesr Date: Sun, 13 Feb 2022 22:26:00 -0300 Subject: [PATCH 1/2] fix: Error message for missing chain id in deeplink --- app/constants/error.ts | 4 ++++ app/core/DeeplinkManager.js | 14 ++++++++++---- app/util/networks/index.js | 11 ++++++++--- locales/languages/en.json | 3 ++- 4 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 app/constants/error.ts diff --git a/app/constants/error.ts b/app/constants/error.ts new file mode 100644 index 00000000000..9a56b45ab81 --- /dev/null +++ b/app/constants/error.ts @@ -0,0 +1,4 @@ +// NETWORK ERRORS +export const NETWORK_ERROR_MISSING_NETWORK_ID = 'Missing network id'; +export const NETWORK_ERROR_UNKNOWN_NETWORK_ID = 'Unknown network with id'; +export const NETWORK_ERROR_MISSING_CHAIN_ID = 'Missing chain id'; diff --git a/app/core/DeeplinkManager.js b/app/core/DeeplinkManager.js index 922e3e67506..1f10c65334f 100644 --- a/app/core/DeeplinkManager.js +++ b/app/core/DeeplinkManager.js @@ -8,6 +8,7 @@ import WalletConnect from '../core/WalletConnect'; import AppConstants from './AppConstants'; import Engine from './Engine'; import { generateApproveData } from '../util/transactions'; +import { NETWORK_ERROR_MISSING_NETWORK_ID } from '../constants/error'; import { strings } from '../../locales/i18n'; import { getNetworkTypeById } from '../util/networks'; import { WalletDevice } from '@metamask/controllers/'; @@ -121,10 +122,15 @@ class DeeplinkManager { TransactionController.addTransaction(txParams, origin, WalletDevice.MM_MOBILE); } } catch (e) { - Alert.alert( - strings('send.network_not_found_title'), - strings('send.network_not_found_description', { chain_id: ethUrl.chain_id }) - ); + let alertMessage; + switch (e.message) { + case NETWORK_ERROR_MISSING_NETWORK_ID: + alertMessage = strings('send.network_missing_id'); + break; + default: + alertMessage = strings('send.network_not_found_description', { chain_id: ethUrl.chain_id }); + } + Alert.alert(strings('send.network_not_found_title'), alertMessage); } } diff --git a/app/util/networks/index.js b/app/util/networks/index.js index 2beb1c072b0..6e23ee3656c 100644 --- a/app/util/networks/index.js +++ b/app/util/networks/index.js @@ -2,6 +2,11 @@ import { colors } from '../../styles/common'; import URL from 'url-parse'; import AppConstants from '../../core/AppConstants'; import { MAINNET, ROPSTEN, KOVAN, RINKEBY, GOERLI, RPC } from '../../../app/constants/network'; +import { + NETWORK_ERROR_MISSING_NETWORK_ID, + NETWORK_ERROR_UNKNOWN_NETWORK_ID, + NETWORK_ERROR_MISSING_CHAIN_ID, +} from '../../../app/constants/error'; import { util } from '@metamask/controllers'; import Engine from '../../core/Engine'; import { toLowerCaseEquals } from './../general'; @@ -88,19 +93,19 @@ export const getNetworkName = (id) => NetworkListKeys.find((key) => NetworkList[ export function getNetworkTypeById(id) { if (!id) { - throw new Error('Missing network Id'); + throw new Error(NETWORK_ERROR_MISSING_NETWORK_ID); } const network = NetworkListKeys.filter((key) => NetworkList[key].networkId === parseInt(id, 10)); if (network.length > 0) { return network[0]; } - throw new Error(`Unknown network with id ${id}`); + throw new Error(`${NETWORK_ERROR_UNKNOWN_NETWORK_ID} ${id}`); } export function getDefaultNetworkByChainId(chainId) { if (!chainId) { - throw new Error('Missing chain Id'); + throw new Error(NETWORK_ERROR_MISSING_CHAIN_ID); } let returnNetwork; diff --git a/locales/languages/en.json b/locales/languages/en.json index 5b86f2ad7fc..44eba2c6c95 100644 --- a/locales/languages/en.json +++ b/locales/languages/en.json @@ -263,7 +263,8 @@ "amount": "Amount", "confirm": "Confirm", "network_not_found_title": "Network not found", - "network_not_found_description": "Network with chain id {{chain_id}} not found in your wallet. Please add the network first." + "network_not_found_description": "Network with chain id {{chain_id}} not found in your wallet. Please add the network first.", + "network_missing_id": "Missing chain id." }, "deposit": { "title": "Deposit" From 96a709e2c72a2fc3a044175580d488575739f645 Mon Sep 17 00:00:00 2001 From: gantunesr Date: Sun, 13 Feb 2022 22:55:50 -0300 Subject: [PATCH 2/2] test: update --- app/util/networks/index.test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/util/networks/index.test.ts b/app/util/networks/index.test.ts index 291c1941ea6..4fc44932339 100644 --- a/app/util/networks/index.test.ts +++ b/app/util/networks/index.test.ts @@ -1,5 +1,6 @@ import { isMainNet, getNetworkName, getAllNetworks, getNetworkTypeById } from '.'; import { MAINNET, ROPSTEN, GOERLI, RPC, KOVAN } from '../../../app/constants/network'; +import { NETWORK_ERROR_MISSING_NETWORK_ID, NETWORK_ERROR_UNKNOWN_NETWORK_ID } from '../../../app/constants/error'; describe('getAllNetworks', () => { const allNetworks = getAllNetworks(); @@ -69,7 +70,7 @@ describe('getNetworkTypeById', () => { try { getNetworkTypeById(); } catch (error) { - expect(error.message).toEqual('Missing network Id'); + expect(error.message).toEqual(NETWORK_ERROR_MISSING_NETWORK_ID); } }); it('should fail if network Id is unknown', () => { @@ -77,7 +78,7 @@ describe('getNetworkTypeById', () => { try { getNetworkTypeById(id); } catch (error) { - expect(error.message).toEqual(`Unknown network with id ${id}`); + expect(error.message).toEqual(`${NETWORK_ERROR_UNKNOWN_NETWORK_ID} ${id}`); } }); });