Lemline Error Handling Examples
This document provides examples of error handling and resilience patterns in Lemline workflows. Robust error handling is essential for building reliable workflows that can gracefully recover from failures when interacting with external systems, processing data, or executing business logic.
Run the Examples
Each example below is a complete workflow. Complete the SQLite setup and keep its worker running. Run the commands from a second terminal in the setup directory. With the JAR, substitute java -jar lemline.jar for bin/lemline. The examples call the tutorial shop, whose fail, failTimes and delayMs query parameters produce the failures and the slow answer. Start it from the root of the Lemline source checkout:
The worker picks up delayed retries every 10 seconds, so allow a minute or two for the retry examples.
Basic Try-Catch Pattern
The fundamental error handling mechanism in Open Workflow DSL is the try-catch block, which allows workflows to catch and handle exceptions.
Simple Try-Catch Example
Save it as simple-try-catch-example.yaml:
The first instance returns the garden hose. For SKU-404 the shop answers HTTP 404: the error is caught and the workflow completes with the task input, {"productId":"SKU-404"}. The HTTP 500 does not match the condition: the instance faults with a communication error, status 500.
This example demonstrates:
Wrapping a potentially failing HTTP call in a
tryblockCatching only the HTTP 404 response, with
catch.errors.withon thecommunicationtype and status 404Building the request URI from the
productIdworkflow inputThe workflow continues after the error is caught
Key aspects of basic try-catch:
Execution continues after the catch block
If an error occurs that doesn't match the
errorscriteria, it will propagate upIf no error occurs, the
catchblock is skipped
Error Handling with Custom Actions
When an error occurs, you often need to perform specific actions in response, such as logging, notification, or fallback behavior.
Try-Catch with Custom Error Handling
Save it as try-catch-with-custom-error-handling.yaml:
For SKU-1, the product is found and one unit is reserved for ORD-1201. For SKU-404, the catch emits com.example.product.not-found and stores the error in the context, so reserveProduct is skipped: the shop has no reservation for ORD-1202.
This example demonstrates:
Capturing the error in a variable using
as: errorExecuting custom actions in the catch block using
doEmitting an event for notification purposes
Storing the error in the workflow context
Conditionally executing subsequent tasks based on error state
Retry Mechanisms
For transient failures, retry mechanisms allow workflows to automatically attempt the operation again after a delay.
Inline Retry Configuration
Save it as inline-retry-configuration.yaml:
The shop answers HTTP 503 twice, then returns the watering can after two retries.
This example demonstrates:
Retrying a failed operation only for an HTTP 503 response (Service Unavailable)
Initial delay of 3 seconds before the first retry
Exponential backoff for subsequent retries
Maximum of 5 retry attempts
Key aspects of retry configuration:
delay: The initial delay before the first retrybackoff: Strategy for increasing delay between retries (exponential recommended)limit: Constraints on retry attempts, typically by count or duration
Reusable Retry Configuration
For consistent retry policies across multiple operations, define reusable retry configurations:
Save it as reusable-retry-configuration.yaml:
Same result as the inline policy: two retries, then the watering can.
This example demonstrates:
Defining a reusable retry policy named
defaultReferencing the reusable policy in catch blocks
Maintaining consistent retry behavior across the workflow
Conditional Retry
Sometimes you want to retry only under specific conditions, rather than for all errors of a certain type.
Conditional Retry Example
Save it as conditional-retry-example.yaml:
Each input makes the shop fail once. After a 503 or a 429, the call is retried and the workflow returns the stock. After a 400, when is false: there is no retry, the error still matches the catch, so it counts as handled and the workflow completes with its input {"failStatus":400}. The shop's request list shows two calls for each of the first two instances and one for the third.
This example demonstrates:
Capturing the error in a variable using
as: stockErrorUsing a condition with
whento determine if retry should occurOnly retrying for server errors (5xx) or rate limiting (429)
Customizing the retry policy for these specific conditions
Error Type Hierarchies
The DSL supports hierarchical error types, allowing for more flexible error matching.
Error Type Hierarchy Example
Save it as error-type-hierarchy-example.yaml:
A valid email completes with the shop's answer. An invalid one raises the typed error, which the switch routes to handleValidation: errorType is validation. When the shop answers HTTP 503, the error is a communication error outside https://example.com/errors: the catch ignores it and the instance faults.
This example demonstrates:
A task that raises a typed error when the shop reports an invalid email
A single
catchthat only accepts errors underhttps://example.com/errorsHierarchical error matching with a
switchon the caught error's type (most specific to most generic)Different handling for each error category
Switch cases are evaluated in order, and the first matching case handles the error
Compensating Transactions
In some workflows, you need to perform cleanup actions when an error occurs after some operations have already succeeded.
Compensating Transaction Example
Save it as compensating-transaction-example.yaml:
ORD-1203 is reserved and paid. For ORD-1204 the charge answers HTTP 402: the catch releases the reservation and leaves Payment failed: HTTP 402: … in the inbox of C-100. The shop's stock goes back up by 2.
This example demonstrates:
A sequence of operations (inventory reservation, payment processing)
Capturing the output of the first operation for later reference
When a call in the
tryblock fails, executing compensating actions to clean upCanceling the inventory reservation to maintain consistency
Notifying the customer about the failure
Circuit Breaker Pattern
The circuit breaker pattern prevents repeated calls to a failing service, reducing load and allowing it to recover.
Circuit Breaker Example
Save it as circuit-breaker-example.yaml:
The shop answers HTTP 503 every time. The workflow calls it four times (the call and three retries), then stops calling: the catch has no do list, so the error counts as handled and the instance completes with its input.
This example demonstrates:
A reusable retry policy named
circuitBreakerthat bounds the calls to a failing serviceExponential backoff starting at 1 second, so each retry waits longer than the previous one
limit.attempt.count: at most 3 retries, after which the workflow stops calling the serviceThe DSL has no dedicated circuit breaker settings (failure threshold, recovery time): the retry limit is what stops the calls
Timeout Handling
Timeouts prevent workflows from hanging indefinitely when external systems are unresponsive.
Timeout Example
Save it as timeout-example.yaml:
The shop answers after 5 seconds. The 2-second task timeout is not applied: the call waits for the answer, the instance completes with the stock, and the catch branch does not run.
This example shows how a timeout would be declared and caught:
Setting a timeout for an HTTP call
Catching the specific timeout error type
Custom handling for timeout conditions
On Lemline, use a listen timeout, as in How to cancel, resume, or suspend a workflow, to bound a wait.
Comprehensive Error Handling Strategy
Several of these patterns combine in a single try task:
Save it as retry-and-fallback.yaml:
The call and its three retries fail; the workflow returns {"inventory":[],"stale":true}. Stop the shop when you are done:
This example demonstrates:
A reusable retry definition declared in
use.retriesand referenced by nameCatching only the HTTP 503 response
Retrying with exponential backoff, up to the attempt limit
A fallback result set in
catch.doonce the retries are exhausted
Best Practices for Error Handling
When implementing error handling in Lemline workflows:
Categorize errors: Distinguish between transient failures, permanent failures, and business exceptions
Use hierarchy: Organize error types hierarchically for flexible error matching
Isolate dangerous operations: Wrap risky operations in their own try-catch blocks
Add context: Capture and log relevant context information with errors
Compensate for partial failures: Always clean up after partial successes when an error occurs
Consider idempotency: Design operations to be safely retryable
Set timeouts: Prevent indefinite hanging by setting appropriate timeouts (Lemline only enforces timeouts on
listentasks; HTTP calls use fixed limits of 15 s to connect and 30 s per request)Provide good error messages: Make error messages informative for operations and customers
Implement circuit breakers: Protect services from overload during persistent failures
Conclusion
These error handling examples demonstrate how to build robust, resilient workflows in Lemline. By properly implementing try-catch blocks, retries, and compensating transactions, you can create workflows that gracefully handle failures and maintain system consistency.
For complex real-world examples that incorporate these error handling patterns, see the Real-world Examples document.