Skip to content
CCPEDIAby Unity Nodes
App Development

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

3 posts408 views4 likesLast activity Mar 2021

  1. gyorgybalazsiOP 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
    
  2. Stephen 2

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

  3. gyorgybalazsi 1

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