Skip to content
CCPEDIAby Unity Nodes
Discussions/App Development/Is there a way to return `assertMsg` error messages defined in the Daml code to the UI?Forum ↗

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

App Development3 posts392 views4 likesLast activity Mar 2021
GY
gyorgybalazsiOP
Mar 2021 1

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
ST
Stephen
Mar 2021 2

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

GY
gyorgybalazsi
Mar 2021 1

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

← Back to Discussions