web3-react icon indicating copy to clipboard operation
web3-react copied to clipboard

How to catch the error from activate

Open cosullivan opened this issue 3 years ago • 3 comments

When calling the connector.activate(...) method, if the user cancels the network switch in MetaMask I can see the RPC error in the console, but the error isn't propogated to the caller because it is caught and supplied to reportError().

What is the best approach to determining whether the activate succeeds?

cosullivan avatar Apr 06 '22 14:04 cosullivan

Also looking at this. You can catch the error by using the Web3ReactProvider and the useWeb3React() hook. So it would look something like this:


  const { error } = useWeb3React();

  useEffect(() => {
    if (error) {
      handleError();
    }
  }, [error]);

It would be very nice to allow onConnected or onRejected callbacks to be passed to the activate method. That or just return the error in the promise of activate instead of returning a void

ghardin1314 avatar Apr 06 '22 18:04 ghardin1314

Yes, I think having the activate method return/throw the error would be highly benefical.

At the moment I have restored to this which seems to work but isn't ideal imo.

await connector.activate(chainId);

const { error } = store.getState();
if (error === undefined) {
    connect(type);
}

cosullivan avatar Apr 07 '22 01:04 cosullivan

@cosullivan where are you imprting the store?

wobsoriano avatar Jun 09 '22 22:06 wobsoriano