Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #3475] Adding end metadata usage example #1912

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading