Web3js – How to Catch JSON RPC Exceptions in a Front End Application

solidityweb3js

I have a transaction in contract which "requires" only owner can call. In front end when I call this with accounts other than owner of the contract I want to display an error. Code below doesn't work. It says

:Uncaught (in promise) Error: Invalid JSON RPC response: {"id":6,"jsonrpc":"2.0"}

      this.state.web3.eth.getAccounts((error, accounts) => {
        TalioInterview.deployed().then((instance) => {
          try {
            return instance.isTalioOwner.call({from:accounts[1]})

          }catch(err){
            alert("You are not authorized to run this.");
          }

My contract looks as below

 function isTalioOwner()
    public onlyIfTalioOwner()
    constant
    returns(bool)
    {
        return true;
    }

Found a similar question here, but without any answer. What is the best solution here other than changing contract to return false for non-owner accounts ?

Best Answer

I'm not too familiar with Truffle. (I assume this is Truffle, right?) But just based on how Promises usually work, I think you want something like this:

instance.isTalioOwner.call({from:accounts[1]}).then((result) => {
  console.log("Success! Got result: " + result);
}).catch((err) => {
  console.log("Failed with error: " + err);
});
Related Topic