taquito: Document how to capture failwith errors

Doc how Taquito users can smart contract errors surfaced by the FAILWITH michelson instruction.

catch (ex) {
   if (ex instanceof TezosOperationError) {
       if(ex.message === "E_TOMANYBLURBS") {
          //handle error
        }
    }
}

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 17 (6 by maintainers)

Most upvoted comments

Ha, yes, I forgot this issue is for me and by me to document this feature 😆 Thank you @DalyIng

Hi @DalyIng

Yes, Taquito supports this in the latest version. This relies on the Michelson FAILWITH instruction.

If you are using Ligo for example, a contract that has a function such as the following;

function fail_if_not_admin(const admins: set(address)): unit is
  block {
    if (not(set_mem(sender, admins))) then
      failwith("You don't have this privilege.")
    else skip
  } with unit

And the failwith gets executed during an operation, you can catch that error in Taquito as follows;

catch (ex) {
   if (ex instanceof TezosOperationError) {
       if(ex.message === "You don't have this privilege.") {
          //handle error
        }
    }
}

I recommend you define error codes when using failwith, instead of human-readable strings.

We have a demo app that implements this here: https://github.com/ecadlabs/proposal-vote/blob/bf738d68361711a081f399928cbec7edb250e17f/src/components/voter-panel.tsx#L99 for your reference.

If this covers your question, please feel free to close this issue. 😃