Skip to content
CCPEDIAby Unity Nodes
Discussions/App Development/Provide better error message from ensure block?Forum ↗

Provide better error message from ensure block?

App Development2 posts165 views3 likesLast activity Apr 2023
WA
WallaceKellyOP
Apr 2023

In the following code…

import Daml.Script
import DA.Text

template Asset with
    party : Party
    description : Text
  where
    signatory party
    ensure
      not $ isEmpty description 

description_is_required = script do
  party <- allocateParty "party"
  submit party $ createCmd Asset with description = "", ..

…the error message is Template precondition violated: Asset {party = 'party', description = ""}, which may not be helpful, especially for really large templates. I’d like to provide a more helpful error message.

Is there a better way than the following ensure block?

    ensure
      let isNotEmpty val name = (not (isEmpty val) || error ("The field " <> name <> " cannot be empty.")) in
      isNotEmpty description "description"

By “better way” I mean, am I overlooking existing functionality that does this already? Is error the right function? Or should I try to use abort? etc.

CO
cocreature
Apr 2023

There isn’t anything builtin for that. I think your error solution is perfectly fine. You could also throw a custom exception that you define instead but that only really has advantages if you intend to catch it.

← Back to Discussions