0

In the project I am working on (next.js + react) users will enter their own Infura endpoint to connect on-chain. Now is there a way to validate the endpoint they supplied (in this case their input is saved in their browser session storage). When I purposefully enter a bad Infura project id it doesn't get caught in the try/catchbut I get a unhandled runtime error later on. I would like to handle these error somehow, where can I catch them?

//web3.js file
const Web3 = require('web3')
const HDWalletProvider = require('@truffle/hdwallet-provider')

let web3

if (typeof window !== 'undefined') {
  try {
    const walletProvider = new HDWalletProvider(
      sessionStorage.getItem('mnemonic'),
      sessionStorage.getItem('http')
    )
    web3 = new Web3(walletProvider)
  } catch (error) {
    console.log(error)
    web3 = null
  }
  module.exports = {
    web3: web3,
  }
}

1st error

Error: PollingBlockTracker - encountered an error while attempting to update latest block:
Error: Unknown Error: invalid project id

2nd error

Error: Unknown Error: invalid project id

{
  "originalError": {}
}

3rd error

Error: PollingBlockTracker - encountered an error while attempting to update latest block:
Error: Unknown Error: invalid project id
4
  • Did you try making a call to web3 immediately? For example getBlock("latest"). – Ismael Sep 18 '20 at 21:26
  • @Ismael tried a web3.eth.getBlockNumber() right after web3 initialization but page still crashes, If i make it an async function and put await before the getBlockNumber it does enter the catch statment but page also crashes – Conviley Sep 19 '20 at 10:00
  • That looks like a bug with web3 or HDWalletProvider. The other options will be to make a JSON-RPC query to provided URL, see the format expected eth.wiki/json-rpc/API#eth_blocknumber. – Ismael Sep 19 '20 at 15:21
  • 1
    @Ismael Thanks for pointing me in this direction! I ended up using axios to make a post request to the supplied endpoint, sending a JSON-RPC request calling the eth_blockNumber and use that as validation that the supplied infura enpoint is working! Feel free to make an answer and I can accept it! :) – Conviley Sep 22 '20 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.