OpenZeppelin Networkjs throws error with no provider

Using the latest OpenZeppelin NetworkJS (https://github.com/OpenZeppelin/openzeppelin-network.js/tree/master)

useWeb3Injected() doesn’t catch the situation when no web3 provider is available and instead throws an error if MetaMask (or other injected provider) is not found. This error crashes the users’ website completely. There seems to be no way to catch this error and error suitable UI to inform users they are missing Metamask. See the relevant line in the code.

Any ideas? It would be nice to catch the error and do something with it, like inform the user.

2 Likes

Hey @Dennison. It always pleasing to get some feedback from you!
You are right that network.js doesn’t catch A web3 provider is not attached to a window. error. This is by design because it needs web3 provider to operate. This can be easily prevented by checking windows.ethereum field before creating any Web3Context instances.
In case if the error happens somehow anyway, it can be caught by try/catch in vanilla JS or Error Boundaries in React.
In case if you use React Hooks, they unfortunately doesn’t support handling errors yet but they promise to support it soon.
Anyway we would love to see some exact code with which you are having issues.

2 Likes

A post was split to a new topic: Can you use OpenZeppelin Network.js with Ethers?

Hi @Dennison,

There is now a Pull Request to change this behavior: https://github.com/OpenZeppelin/openzeppelin-network.js/pull/24

1 Like

Hey @Dennison. After some considerations we decided to support not throwing errors. Enjoy!

1 Like