DSL: execute step if previous step failed

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.

Hi,
use .doIf(“#{condition}”).then
So you need any information from previous .exec about failure and then you can:

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

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:

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)?

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

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

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

should just work

3 Likes