2

I want to know if it is possible to know from "error" object returned by geth that the error it was due to insufficient balance (something to compare with)

I have a js code like that:

mycontract.storeData("some-data")
     .then(res => {
          // transaction confirmed 
            })
     .catch(error => {
     // I want to know the cause if the error (programmatically) like error.statusCode ... ?
  })
10
  • When you investigate things, while trying to gather some info that could help you with your question, you could by the least console.log(error) and share it with us! BTW, there's no way to tell if the error was due to insufficient balance, but there IS a way to deduce the answer to that questions from the error object along with the current balance of your account. Commented Nov 4, 2019 at 16:07
  • error message is widely known ("insufficient funds for gas * price + value”), the question is about how to identify error programmatically
    – maroodb
    Commented Nov 4, 2019 at 16:22
  • Sounds like you already know the answer to your question then. Commented Nov 4, 2019 at 16:24
  • How ? I am asking if there a way to identify the error programmatically
    – maroodb
    Commented Nov 4, 2019 at 16:27
  • I dont want to compare error message with this string "insufficient funds for gas * price + value”, i think isn't reliable
    – maroodb
    Commented Nov 4, 2019 at 16:28

2 Answers 2

2

Switch from web3 v0.x (which you're apparently on) to web3 v1.2.x, and then follow this example.

In particularly, read about the error-handler at the bottom of this example, which appears to be designated to catch exactly what you're after:

.on('error', console.error);

If there's an out-of-gas error, then the second parameter is the receipt.

EDIT - the error-handler example above is rather poor; let me rewrite it:

.on('error', function(error, receipt) {
    if (receipt !== undefined) {
        // handle out-of-gas errors
    }
    else {
        // handle all other errors
    }
});
7
  • but this catch any type of errors, yes ?
    – maroodb
    Commented Nov 5, 2019 at 8:00
  • @maroodb: I suggest that you try it on first, see what you get inside the function and all. From my understanding of the comment If there's an out-of-gas error, then the second parameter is the receipt, the answer to your question is Yes (because it implies that there are other types of errors). Commented Nov 5, 2019 at 8:02
  • I tried it ofcourse, I have an example of smart contract methods that it run 'revert()' if some condition, and here the code above log the revert error
    – maroodb
    Commented Nov 5, 2019 at 8:07
  • @maroodb: Is that a question? Commented Nov 5, 2019 at 8:09
  • I meant when I tried this code even errors related to smart contract (revert, require..) they were catched, so it does the same thing as using promise (catch(error)..)
    – maroodb
    Commented Nov 5, 2019 at 8:11
0

Ultimate way to know why mined transaction failed is to trace it. In Geth you may do this using debug.traceTransaction().

6
  • But in the case of "insufficient funds for gas * price + value”, there is no transaction yet to debug
    – maroodb
    Commented Nov 4, 2019 at 16:26
  • Yes, but this situation is easy to recognize, because sender's balance, transaction value, gas limit, and gas price are known. Commented Nov 4, 2019 at 16:35
  • but I am asking about "insufficient funds for gas * price + value” error, and here it doesn't help
    – maroodb
    Commented Nov 4, 2019 at 16:57
  • Original question was about transaction that failed because of insufficient balance. The fact that transaction failed implies that it was mined. Commented Nov 4, 2019 at 17:29
  • It cant be mined while wallet hasn't fund, anyway thank you
    – maroodb
    Commented Nov 5, 2019 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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