This is really a two part question.

First, the only way I can get the error message thrown by a Smart Contract require statement is to call the function first with a call rather than a send. send does not return the message thrown by a failed require statement whereas call does.

Example. In Solidity I might have the following:

require (org._isUser(_user),'Error: User not registered');

In my React frontend I want to be able to display 'Error: User not registered'.

My function call in React is as follows:

async function createPO(e) {
    const poeacct = e.target.elements[0].value
    const ponumber = e.target.elements[1].value
    const podate = e.target.elements[2].value
    let callOK = true
    try {
      await contract2.methods
        .createOrder('0xd2c4895e24095f23277df168f94a6c035813fe5d', poeacct, ponumber, podate)
        .call({ from: accounts[0] })
    } catch (err) {
      callOK = false
    if (callOK) {
      await contract2.methods
        .createOrder('0xd2c4895e24095f23277df168f94a6c035813fe5d', poeacct, ponumber, podate)
        .send({ from: accounts[0] })

      setMess('Transaction successful')

This works but it's kind of klugey so I'm wondering if there's a better way.

Second. The error returned by the 'catch' in the Web3 call is JSON but there's a prefix:

Error: [object Object]
  "message": "VM Exception while processing transaction: revert Error: Order already exists",
  "code": -32000,
  "data": {
    "0x270bb8ddea630f3fc42a4f6196d7710cc50779a34994fd534a8ce139976b0ef5": {
      "error": "revert",
      "program_counter": 21219,
      "return": "0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001b4572726f723a204f7264657220616c7265616479206578697374730000000000",
      "reason": "Error: Order already exists"

The Error: [object Object] prefix prevents me from parsing out the 'reason:' node. If I remove the prefix manually (i.e. hard code it) it works.

I've tried substring,slice and JSON.parse etc. but I can't find a solution.


I got the parse part to work like this:


Still thinking there must be a cleaner way of handling errors passed from Web3.

  • 1
    Did you find any better way to parse the error?
    – Amin
    Jan 16 '21 at 6:41

In my case, using truffle, I've solved this way:

...catch((err) => {
    let message = JSON.parse(err.message.substring(56).trim().replace("'", "")).value.data.data;

On this way I can show the message I set on my solidity contract.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.