Skip to content

TRY Step

The try step provides robust error handling and recovery within your Flow Services. It allows you to execute a block of steps and catch any errors that occur.

Properties

PropertyTypeDescription
typestringMust be "try"
stepsarrayNested steps to attempt
catcharray(Optional) Steps to execute if steps fail. If omitted, errors are still absorbed.

Overview

Joynare Nexus implements TRY/CATCH semantics where the try block executes first. If it fails, the catch block is executed, and the original error is absorbed if the catch succeeds.

Schema

yaml
- type: "try"
  steps:
    - type: "invoke"
      service: "system.http:Request"
      input:
        url: "https://api.example.com/data"
  catch:
    - type: "map"
      set:
        status: "fallback"
        data: {}

How It Works

  1. Execute TRY Block: The steps in steps are executed first.
  2. Handle Error: If any step fails, the error is injected into the pipeline as _error.
  3. Execute CATCH Block: The steps in catch are executed with access to _error.
  4. Absorb or Propagate: If catch succeeds, execution continues. If catch fails, the error propagates.

Error Injection

When an error occurs in the try block, the error message is automatically injected into the pipeline:

yaml
pipeline:
  output:
    errorMessage: string

steps:
  - type: "try"
    steps:
      - type: "invoke"
        service: "external:UnreliableService"
    catch:
      - type: "map"
        set:
          errorMessage: "${_error}"

Common Use Cases

API Error Handling

yaml
steps:
  - type: "try"
    steps:
      - type: "invoke"
        service: "system.http:Request"
        input:
          url: "${apiUrl}"
          method: "GET"
    catch:
      - type: "map"
        set:
          responseData: null
          status: "unavailable"

Graceful Degradation

yaml
steps:
  - type: "try"
    steps:
      - type: "invoke"
        service: "premium:CalculateDiscount"
        input:
          customerId: "${customerId}"
    catch:
      - type: "invoke"
        service: "system.math:Multiply"
        input:
          value1: "${orderTotal}"
          value2: 1.0

Try Without Catch (Silent Failure)

yaml
steps:
  - type: "try"
    steps:
      - type: "invoke"
        service: "analytics:TrackEvent"
        input:
          event: "user_action"
  # If analytics fails, we don't care - continue anyway

Catch Block Behavior

With Catch Block

If a catch block is defined and the try block fails:

  • The error message is injected as _error into the pipeline
  • The catch block steps are executed
  • If catch succeeds, the error is absorbed and flow continues
  • If catch fails, a new error is returned: "catch block failed: [original error]"

Without Catch Block

If no catch block is defined and the try block fails:

  • The error is still absorbed (not propagated)
  • Execution continues to the next step
  • This is useful for "fire and forget" operations where failures are acceptable

Key Points

  • The _error variable contains the error message from the failed step.
  • Exit signals (from exit step with ExitError type) are not caught and will propagate correctly.
  • Catch block is optional. Without it, errors are silently absorbed.

Real-World Example

See flows/showcase/07-error-handling/Reliability/flow.yaml for a complete example demonstrating:

  • Try-catch with timeout handling
  • Error recovery with _error variable
  • Integration with retry policies

See Also

  • SEQUENCE - For grouped steps with error handling
  • REPEAT - For retry logic
  • EXIT - For controlled flow termination

Released under the ISC License.