This is an automated email from the ASF dual-hosted git repository.
fjtiradosarti pushed a commit to branch main
in repository
https://gitbox.apache.org/repos/asf/incubator-kie-kogito-examples.git
The following commit(s) were added to refs/heads/main by this push:
new 49d887000 KOGITO-7453: Extend error handling example to show usage of
error code (#1341)
49d887000 is described below
commit 49d887000b233d8d9c02a4dea528ce42cfbb3bef
Author: Martin Weiler <[email protected]>
AuthorDate: Tue Jul 23 08:53:40 2024 -0600
KOGITO-7453: Extend error handling example to show usage of error code
(#1341)
* KOGITO-7453: Extend error handling example to show usage of error code
* KOGITO-7453: Replace FQCN in error code with error message
* KOGITO-7453: Apply QE review changes
* Update PublishRestService.java
* Update application.properties
* Create application.properties
* Delete
serverless-workflow-examples/serverless-workflow-error-quarkus/src/test/resources/application.properties
* Create application.properties
* Delete
serverless-workflow-examples/serverless-workflow-error-quarkus/src/test/resources/application.properties
* Update application.properties
* Update ErrorRestIT.java
---------
Co-authored-by: Francisco Javier Tirado Sarti
<[email protected]>
---
.../serverless-workflow-error-quarkus/README.md | 8 ++-
.../java/org/kie/kogito/examples/EvenService.java | 7 ---
.../kie/kogito/examples/PublishRestService.java | 60 ++++++++++++++++++
.../src/main/resources/application.properties | 5 +-
.../src/main/resources/error.sw.json | 72 +++++++++++++++++-----
.../java/org/kie/kogito/examples/ErrorRestIT.java | 2 +-
6 files changed, 127 insertions(+), 27 deletions(-)
diff --git
a/serverless-workflow-examples/serverless-workflow-error-quarkus/README.md
b/serverless-workflow-examples/serverless-workflow-error-quarkus/README.md
index 612799da9..47b7aa2ca 100644
--- a/serverless-workflow-examples/serverless-workflow-error-quarkus/README.md
+++ b/serverless-workflow-examples/serverless-workflow-error-quarkus/README.md
@@ -6,10 +6,11 @@ This example contains a simple workflow service that
illustrate error handling.
The service is described using JSON format as defined in the
[CNCF Serverless Workflow
specification](https://github.com/serverlessworkflow/specification).
-The workflow check if the number is odd or even and print a message indicating
that.
-The main feature of this demo is that if the number is odd, an exception is
thrown, and it is the exception error handling the one that sets the odd
message.
+The workflow consists of a Java service that determines if a provided number
is odd or even, followed by a call to a REST service to publish any even
number. The main feature of this demo is to show different ways of exception
handling within a workflow. In the Java service, if the number is odd, an
exception is thrown, and it is the exception error handling the one that sets
the odd message. If the REST service call returns a 400 response, the exception
error handling mechanism causes t [...]
-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/specific
[...]
+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/specific
[...]
+
+In the next step, the workflow calls the `PublishRestService` via REST. This
service evaluates the `numberType` from the previous step and either returns
with a successful response if the number is `even`, or with a failure response
(HTTP status code 400) if the number is `odd`. The failure event is handled as
the action node contains an `onError` definition. The referenced error is
defined as `"code": "HTTP:400"`. If this exception is encountered, the workflow
execution continues on an [...]
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.
@@ -103,4 +104,5 @@ In Quarkus you should see the log message printed:
```text
odd
+Fail to publish result
```
diff --git
a/serverless-workflow-examples/serverless-workflow-error-quarkus/src/main/java/org/kie/kogito/examples/EvenService.java
b/serverless-workflow-examples/serverless-workflow-error-quarkus/src/main/java/org/kie/kogito/examples/EvenService.java
index 1130ba4cf..45abeb607 100644
---
a/serverless-workflow-examples/serverless-workflow-error-quarkus/src/main/java/org/kie/kogito/examples/EvenService.java
+++
b/serverless-workflow-examples/serverless-workflow-error-quarkus/src/main/java/org/kie/kogito/examples/EvenService.java
@@ -28,11 +28,4 @@ public class EvenService {
throw new IllegalArgumentException("Odd situation");
}
}
-
- public void isSquare(int number) {
- double sqrt = Math.sqrt(number);
- if (sqrt == Math.round(sqrt)) {
- throw new RuntimeException("Number has a perfect square");
- }
- }
}
diff --git
a/serverless-workflow-examples/serverless-workflow-error-quarkus/src/main/java/org/kie/kogito/examples/PublishRestService.java
b/serverless-workflow-examples/serverless-workflow-error-quarkus/src/main/java/org/kie/kogito/examples/PublishRestService.java
new file mode 100644
index 000000000..ec96c0380
--- /dev/null
+++
b/serverless-workflow-examples/serverless-workflow-error-quarkus/src/main/java/org/kie/kogito/examples/PublishRestService.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2020 Red Hat, Inc. and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.kie.kogito.examples;
+
+import jakarta.annotation.PostConstruct;
+import jakarta.ws.rs.POST;
+import jakarta.ws.rs.Path;
+import jakarta.ws.rs.PathParam;
+import jakarta.ws.rs.Produces;
+import jakarta.ws.rs.core.MediaType;
+import jakarta.ws.rs.core.Response;
+import jakarta.ws.rs.core.Response.Status;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+@Path("/publish")
+@Produces(MediaType.APPLICATION_JSON)
+public class PublishRestService {
+
+ private ObjectMapper objectMapper;
+ private static final Logger logger =
LoggerFactory.getLogger(PublishRestService.class);
+
+ @PostConstruct
+ void init() {
+ objectMapper = new ObjectMapper();
+ }
+
+ @Path("/{type}/{number}")
+ @POST
+ public Response publishEvenNumber(@PathParam("type") String type,
@PathParam("number") int number) {
+ logger.info("Publish type " + type + " number " + number);
+ // check if the input number is even
+ if (!"even".equals(type)) {
+ return
Response.status(Status.BAD_REQUEST).entity(objectMapper.createObjectNode().put("error",
"Perfect square assessment not supported for odd numbers by this
service")).build();
+ }
+ return
Response.ok().entity(objectMapper.createObjectNode().put("perfect",
isPerfectSquare(number))).build();
+ }
+
+ private boolean isPerfectSquare(int number) {
+ double sqrt = Math.sqrt(number);
+ return (sqrt == Math.round(sqrt));
+ }
+
+}
diff --git
a/serverless-workflow-examples/serverless-workflow-error-quarkus/src/main/resources/application.properties
b/serverless-workflow-examples/serverless-workflow-error-quarkus/src/main/resources/application.properties
index 835a80df6..b66b53d5e 100644
---
a/serverless-workflow-examples/serverless-workflow-error-quarkus/src/main/resources/application.properties
+++
b/serverless-workflow-examples/serverless-workflow-error-quarkus/src/main/resources/application.properties
@@ -19,6 +19,9 @@
# Packaging
# quarkus.package.type=fast-jar
+
+kogito.sw.functions.publishPerfectSquare.host=localhost
+kogito.sw.functions.publishPerfectSquare.port=8081
quarkus.native.native-image-xmx=8g
# profile to pack this example into a container, to use it execute activate
the maven container profile, -Dcontainer
@@ -26,4 +29,4 @@ quarkus.native.native-image-xmx=8g
%container.quarkus.container-image.push=false
%container.quarkus.container-image.group=${USER}
%container.quarkus.container-image.registry=dev.local
-%container.quarkus.container-image.tag=1.0-SNAPSHOT
\ No newline at end of file
+%container.quarkus.container-image.tag=1.0-SNAPSHOT
diff --git
a/serverless-workflow-examples/serverless-workflow-error-quarkus/src/main/resources/error.sw.json
b/serverless-workflow-examples/serverless-workflow-error-quarkus/src/main/resources/error.sw.json
index 08d18c80b..7ce064f4c 100644
---
a/serverless-workflow-examples/serverless-workflow-error-quarkus/src/main/resources/error.sw.json
+++
b/serverless-workflow-examples/serverless-workflow-error-quarkus/src/main/resources/error.sw.json
@@ -8,7 +8,11 @@
"errors": [
{
"name": "odd number",
- "code": "java.lang.RuntimeException"
+ "code": "Odd situation"
+ },
+ {
+ "name": "bad request",
+ "code": "HTTP:400"
}
],
"functions": [
@@ -18,9 +22,9 @@
"operation": "service:java:org.kie.kogito.examples.EvenService::isEven"
},
{
- "name": "isSqr",
+ "name": "publishPerfectSquare",
"type": "custom",
- "operation": "service:java:org.kie.kogito.examples.EvenService::isSquare"
+ "operation": "rest:post:/publish/{type}/{number}"
},
{
"name": "printMessage",
@@ -41,15 +45,6 @@
"number": "$.number"
}
}
- },
- {
- "name": "checkSqrAction",
- "functionRef": {
- "refName": "isSqr",
- "arguments": {
- "number": "$.number"
- }
- }
}
],
"transition": "even",
@@ -66,7 +61,7 @@
"data": {
"numberType": "even"
},
- "transition": "finish"
+ "transition": "print"
},
{
"name": "odd",
@@ -74,10 +69,10 @@
"data": {
"numberType": "odd"
},
- "transition": "finish"
+ "transition": "print"
},
{
- "name": "finish",
+ "name": "print",
"type": "operation",
"actions": [
{
@@ -90,6 +85,53 @@
}
}
],
+ "transition": "publish"
+ },
+ {
+ "name": "publish",
+ "type": "operation",
+ "actions": [
+ {
+ "name": "publishAction",
+ "functionRef" : {
+ "refName": "publishPerfectSquare",
+ "arguments": {
+ "type": "$.numberType",
+ "number": "$.number"
+ }
+ }
+ }
+ ],
+ "end": true,
+ "onErrors": [
+ {
+ "errorRef": "bad request",
+ "transition": "setError"
+ }
+ ]
+ },
+ {
+ "name": "setError",
+ "type": "inject",
+ "data": {
+ "errormessage": "Fail to publish result"
+ },
+ "transition": "reportError"
+ },
+ {
+ "name": "reportError",
+ "type": "operation",
+ "actions": [
+ {
+ "name": "printAction",
+ "functionRef": {
+ "refName": "printMessage",
+ "arguments": {
+ "message": "errormessage"
+ }
+ }
+ }
+ ],
"end": true
}
]
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 099437d6c..6fec4c0dc 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
@@ -46,7 +46,7 @@ public class ErrorRestIT {
.post("/error")
.then()
.statusCode(201)
- .body("workflowdata.numberType", is("odd"));
+ .body("workflowdata.numberType", is("even"));
given()
.contentType(ContentType.JSON)
.accept(ContentType.JSON)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]