# ExitBlockOnFail executes even when no errors are present

**URL:** <https://community.gatling.io/t/exitblockonfail-executes-even-when-no-errors-are-present/6948>\
**Category:** Gatling (Open-Source)\
**Created:** [July 19, 2022, 10:22pm UTC](https://community.gatling.io/t/exitblockonfail-executes-even-when-no-errors-are-present/6948 "2022-07-19T22:22:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Drew](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@Drew](https://community.gatling.io/u/Drew)\
**Post date:** [July 19, 2022, 10:22pm UTC](https://community.gatling.io/t/exitblockonfail-executes-even-when-no-errors-are-present/6948/1 "2022-07-19T22:22:31Z")

</div>

Heres a code snippet to show the layout of a ChainBuilder I created

```auto
.exec(
		http(Name)
				.post(url)
				.body(PebbleFileBody("pebbleTemplate.peb")).asJson() 
				.basicAuth(username, password)
				.check(status().is(200).saveAs("responseStatus"))
				.check(
						jmesPath("path")
								.ofMap()
								.find()
								.saveAs("appointment")
				)
				.check(bodyString().saveAs("responseBody"))
)
.exitBlockOnFail(
		exec(session -> {
			logger.error("Search Session failed for reason: {}", session.getString("responseStatus"));
			logger.error("Search Sessions response: \n {}", session.getString("responseBody"));
			return session;
		})
)

```

When something goes wrong I want to capture some additional data in a log file using log4j2.  
I put this additional logic in an `.exitBlockOnFail`.

For some reason, the code in `.exitBlockOnFail` is executing even when my checks are passing; I can find the data I need and the request response is 200.  
All other requests after this one pass with an OK status. Their `exitBlockOnFail` conditionals trigger as well.

The default logging that Gatling provides has been set to log if an error occurs in the requests. Its empty.

Why is the code inside `.exitBlockOnFail` executing? Does Gatling provide a way that I can get additional details on what caused a failure?

---

<div class="post-metadata">

**Author:** ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)\
**Post date:** [July 20, 2022, 11:55am UTC](https://community.gatling.io/t/exitblockonfail-executes-even-when-no-errors-are-present/6948/2 "2022-07-20T11:55:32Z")

</div>

Hi @Drew!

I think you misunderstood the meaning of [`exitBlockOnFail`](https://gatling.io/docs/gatling/reference/current/core/scenario/#exitblockonfail).

Consider this `ChainBuilder`:

```java
  exitBlockOnFail(
    exec(http("My Request that may fail").post(url)) // <1>
    .exec(http("Only if first request succeed").get(otherUrl)) // <2>
    .exec(http("Only if all previous succeed and so on").get(otherUrl)) // <3>
  )
  .exec(http("Always").get(otherUrl)) // <4>
  .doIf(Session::isFailed) // <5>
    .then(exec(session -> {
      logger.error("Search Session failed for reason: {}", session.getString("responseStatus"));
      logger.error("Search Sessions response: \n {}", session.getString("responseBody"));
      return session;
    }));

```

**1** , **2** and **3** are part of the `exitBlockOnFail`.  
**2** should be run only if **1** succeed  
**3** only if both previous succeed, etc.

Even in the case of one step failed in the `exitBlockOnFail` block. The steps outside ( **4** ) will run.

I think you want to log if the session is in failure state, as written in **5**

Does that help?

Cheers!

---

<div class="post-metadata">

**Author:** ![Drew](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@Drew](https://community.gatling.io/u/Drew)\
**Post date:** [July 20, 2022, 2:04pm UTC](https://community.gatling.io/t/exitblockonfail-executes-even-when-no-errors-are-present/6948/3 "2022-07-20T14:04:08Z")

</div>

You’re correct that I misunderstood. I thought it was treated as a sort of catch block; If the above block fails, execute this code. Instead `exitBlockOnFail` is quite literally saying exit this block immediately if any exec’s fail inside.
