when user's transaction fails before submitting to the blockchain, I want to know the failure reason, so I can give a better user experience.
for example if user rejects a transaction in Metamask, or if the transaction fails due to insufficient funds for gas. how can I detect that?
my current implementation is not great, and will break easily. is there more elegant way to do it?
return sendTransaction(txData).catch(err => {
if (err.message.includes('User denied transaction') ||
err.message.includes('Request has been rejected.') ||
err.message.includes('transaction has been discarded') ||
err.message.includes('Transaction not confirmed'))
throw new Error('web3UserTxRejected');
if (err.message.includes('nonce too low'))
throw new Error('web3NonceTooLow');
if (err.message.includes('nonce may not be larger than'))
throw new Error('web3NonceTooHigh');
if (err.message.includes('insufficient funds for gas'))
throw new Error('web3InsufficientFundsForGas');
if (err.message.includes('intrinsic gas too low'))
throw new Error('web3GasTooLow');
throw new Error(err);
});