diff --git a/serverless-workflow-examples/serverless-workflow-error-quarkus/README.md b/serverless-workflow-examples/serverless-workflow-error-quarkus/README.md index 612799da95..47b7aa2cac 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 the workflow to follow an error path instead of propagating this exception to the caller. -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`. +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`. Both execution paths then finish on the same node, which prints the calculated `numberType`. + +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 error path that prints out the failure. 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 1130ba4cf9..45abeb607f 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 void isEven(int number) { 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 0000000000..ec96c03802 --- /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 835a80df6b..b66b53d5e6 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 08d18c80b4..7ce064f4c3 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 099437d6c3..6fec4c0dc6 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 void testErrorRest() { .post("/error") .then() .statusCode(201) - .body("workflowdata.numberType", is("odd")); + .body("workflowdata.numberType", is("even")); given() .contentType(ContentType.JSON) .accept(ContentType.JSON)