Is there a way to return `assertMsg` error messages defined in the Daml code to the UI?

In the Create Daml App example from the docs, the Daml code defines error messages in the assertMsg satements of the Follow choice:

-- FOLLOW_BEGIN
    nonconsuming choice Follow: ContractId User with
        userToFollow: Party
      controller username
      do
        assertMsg "You cannot follow yourself" (userToFollow /= username)
        assertMsg "You cannot follow the same user twice" (notElem userToFollow following)
        archive self
        create this with following = userToFollow :: following
    -- FOLLOW_END

Though when I try to follow myself I get this error message:

The message comes from this code in the MainView.tsx file:

// FOLLOW_BEGIN
  const ledger = useLedger();

  const follow = async (userToFollow: Party): Promise<boolean> => {
    try {
      await ledger.exerciseByKey(User.User.Follow, username, {userToFollow});
      return true;
    } catch (error) {
      alert(`Unknown error:\n${error}`);
      return false;
    }
  }
  // FOLLOW_END
1 Like

What do you get in the alert when you format error to JSON before string-interpolating it?

2 Likes

Thanks, that works, I can extract from this the message already:

1 Like