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/catch
but 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
getBlock("latest")
.web3.eth.getBlockNumber()
right after web3 initialization but page still crashes, If i make it an async function and putawait
before thegetBlockNumber
it does enter the catch statment but page also crasheseth_blockNumber
and use that as validation that the supplied infura enpoint is working! Feel free to make an answer and I can accept it! :)