wallet_switchEthereumChain Errors and the Add-Then-Switch Flow
Handle wallet_switchEthereumChain error 4902 by checking both error code locations, adding full chain parameters, then retrying the switch in MetaMask Mobile.
Uruguay Can Editorial

wallet_switchEthereumChain resolves to null when the wallet switches chains, but error 4902 means the requested chain is not known to the wallet. Defined by EIP-3326, the method takes params: [{ chainId }], with the chain ID as a 0x-prefixed hexadecimal string such as 0x89 for Polygon (chain ID 137).
Switching only works for chains already stored in the wallet, so a dapp must offer the wallet the chain details before retrying. Chainlist is an EVM network directory with chain IDs and RPC endpoints; its entries are useful background when you assemble a chain configuration. Treat any configuration you use as data to validate before presenting it to the wallet.
Handle the result and errors
Catch the rejected request and inspect the error code before choosing a next step. MetaMask Mobile may put 4902 at error.data.originalError.code instead of error.code, so check both locations; otherwise you could miss the add-chain case.
- If the switch resolves, it succeeded with a
nullresult. - If either code location is 4902, call
wallet_addEthereumChainwith the complete chain parameters:chainId,chainName,nativeCurrency,rpcUrls, andblockExplorerUrls. EIP-3085 defines the add method; wallets can require fields beyond the chain ID. - After the add request succeeds, retry the switch. Adding a chain does not necessarily select it.
- For 4001, the user rejected the request, as specified by EIP-1193; stop and let them choose whether to try again. For -32002, a request is already pending, so wait for the wallet prompt instead of sending another.
- For other errors, handle them as failures rather than treating them as an unknown chain.
Polygon switch, add, retry
Say you request wallet_switchEthereumChain with chainId: "0x89" and MetaMask returns 4902. Build an add request for Polygon with that same chain ID, a chain name, its native currency details, RPC URL, and block explorer URL. Once the user approves adding it, send the switch request for 0x89 again. The second request should resolve to null if the switch succeeds.
Use this sequence whenever your dapp needs to move a connected user to a required EVM chain: request the switch, identify the error, add only when the wallet reports an unknown chain, then retry. Keep user rejection and a pending prompt on their own paths.