Handling enableweb3 error

Hi I am having problems to handle the Moralis.enableweb3 error. When the user presses on the button that triggers this function, if the user closes the Metamask notification, my Dapp will get collapsed with this error: “moralis.js:6081 Uncaught (in promise) Error: Cannot execute Moralis.enableWeb3(), as Moralis Moralis.enableWeb3() already has been called, but is not finished yet”.

I am trying to handle it with a try/catch, as I have seen in the forum but when I catch the error I do not know what to code in order finish the request, and enable the user to request the metamask notification once again.

This is what my code looks like:

isWeb3Enable = async () => {

            if(Moralis.isWeb3Enabled() == false) {

                try{

                    await Moralis.enableWeb3()

                } catch(err){

                    console.error("Error isWeb3Enable", err)

                    //here is what i dont know what to type to stop the Dapp from collapsing

                }

            }

        }

I have also tried the suggested option I have seen in the forum of reloading the page. When I do this, it keeps not working. I get then this error: “Uncaught (in promise) {code: -32002, message: ‘Already processing eth_requestAccounts. Please wait.’, stack: ‘{\n “code”: -32002,\n “message”: "Already processi…fbeogaeaoehlefnkodbefgpgknn/common-5.js:19:38864)’}”.

So i need antoher solution, that finishes the request and enables the user to throw another one.

Thank you in advanced,

Iñigo

Moralis.isWeb3Enabled() is expected to be called once best immediately when the page is rendered, but better considering user being authenticated along side.

Error: Cannot execute Moralis.enableWeb3(), as Moralis Moralis.enableWeb3() already has been called, but is not finished yet”.

This error shows up when the user doesn’t complete the enableWeb3 process and you tried calling it again and most times it breaks the app process.
Try catch is a good option to handle this but considering the solution mentioned above

could you please provide a template of how try catch could be used to prevent the dapp from crashing when the user does not complete the enableWeb3 process? As i have tried but my code does not solve the issue. Thank you I would really appreciate it!

You can have such. You can call await init() and this should handle that at least. If you try call init consecutively without the await keyword, you’ll get the error, but you can call the init without the await keyword once when the page loads and maybe await init() in your other functions

async function init(){
    if(!Moralis.isWeb3Enabled() && Moralis.User.current()){
        await Moralis.enableWeb3()
    }
}