Error Handling with Try-Catch
A try task runs a list of tasks. If a task fails, catch can filter the error, retry the try block, or execute a fallback list.
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 HTTP examples call the tutorial shop; start it from the root of the Lemline source checkout:
docker compose -f examples/tutorial-services/compose.yaml up -d
Filter an error and retry
Set catch.errors.with to match predefined fields such as the error type or status. Use catch.when for a jq predicate over the error when the field filter is insufficient. A failed HTTP call is a communication error whose status is the HTTP status, so the example below filters on both.
document:
dsl: '1.0.3'
namespace: examples
name: retry-and-fallback
version: '1.0.0'
use:
retries:
transient:
delay: PT2S
backoff:
exponential: {}
limit:
attempt:
count: 3
do:
- fetchInventory:
try:
- requestInventory:
call: http
with:
method: get
endpoint: http://127.0.0.1:8787/stock
query:
fail: '503'
failTimes: ${ .failures // 0 | tostring }
key: ${ $workflow.id }
catch:
errors:
with:
type: https://open-workflow-specification.org/spec/1.0.0/errors/communication
status: 503
retry: transient
do:
- useCachedInventory:
set:
inventory: []
stale: true
Save it as retry-and-fallback.yaml. The failures input makes the shop answer HTTP 503 that many times:
bin/lemline definition post -f retry-and-fallback.yaml
bin/lemline instance start examples retry-and-fallback --input '{"failures":1}'
bin/lemline instance start examples retry-and-fallback --input '{"failures":5}'
The first instance returns the stock after one retry. The second fails four times and returns {"inventory":[],"stale":true}. Allow about a minute: the worker picks up delayed retries every 10 seconds.
Read the caught error
The caught error is available as $error by default. Use catch.as to choose another name. The following example reads error fields inside the catch task list.
document:
dsl: '1.0.3'
namespace: examples
name: format-error-result
version: '1.0.0'
do:
- submitOrder:
try:
- submit:
call: http
with:
method: post
endpoint: http://127.0.0.1:8787/reservations
body: ${ . }
catch:
as: orderError
do:
- formatFailure:
set:
success: false
status: ${ $orderError.status }
errorType: ${ $orderError.type }
reason: ${ $orderError.title }
Save it as format-error-result.yaml. Send one valid reservation and two the shop refuses:
bin/lemline definition post -f format-error-result.yaml
bin/lemline instance start examples format-error-result --input '{"orderId":"ORD-901","items":[{"id":"SKU-1","quantity":1}]}'
bin/lemline instance start examples format-error-result --input '{"orderId":"ORD-902","items":[{"id":"SKU-9","quantity":1}]}'
bin/lemline instance start examples format-error-result --input '{"orderId":"ORD-903","items":[{"id":"SKU-1","quantity":500}]}'
The first instance returns the reservation. The shop answers HTTP 400 to the second and HTTP 409 to the third; both complete with success: false, the communication error type, the HTTP status (400, then 409) and a reason that starts with HTTP 400: {"error":"Invalid items" and HTTP 409: {"error":"Out of stock" respectively.
Propagate failures
Errors that do not match the filter propagate to the surrounding task scope. A fallback can also raise an error if the workflow should fault after cleanup.
document:
dsl: '1.0.3'
namespace: examples
name: compensate-reservation
version: '1.0.0'
do:
- reserveInventory:
call: http
with:
method: post
endpoint: http://127.0.0.1:8787/reservations
body:
orderId: ${ $workflow.input.orderId }
items: ${ $workflow.input.items }
export:
as: '${ $context + { reservationId: .id } }'
- completeOrder:
try:
- chargePayment:
call: http
with:
method: post
endpoint: http://127.0.0.1:8787/charges
body:
orderId: ${ $workflow.input.orderId }
paymentInfo: ${ $workflow.input.paymentInfo }
catch:
do:
- releaseInventory:
call: http
with:
method: delete
endpoint: '${ "http://127.0.0.1:8787/reservations/\($context.reservationId | @uri)" }'
- reportFailure:
raise:
error:
type: https://errors.example.com/order-failed
status: 502
title: Order completion failed
- compareCarriers:
fork:
branches:
- economy:
try:
- quote:
call: http
with:
method: post
endpoint: http://127.0.0.1:8787/quotes/economy
body:
orderId: ${ $workflow.input.orderId }
failTwice: ${ $workflow.input.failTwice // false }
catch:
retry:
delay: { seconds: 1 }
limit:
attempt: { count: 3 }
- express:
call: http
with:
method: post
endpoint: http://127.0.0.1:8787/quotes/express
body:
orderId: ${ $workflow.input.orderId }
- selectCarrier:
set:
carrier: ${ min_by(.price).carrier }
- bookShipment:
call: http
with:
method: post
endpoint: http://127.0.0.1:8787/shipments
body:
orderId: ${ $workflow.input.orderId }
carrier: ${ .carrier }
- confirmShipment:
call: http
with:
method: post
endpoint: http://127.0.0.1:8787/notifications
body:
reference: ${ .orderId }
recipient: customer
message: '${ "Shipment booked with " + .carrier }'
Save it as compensate-reservation.yaml and send a declined payment:
bin/lemline definition post -f compensate-reservation.yaml
bin/lemline instance start examples compensate-reservation --input '{"orderId":"ORD-904","items":[{"id":"SKU-1","quantity":2}],"paymentInfo":{"token":"declined"}}'
The charge fails with HTTP 402, the catch releases the reservation, then raises Order completion failed: the instance faults with that title and status 502, and the shop shows the ORD-904 reservation as released.
Stop the shop when you are done:
docker compose -f examples/tutorial-services/compose.yaml down
See Retry Mechanism and Custom Errors.
Run the shop checkout tutorial to observe the release before the workflow faults.
25 November 2026