Skip to content

feat: Add then directive in catch clause for try tasks. #1148

@ricardozanini

Description

@ricardozanini

What would you like to be added?

Support for the then directive (or a similar control-flow transition) directly within the catch clause of a try task in the Serverless Workflow DSL.

Proposal(s):

I propose adding a then property to the catch object definition. When an execution error is caught by matching the errors criteria, the workflow should immediately transition to the task specified by the then directive, bypassing the default sequential flow.

Here is an example of how this would look in the DSL:

document:
  dsl: '1.0.3'
  namespace: test
  name: try-example
  version: '0.1.0'
do:
  - trySomething:
      try:
        - invalidHttpCall:
            call: http
            with:
              method: get
              endpoint: https://
      catch:
        errors:
          with:
            type: https://serverlessworkflow.io/dsl/errors/types/communication
            status: 503
        then: sharedErrorHandlerTask #  <-- Transitions to a reusable task defined elsewhere

Alternative(s):

  1. Using a subsequent switch task: Catching the error, mapping it to a state variable, and following the try task with a switch statement to evaluate the error variable and branch accordingly. However, this is much more verbose, spreads the error-handling logic across multiple tasks, and hurts readability.

  2. Using the do directive within catch: We can currently define tasks inline using do. However, if multiple tasks across the workflow need to trigger the same compensation or error-handling sequence, using do forces us to duplicate that logic inline every single time, violating DRY principles.

Additional info:

The primary motivation for this addition is task reusability and control flow flexibility.

Currently, if we want to handle an error, we are forced to define the reaction inline (via do). However, in complex workflows or Saga patterns, it is common for multiple different tasks to fail in ways that require the exact same compensation sequence (e.g., a shared rollbackTransaction or notifyAdmin task).

By introducing the then directive into the catch clause, we can transition the control flow to a task that is called by another control flow or sits in the default control flow of the same workflow. This allows multiple try tasks to cleanly route to a single, shared error-handling task, significantly reducing duplication and making the workflow much easier to maintain.

Community Notes

  • Please vote by adding a 👍 reaction to the feature to help us prioritize.
  • If you are interested to work on this feature, please leave a comment.

Metadata

Metadata

Assignees

Projects

Status

Backlog

Relationships

None yet

Development

No branches or pull requests

Issue actions