# What's the recommended way to terminate early in an action, without failing?

**URL:** <https://forum.canton.network/t/whats-the-recommended-way-to-terminate-early-in-an-action-without-failing/1556>\
**Category:** App Development\
**Created:** [October 29, 2020, 6:24pm UTC](https://forum.canton.network/t/whats-the-recommended-way-to-terminate-early-in-an-action-without-failing/1556 "2020-10-29T18:24:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leonid\_Rozenberg](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/leonid_rozenberg/32/48_2.png) [@Leonid\_Rozenberg](https://forum.canton.network/u/Leonid_Rozenberg)\
**Post date:** [October 29, 2020, 6:24pm UTC](https://forum.canton.network/t/whats-the-recommended-way-to-terminate-early-in-an-action-without-failing/1556/1 "2020-10-29T18:24:18Z")

</div>

For example, if I have a script and I want to run it to a given step (`Int`)?

I could have the full return type be an `Either () ()` and include

```haskell
if stopStep == 1
then pure $ Left ()
else do ..

```

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [October 29, 2020, 6:40pm UTC](https://forum.canton.network/t/whats-the-recommended-way-to-terminate-early-in-an-action-without-failing/1556/2 "2020-10-29T18:40:16Z")

</div>

`Either` or the transformer version `EitherT` (not in the standard library) are one of the better solutions for that atm. I wrote up an example of this a while back at [ex-daml-error-handling/M2\_UseAssert.daml at master · digital-asset/ex-daml-error-handling · GitHub](https://github.com/digital-asset/ex-daml-error-handling/blob/master/src/M2_UseAssert.daml). The biggest caveat if you are working in `Update` is that the actions you’ve taken are not rolled back which is often not what’s intended.

We are currently working on adding exception handling to DAML to make this a bit easier and allow for rollback. However if you are not in `Update`, then `EitherT` is probably still your best option.

---

<div class="post-metadata">

**Author:** ![bernhard](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/bernhard/32/356_2.png) [@bernhard](https://forum.canton.network/u/bernhard)\
**Post date:** [October 30, 2020, 8:03am UTC](https://forum.canton.network/t/whats-the-recommended-way-to-terminate-early-in-an-action-without-failing/1556/3 "2020-10-30T08:03:40Z")

</div>

You don’t have to indent `do` blocks to nest them so if you don’t need to return anything in the case where you don’t want continue, you can use the below pattern.

```haskell
module Main where

import DA.Action

import Daml.Script

stepLimit = 3

stepwise : Script ()
stepwise = script do

  when (stepLimit >= 1) do
  debug "Step 1"

  when (stepLimit >= 2) do
  debug "Step 2"

  when (stepLimit >= 3) do
  debug "Step 3"

  when (stepLimit >= 4) do
  debug "Step 4"

  when (stepLimit >= 5) do
  debug "Step 5"

```

Or to make it prettyer:

```haskell
module Main where

import DA.Action

import Daml.Script

stepWithLimit : (Action m) => Int -> Int -> m () -> m ()
stepWithLimit l n = when (l >= n)

stepwise : Script ()
stepwise = script do
  let step = stepWithLimit 3

  step 1 do
  debug "Step 1"

  step 2 do
  debug "Step 2"

  step 3 do
  debug "Step 3"

  step 4 do
  debug "Step 4"

  step 5 do
  debug "Step 5"

```
