# Restart loop on fail

**URL:** <https://community.gatling.io/t/restart-loop-on-fail/1623>\
**Category:** Gatling (Open-Source)\
**Created:** [October 31, 2014, 4:51pm UTC](https://community.gatling.io/t/restart-loop-on-fail/1623 "2014-10-31T16:51:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jarrowwx](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@jarrowwx](https://community.gatling.io/u/jarrowwx)\
**Post date:** [October 31, 2014, 4:51pm UTC](https://community.gatling.io/t/restart-loop-on-fail/1623/1 "2014-10-31T16:51:31Z")

</div>

I have code that looks like this:

`scenario("Test Service") .exec( SessionVariables.initialize ) .during( Test.duration ) { feed( User.feed.random ) .exec( RTDE.Login.sequence ) **.exitHereIfFailed**.exec( RTDE.SomeService.GET .request .check( jsonPath( "$" ).saveAs( RESPONSE_BODY ) ) ) }`

I did that (.exitHereIfFailed) because if the login fails, there is no point in going on. But I don’t want the whole scenario to end, I just want to skip the rest of the loop body. Like this:

`  
during( time ) {  
login  
if ( ! failed ) {  
continue the test  
} else {  
log the user information that failed, for investigation  
reset the fail flag so we can continue the loop  
}  
}

`

How to do that does not appear to be documented (that I can find). So, digging in the code, it looks like I would do it like this:

`  
.during( Test.duration ) {  
feed( User.feed.random )  
.exec( RTDE.Login.sequence )  
.doIfOrElse( session =\> session.status == OK ) {  
// …  
} {  
// log failure  
.exec( session =\> session.markAsSucceeded )  
}  
}

`

Seems to work. But is that the way it is meant to be done, or is there an easier way that I did not come across yet?

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [October 31, 2014, 4:58pm UTC](https://community.gatling.io/t/restart-loop-on-fail/1623/2 "2014-10-31T16:58:21Z")

</div>

Why don’t you use trymax?

---

<div class="post-metadata">

**Author:** ![jarrowwx](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@jarrowwx](https://community.gatling.io/u/jarrowwx)\
**Post date:** [October 31, 2014, 5:10pm UTC](https://community.gatling.io/t/restart-loop-on-fail/1623/3 "2014-10-31T17:10:57Z")

</div>

That doesn’t quite do what I need.

If I use tryMax, then if ANYTHING fails, it bails.

I only want to bypass the rest of the loop body if the login fails. Once past the login, failures are real failures, and I want to know about them. And the contents of that body may or may not be a linear flow where each step depends on the one before it. I may have 5 things I want to do in the body of that loop, and if they are independent, then I don’t want an error in one to stop the rest from executing.

I admit, I’m thinking like a functional tester, not a load tester. But that’s because I’m functionally testing my Gatling code. But the point is just to understand which DSL best applies to my intended scenario behavior.

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [November 3, 2014, 9:31pm UTC](https://community.gatling.io/t/restart-loop-on-fail/1623/4 "2014-11-03T21:31:02Z")

</div>

You’re right?  
So yes, there’s no other solution than what you’re doing ATM.

---

<div class="post-metadata">

**Author:** ![jarrowwx](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@jarrowwx](https://community.gatling.io/u/jarrowwx)\
**Post date:** [November 3, 2014, 9:46pm UTC](https://community.gatling.io/t/restart-loop-on-fail/1623/5 "2014-11-03T21:46:51Z")

</div>

Good. Means I understand Gatling better now than I did. 🙂
