Skip to content

Commit

Permalink
[Fix #3475] Adding end metadata usage example
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed May 3, 2024
1 parent 1d0004e commit 2e63121
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ The main feature of this demo is that if the number is odd, an exception is thro

Hence, this workflow expects JSON input containing a natural number. This number is passed using a service operation to `EvenService` java class. If the number is even, the workflow moves to the next defined state, injecting "even" `numberType`. But if the number is odd, the class throws an `IllegalArgumentException`. This exception is handled and redirected to odd inject node by using [inline workflow error handling](https://github.com/serverlessworkflow/specification/blob/main/specification.md#Workflow-Error-Handling). This basically consists on adding `onErrors` field, where the expected exception is specified in `code` and the target state (a node injecting "odd" `numberType`) in `transition`. Finally, both execution paths finish on the same node, which prints the calculated `eventType`.

As per 0.8 version of the specification, there is no standard way to set a process in error. To do that, users can use a custom metadata key called `errorMessage` which will contain either the error message to be associated to the process instance or an expression that returns the error message to associated to the process instance. In addition to the workflow described before, this example includes a file called `errorWithMEtadata.sw.json` that illustrate the usage of such metadata.


## Installing and Running

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"id": "errorWithMetadata",
"version": "1.0",
"name": "Workflow Error example with metadata",
"description": "An example of how to abort a workflow with error given a condition",
"start": "checkEven",
"states": [
{
"name": "checkEven",
"type": "operation",
"actions": [],
"end" : true,
"metadata": {
"errorMessage": "if .number % 2 != 0 then \"Is Odd number!!!!\" else null end"
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,25 @@ public void testErrorRest() {
.statusCode(201)
.body("workflowdata.numberType", is("even"));
}

@Test
public void testErrorWithMetadata() {
given()
.contentType(ContentType.JSON)
.accept(ContentType.JSON)
.body("{\"number\" : 12342}")
.when()
.post("/errorWithMetadata")
.then()
.statusCode(201);

given()
.contentType(ContentType.JSON)
.accept(ContentType.JSON)
.body("{\"number\" : 12341}")
.when()
.post("/errorWithMetadata")
.then()
.statusCode(400);
}
}

0 comments on commit 2e63121

Please sign in to comment.