Skip to content

Commit

Permalink
Merge pull request #5399 from poorna2152/master_worker_with_on_fail
Browse files Browse the repository at this point in the history
[Master] Add named worker with on fail clause bbe
  • Loading branch information
lochana-chathura authored May 7, 2024
2 parents 8e8729f + 2626a97 commit 7d59323
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,13 @@
"verifyOutput": true,
"isLearnByExample": true
},
{
"name": "Named worker with on fail clause",
"url": "named-worker-with-on-fail-clause",
"verifyBuild": true,
"verifyOutput": true,
"isLearnByExample": true
},
{
"name": "Synchronize message passing",
"url": "synchronize-message-passing",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import ballerina/io;

public function main() {
int[] values = [2, 3, 4, 5];
int value = 0;

worker w1 {
int index = check getIndex(values, value);
index -> function;
} on fail {
// Handle the error thrown in the worker body.
-1 -> function;
}

int|error:NoMessage result = <- w1 | w1;
io:println(result);
}

function getIndex(int[] values, int value) returns int|error =>
let int? index = values.indexOf(value) in index ?: error("value not found");
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Named worker with on fail clause

The `on fail` clause can be used with a named worker, to handle any errors that occur within the worker's body.

::: code named_worker_with_on_fail_clause.bal :::

::: out named_worker_with_on_fail_clause.out :::
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
description: This BBE demonstrates the use of `on fail` clause with named workers
keywords: ballerina, ballerina by example, bbe, worker, named workers, on fail
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$ bal run named_worker_with_on_fail_clause.bal
-1

0 comments on commit 7d59323

Please sign in to comment.