2

What is the difference between error, fail, abort and assert in DAML?

Meetanshru
  • 35
  • 2

1 Answers1

1

fail and abort are aliases for the same function, with abort being the more common use. They are used to fail an Action like Update and Scenario, but still return a value of the appropriate type. The following scenario is perfectly fine, since s is never actually performed:

t = scenario do
  let
    s : Scenario () = abort "Foo"
  return ()

When you want a branch of a Scenario or Update to cause failure, use abort. For example, the following Scenario will succeed or fail depending on the value of abortScenario:

t2 = scenario do
  let abortScenario = True
  if abortScenario
    then abort "Scenario was aborted"
    else return ()

assert is just a wrapper for abort:

-- | Check whether a condition is true. If it's not, abort the transaction.
assert : CanAbort m => Bool -> m ()
assert = assertMsg "Assertion failed"

-- | Check whether a condition is true. If it's not, abort the transaction
-- with a message.
assertMsg : CanAbort m => Text -> Bool -> m ()
assertMsg msg b = if b then return () else abort msg

It's almost always better to use abortMsg, as you can supply an informative error message.

error is used to define partial functions. It does not return a value, but causes the interpreter to exit immediately with the given error message. E.g.

divide : Int -> Int -> Int
divide x y
  | y == 0 = error "Division by Zero!"
  | otherwise = x / y

DAML is eagerly executed so you have to be very careful with error. The below scenario will fail, even though e is not used.

s = scenario do
  let
    e = divide 1 0
  return ()
bame
  • 960
  • 5
  • 7