From 2e63121e843d689ee699d17d55ada6dcfeed50f0 Mon Sep 17 00:00:00 2001 From: Francisco Javier Tirado Sarti Date: Tue, 30 Apr 2024 19:08:54 +0200 Subject: [PATCH] [Fix #3475] Adding end metadata usage example --- .../README.md | 2 ++ .../main/resources/errorWithMetadata.sw.json | 18 ++++++++++++++++ .../org/kie/kogito/examples/ErrorRestIT.java | 21 +++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 serverless-workflow-examples/serverless-workflow-error-quarkus/src/main/resources/errorWithMetadata.sw.json diff --git a/serverless-workflow-examples/serverless-workflow-error-quarkus/README.md b/serverless-workflow-examples/serverless-workflow-error-quarkus/README.md index 8cf7623931..612799da95 100644 --- a/serverless-workflow-examples/serverless-workflow-error-quarkus/README.md +++ b/serverless-workflow-examples/serverless-workflow-error-quarkus/README.md @@ -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 diff --git a/serverless-workflow-examples/serverless-workflow-error-quarkus/src/main/resources/errorWithMetadata.sw.json b/serverless-workflow-examples/serverless-workflow-error-quarkus/src/main/resources/errorWithMetadata.sw.json new file mode 100644 index 0000000000..7a7a9551e9 --- /dev/null +++ b/serverless-workflow-examples/serverless-workflow-error-quarkus/src/main/resources/errorWithMetadata.sw.json @@ -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" + } + } + ] +} diff --git a/serverless-workflow-examples/serverless-workflow-error-quarkus/src/test/java/org/kie/kogito/examples/ErrorRestIT.java b/serverless-workflow-examples/serverless-workflow-error-quarkus/src/test/java/org/kie/kogito/examples/ErrorRestIT.java index a1225c0962..099437d6c3 100644 --- a/serverless-workflow-examples/serverless-workflow-error-quarkus/src/test/java/org/kie/kogito/examples/ErrorRestIT.java +++ b/serverless-workflow-examples/serverless-workflow-error-quarkus/src/test/java/org/kie/kogito/examples/ErrorRestIT.java @@ -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); + } }