# DSL: execute step if previous step failed

**URL:** <https://community.gatling.io/t/dsl-execute-step-if-previous-step-failed/7010>\
**Category:** Gatling (Open-Source)\
**Created:** [August 2, 2022, 10:41am UTC](https://community.gatling.io/t/dsl-execute-step-if-previous-step-failed/7010 "2022-08-02T10:41:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aerondigth](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@aerondigth](https://community.gatling.io/u/aerondigth)\
**Post date:** [August 2, 2022, 10:41am UTC](https://community.gatling.io/t/dsl-execute-step-if-previous-step-failed/7010/1 "2022-08-02T10:41:51Z")

</div>

Hello!

Is there a mechanism to execute a step if previous step failed? For example:

exec(chainMayFail)  
.exec(customLogicToRecover) // Needs to be executed only if previous failed

Found only exitHereIfFailed, but impossible to inject a chain there

Would be happy for any ideas for this.

---

<div class="post-metadata">

**Author:** ![GeMi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/gemi/32/939_2.png) [@GeMi](https://community.gatling.io/u/GeMi)\
**Post date:** [August 2, 2022, 11:06am UTC](https://community.gatling.io/t/dsl-execute-step-if-previous-step-failed/7010/2 "2022-08-02T11:06:55Z")

</div>

Hi,  
use [.doIf(“#{condition}”).then](https://gatling.io/docs/gatling/reference/current/core/scenario/#doif)  
So you need any information from previous `.exec` about failure and then you can:

```auto
// with a function
doIf(session -> session.getBoolean("condition")).then(
  exec(http("name").get("/"))
);

```

---

<div class="post-metadata">

**Author:** ![aerondigth](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@aerondigth](https://community.gatling.io/u/aerondigth)\
**Post date:** [August 2, 2022, 12:18pm UTC](https://community.gatling.io/t/dsl-execute-step-if-previous-step-failed/7010/3 "2022-08-02T12:18:20Z")

</div>

Thanks for reply!

I was looking for ability to handle exception from previous block and execute recovery routine before exiting, so there is no way to save info to session, afaik

Guess I need smth like this:

```auto
exec(s => {
  Try {
    exec(mayThrowExceptionAndFail)
  } recoverWith {
    case _: SomeException => Success(exec(recoverBlock))
  }
  s
}) 

```

But these exec-s will not be added to the chain, so this does not work.

Btw, does session hold status of previous block execution in internals (is it the way exitHereIfFailed implemented)?

---

<div class="post-metadata">

**Author:** ![aerondigth](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@aerondigth](https://community.gatling.io/u/aerondigth)\
**Post date:** [August 2, 2022, 12:28pm UTC](https://community.gatling.io/t/dsl-execute-step-if-previous-step-failed/7010/4 "2022-08-02T12:28:53Z")

</div>

Found a solution in exitHereIfFailed implementation, seems to work fine:

```auto
import io.gatling.commons.stats.KO
import io.gatling.commons.validation.SuccessWrapper

.doIf(s => { (s.status == KO).success }) {
  exec(recoverBlock)
}
.exitHereIfFailed

```

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [August 3, 2022, 11:55am UTC](https://community.gatling.io/t/dsl-execute-step-if-previous-step-failed/7010/5 "2022-08-03T11:55:43Z")

</div>

```scala
.doIf(s => s.status == KO)

```

should just work
