Provide better error message from ensure block?

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.

1 Like

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.

2 Likes