'Sending BNB with web3js won't log errors

I have this code that allows me to send bnb using web3.js using MetaMask I want to display a message when user rejects payment or there is any other error and if it is successful, display a success message. I can not find my way around it.

My Code is

$(".pay-button").click(async () => {
        await initWeb3('https://bsc-dataseed1.binance.org:443');
        // paymentAddress is where funds will be send to
        const web3 = new Web3('https://bsc-dataseed1.binance.org:443');
const reciever = '0x5c03906C1c6f737eF982BDdA481D2E04D0089155';

            ethereum.request({
            method: 'eth_sendTransaction',
            params: [
                {
                    from: ethereum.selectedAddress,
                    to: reciever,
                    value: web3.toHex(web3.toWei('0.001', 'ether')),
                     gas: '0x76c0', // 30400
    gasPrice: '', // 10000000000000
    
                },
            ],
            })
            .then((txHash) => console.log(txHash))
            .catch((error) => console.error);
   
      });

I am using this package https://unpkg.com/@metamask/legacy-web3@latest/dist/metamask.web3.min.js



Solution 1:[1]

You can try to check for the following error codes that are emitted by Metamask and tweak you UI accordingly.

Error codes given by metamask

https://docs.metamask.io/guide/ethereum-provider.html#events

If your transaction is success, i.e. when you receive the txHash, you can show success message in the UI https://github.com/skiran017/sol-tip-jar/blob/main/src/modules/TipWidget/TipWidget.tsx transaction status here is doing some similar job

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TylerH