Vibescript Showcase
Task error propagation
A failing task propagates out of its Tasks.run scope; wrap the scope in begin/rescue to recover a structured outcome.
Source
showcase/concurrency/task_error_propagation.vibe
# title: Task error propagation
# category: Vibescript Showcase
# difficulty: Showcase
# summary: A failing task propagates out of its Tasks.run scope; wrap the scope in begin/rescue to recover a structured outcome.
# description: When a spawned task raises, the failure propagates out of the structured scope — at handle.value and again when the scope exits. Wrapping the whole Tasks.run in begin/rescue catches that propagation, letting the caller turn a task error into structured data instead of crashing the run.
# tags: concurrency, tasks, structured-concurrency, errors
# vibe: 0.4
def validate_amount(record)
assert record[:amount] > 0, "amount must be positive"
record[:amount]
end
def settle(record)
begin
Tasks.run do |tasks|
handle = tasks.spawn(:validate_amount, record)
amount = handle.value
{ id: record[:id], ok: true, amount: amount }
end
rescue
{ id: record[:id], ok: false, error: "validation failed" }
end
end
def run
{
valid: settle({ id: "p1", amount: 50 }),
invalid: settle({ id: "p2", amount: 0 })
}
end
Output
Press run to execute run from this example.