# Do While

**Do While** allows the construction of blocks in loop inside a pipeline. It's part of a set of connectors that work with subflows to execute their functions.

## **Parameters**

Take a look at the configuration parameters for the connector. Parameters supported by [Double Braces expressions](https://docs.digibee.com/documentation/connectors-and-triggers/double-braces/overview) are marked with `(DB)`.

<table data-full-width="true"><thead><tr><th>Parameter</th><th>Description</th><th>Default value</th><th>Data type</th></tr></thead><tbody><tr><td><strong>Iteration</strong></td><td>Defines the maximum number of times a loop is executed (it's possible to stop the loop before this number is reached).</td><td>10</td><td>Integer</td></tr><tr><td><strong>Timeout</strong></td><td>Maximum time the loop must execute.</td><td>300000</td><td>Integer</td></tr><tr><td><strong>Loop Index</strong></td><td>If the option is activated, a “loopIndex” property will be inserted in the presented message at each loop iteration and will inform the current iteration index (1, 2, 3, …).</td><td>False</td><td>Boolean</td></tr><tr><td><strong>Interrupt Loop On Error</strong></td><td>If the option is activated, an error occurred inside an iteration will cause the loop interruption as a whole. Otherwise, the loop will continue from the next iteration.</td><td>True</td><td>Boolean</td></tr></tbody></table>

## **Defining the subflow to be deployed at each iteration**

To work with this type of connector, it's important to keep in mind:

* **Subflow defined in the onProcess block:** the pipeline defined in this block is deployed at each loop iteration. Therefore, if the loop has 10 iterations, the subflow is executed 10 times.
* **Subflow defined in the onException block:** the pipeline defined in this block is deployed whenever a loop iteration results in error.

To define the subflow to be deployed at each iteration, just click on the **onProcess** icon of the **Do While** connector:

<figure><img src="https://content.gitbook.com/content/EKM2LD3uNAckQgy1OUyZ/blobs/g1GrlrvurzUjVM2LnjmP/Do%20While%20onprocess%20nov%2023.png" alt=""><figcaption></figcaption></figure>

When clicking on this icon, a subflow will be created (or shown, if it already exists). Then all you have to do is build the desired flow according to the execution needs of each iteration.

{% hint style="warning" %}
The input of this pipeline will be fed with the message that comes before **Do While**. This will happen in the #1 loop execution. For the other iterations, the presented message will be the final message of the previous iteration and so on. If **Loop Index** is activated, a `loopIndex` property will be inserted in the message informing the current iteration.
{% endhint %}

## **Interrupting a loop**

To interrupt a loop before the maximum number of defined iterations, just inform a `"loopBreak: true"` property at the end of the iteration. If the property is found, the loop will be broken.

A way to inform this property is by using the [**JSON Generator**](https://docs.digibee.com/documentation/connectors-and-triggers/connectors/tools/json-generator) connector, which allows the definition of arbitrary JSONs in the pipeline flow.

{% hint style="info" %}
When the `loopBreak: true` property is used, the connector's output will be empty. To make it easier to locate this property in your flow, we recommend naming the connector **"loopBreak"**. This way, you can quickly identify where the loop was interrupted when analyzing the execution.
{% endhint %}

## **Handling errors in the loop**

The standard behavior of **Do While** is to interrupt the loop if an error is found. Errors are atypical situations in the deployment of a pipeline that end up generating a stop. For example, the use of an Assert connector causes an error in the pipeline when the assertion condition isn't met. Other error situations happen when connectors are used with the enabled **Fail On Error** configuration.

As previously explained, it's possible to define a subflow to handle errors. The definition of this subflow is made through the **onException** icon of the **Do While** connector:

<figure><img src="https://content.gitbook.com/content/EKM2LD3uNAckQgy1OUyZ/blobs/0HuksYEFodD07756Rymy/Do%20While%20onexception%20nov%2023.png" alt=""><figcaption></figcaption></figure>

The input of this subflow will be fed with an error message, which will inform what happened to the iteration and the index of the iteration (`loopIndex`) that caused the failure.

```json
{
    "timestamp": <error's timestamp>
    "error": <error message>
    "code": 500
    "loopIndex": <iteration index where the error occurred>
}
```

## **Usage scenarios of the Do While connector with errors handling**

### **"Interrupt loop on error" property enabled and no subflow defined in onException**

* The subflow will be immediately interrupted;
* An error will be thrown from the **Do While** connector;
* No other connector will be invoked after **Do While**.

### **"Interrupt loop on error" property enabled and a subflow defined in onException**

* The subflow will be immediately interrupted;
* The subflow defined in onException will be executed and its output will feed the connector that follows **Do While**;
* The connector that follows **Do While** will be invoked.

### **"Interrupt loop on error" property disabled and no subflow defined in onException**

* The iteration where the error occurred will be interrupted;
* A standard error message will be informed in the input of the following iteration and the loop will keep being normally executed.

### **"Interrupt loop on error" property disabled and a subflow defined in onException**

* The iteration where the error occurred will be interrupted;
* The subflow defined in onException will be executed and its output will feed the following iteration;
* The loop will keep being normally executed.

## **Error during the onException subflow**

* The loop will be interrupted;
* The error will be thrown for the following connector which the **Do While** connector is associated to;
* If **Do While** is in the pipeline main flow, then the pipeline will be interrupted;
* If **Do While** is inside a connector that has a subflow, then the onException subflow will be invoked and the error will be informed in this pipeline input.
