github.com/Jeffail/benthos/v3@v3.65.0/website/docs/components/processors/try.md (about)

     1  ---
     2  title: try
     3  type: processor
     4  status: stable
     5  categories: ["Composition"]
     6  ---
     7  
     8  <!--
     9       THIS FILE IS AUTOGENERATED!
    10  
    11       To make changes please edit the contents of:
    12       lib/processor/try.go
    13  -->
    14  
    15  import Tabs from '@theme/Tabs';
    16  import TabItem from '@theme/TabItem';
    17  
    18  Executes a list of child processors on messages only if no prior processors have failed (or the errors have been cleared).
    19  
    20  ```yaml
    21  # Config fields, showing default values
    22  label: ""
    23  try: []
    24  ```
    25  
    26  This processor behaves similarly to the [`for_each`](/docs/components/processors/for_each) processor, where a list of child processors are applied to individual messages of a batch. However, if a message has failed any prior processor (before or during the try block) then that message will skip all following processors.
    27  
    28  For example, with the following config:
    29  
    30  ```yaml
    31  pipeline:
    32    processors:
    33      - resource: foo
    34      - try:
    35        - resource: bar
    36        - resource: baz
    37        - resource: buz
    38  ```
    39  
    40  If the processor `bar` fails for a particular message, that message will skip the processors `baz` and `buz`. Similarly, if `bar` succeeds but `baz` does not then `buz` will be skipped. If the processor `foo` fails for a message then none of `bar`, `baz` or `buz` are executed on that message.
    41  
    42  This processor is useful for when child processors depend on the successful output of previous processors. This processor can be followed with a [catch](/docs/components/processors/catch) processor for defining child processors to be applied only to failed messages.
    43  
    44  More information about error handing can be found [here](/docs/configuration/error_handling).
    45  
    46  ### Nesting within a catch block
    47  
    48  In some cases it might be useful to nest a try block within a catch block, since the [`catch` processor](/docs/components/processors/catch) only clears errors _after_ executing its child processors this means a nested try processor will not execute unless the errors are explicitly cleared beforehand.
    49  
    50  This can be done by inserting an empty catch block before the try block like as follows:
    51  
    52  ```yaml
    53  pipeline:
    54    processors:
    55      - resource: foo
    56      - catch:
    57        - log:
    58            level: ERROR
    59            message: "Foo failed due to: ${! error() }"
    60        - catch: [] # Clear prior error
    61        - try:
    62          - resource: bar
    63          - resource: baz
    64  ```
    65  
    66  
    67  
    68  
    69