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-runtimes.git
The following commit(s) were added to refs/heads/main by this push:
new e1e992ea1b [KOGITO-9838] Improving error message (#3238)
e1e992ea1b is described below
commit e1e992ea1bb148a8c6c9896ef30abd65fc399e29
Author: Francisco Javier Tirado Sarti
<[email protected]>
AuthorDate: Fri Oct 6 12:22:16 2023 +0200
[KOGITO-9838] Improving error message (#3238)
---
.../resource/exceptions/BaseExceptionsHandler.java | 56 +++++++++++++++-------
.../exceptions/BaseExceptionHandlerTest.java | 11 +++++
.../WorkItemExecutionExceptionMapper.java | 33 +++++++++++++
.../exceptions/springboot/ExceptionsHandler.java | 6 +++
4 files changed, 88 insertions(+), 18 deletions(-)
diff --git
a/addons/common/rest-exception-handler/src/main/java/org/kie/kogito/resource/exceptions/BaseExceptionsHandler.java
b/addons/common/rest-exception-handler/src/main/java/org/kie/kogito/resource/exceptions/BaseExceptionsHandler.java
index 9c3f51d3b3..30409c0f6a 100644
---
a/addons/common/rest-exception-handler/src/main/java/org/kie/kogito/resource/exceptions/BaseExceptionsHandler.java
+++
b/addons/common/rest-exception-handler/src/main/java/org/kie/kogito/resource/exceptions/BaseExceptionsHandler.java
@@ -33,6 +33,7 @@ import org.kie.kogito.process.VariableViolationException;
import org.kie.kogito.process.workitem.InvalidLifeCyclePhaseException;
import org.kie.kogito.process.workitem.InvalidTransitionException;
import org.kie.kogito.process.workitem.NotAuthorizedException;
+import org.kie.kogito.process.workitem.WorkItemExecutionException;
public abstract class BaseExceptionsHandler<T> {
@@ -48,9 +49,9 @@ public abstract class BaseExceptionsHandler<T> {
private static class FunctionHolder<T, R> {
private final Function<Exception, R> contentGenerator;
- private final Function<R, T> responseGenerator;
+ private final Function<Exception, Function<R, T>> responseGenerator;
- public FunctionHolder(Function<Exception, R> contentGenerator,
Function<R, T> responseGenerator) {
+ public FunctionHolder(Function<Exception, R> contentGenerator,
Function<Exception, Function<R, T>> responseGenerator) {
this.contentGenerator = contentGenerator;
this.responseGenerator = responseGenerator;
}
@@ -59,20 +60,20 @@ public abstract class BaseExceptionsHandler<T> {
return contentGenerator;
}
- public Function<R, T> getResponseGenerator() {
+ public Function<Exception, Function<R, T>> getResponseGenerator() {
return responseGenerator;
}
}
- private final FunctionHolder<T, Exception> defaultHolder = new
FunctionHolder<>(ex -> ex, BaseExceptionsHandler.this::internalError);
+ private final FunctionHolder<T, Exception> defaultHolder = new
FunctionHolder<>(ex -> ex, ex -> BaseExceptionsHandler.this::internalError);
protected BaseExceptionsHandler() {
mapper = new HashMap<>();
mapper.put(InvalidLifeCyclePhaseException.class, new FunctionHolder<>(
- ex -> Collections.singletonMap(MESSAGE, ex.getMessage()),
BaseExceptionsHandler.this::badRequest));
+ ex -> Collections.singletonMap(MESSAGE, ex.getMessage()), ex
-> BaseExceptionsHandler.this::badRequest));
mapper.put(InvalidTransitionException.class, new FunctionHolder<>(
- ex -> Collections.singletonMap(MESSAGE, ex.getMessage()),
BaseExceptionsHandler.this::badRequest));
+ ex -> Collections.singletonMap(MESSAGE, ex.getMessage()), ex
-> BaseExceptionsHandler.this::badRequest));
mapper.put(NodeInstanceNotFoundException.class, new FunctionHolder<>(
ex -> {
@@ -82,7 +83,7 @@ public abstract class BaseExceptionsHandler<T> {
response.put(PROCESS_INSTANCE_ID,
exception.getProcessInstanceId());
response.put(NODE_INSTANCE_ID,
exception.getNodeInstanceId());
return response;
- }, BaseExceptionsHandler.this::notFound));
+ }, ex -> BaseExceptionsHandler.this::notFound));
mapper.put(NodeNotFoundException.class, new FunctionHolder<>(
ex -> {
@@ -92,10 +93,10 @@ public abstract class BaseExceptionsHandler<T> {
response.put(PROCESS_INSTANCE_ID,
exception.getProcessInstanceId());
response.put(NODE_ID, exception.getNodeId());
return response;
- }, BaseExceptionsHandler.this::notFound));
+ }, ex -> BaseExceptionsHandler.this::notFound));
mapper.put(NotAuthorizedException.class, new FunctionHolder<>(
- ex -> Collections.singletonMap(MESSAGE, ex.getMessage()),
BaseExceptionsHandler.this::forbidden));
+ ex -> Collections.singletonMap(MESSAGE, ex.getMessage()), ex
-> BaseExceptionsHandler.this::forbidden));
mapper.put(ProcessInstanceDuplicatedException.class, new
FunctionHolder<>(
ex -> {
@@ -104,7 +105,7 @@ public abstract class BaseExceptionsHandler<T> {
response.put(MESSAGE, exception.getMessage());
response.put(PROCESS_INSTANCE_ID,
exception.getProcessInstanceId());
return response;
- }, BaseExceptionsHandler.this::conflict));
+ }, ex -> BaseExceptionsHandler.this::conflict));
mapper.put(ProcessInstanceExecutionException.class, new
FunctionHolder<>(
ex -> {
@@ -114,7 +115,7 @@ public abstract class BaseExceptionsHandler<T> {
response.put(FAILED_NODE_ID, exception.getFailedNodeId());
response.put(MESSAGE, exception.getErrorMessage());
return response;
- }, BaseExceptionsHandler.this::internalError));
+ }, ex -> BaseExceptionsHandler.this::internalError));
mapper.put(ProcessInstanceNotFoundException.class, new
FunctionHolder<>(
ex -> {
@@ -123,12 +124,12 @@ public abstract class BaseExceptionsHandler<T> {
response.put(MESSAGE, exception.getMessage());
response.put(PROCESS_INSTANCE_ID,
exception.getProcessInstanceId());
return response;
- }, BaseExceptionsHandler.this::notFound));
+ }, ex -> BaseExceptionsHandler.this::notFound));
mapper.put(WorkItemNotFoundException.class, new FunctionHolder<>(ex ->
{
WorkItemNotFoundException exception = (WorkItemNotFoundException)
ex;
return Map.of(MESSAGE, exception.getMessage(), TASK_ID,
exception.getWorkItemId());
- }, BaseExceptionsHandler.this::notFound));
+ }, ex -> BaseExceptionsHandler.this::notFound));
mapper.put(VariableViolationException.class, new FunctionHolder<>(
ex -> {
@@ -138,9 +139,28 @@ public abstract class BaseExceptionsHandler<T> {
response.put(PROCESS_INSTANCE_ID,
exception.getProcessInstanceId());
response.put(VARIABLE, exception.getVariableName());
return response;
- }, BaseExceptionsHandler.this::badRequest));
+ }, ex -> BaseExceptionsHandler.this::badRequest));
- mapper.put(IllegalArgumentException.class, new FunctionHolder<>(ex ->
Collections.singletonMap(MESSAGE, ex.getMessage()),
BaseExceptionsHandler.this::badRequest));
+ mapper.put(WorkItemExecutionException.class, new FunctionHolder<>(
+ ex -> Map.of(MESSAGE, ex.getMessage()),
+ ex -> fromErrorCode(((WorkItemExecutionException)
ex).getErrorCode())));
+
+ mapper.put(IllegalArgumentException.class, new FunctionHolder<>(ex ->
Collections.singletonMap(MESSAGE, ex.getMessage()), ex ->
BaseExceptionsHandler.this::badRequest));
+ }
+
+ private <R> Function<R, T> fromErrorCode(String errorCode) {
+ switch (errorCode) {
+ case "400":
+ return this::badRequest;
+ case "403":
+ return this::forbidden;
+ case "404":
+ return this::notFound;
+ case "409":
+ return this::conflict;
+ default:
+ return this::internalError;
+ }
}
protected abstract <R> T badRequest(R body);
@@ -156,8 +176,8 @@ public abstract class BaseExceptionsHandler<T> {
public <R extends Exception, U> T mapException(R exception) {
FunctionHolder<T, U> holder = (FunctionHolder<T, U>)
mapper.getOrDefault(exception.getClass(), defaultHolder);
U body = holder.getContentGenerator().apply(exception);
- if (exception instanceof ProcessInstanceExecutionException) {
- Throwable rootCause = ((ProcessInstanceExecutionException)
exception).getCause();
+ if (exception instanceof ProcessInstanceExecutionException ||
exception instanceof WorkItemExecutionException) {
+ Throwable rootCause = exception.getCause();
while (rootCause != null) {
if (mapper.containsKey(rootCause.getClass())) {
@@ -167,6 +187,6 @@ public abstract class BaseExceptionsHandler<T> {
rootCause = rootCause.getCause();
}
}
- return holder.getResponseGenerator().apply(body);
+ return holder.getResponseGenerator().apply(exception).apply(body);
}
}
diff --git
a/addons/common/rest-exception-handler/src/test/java/org/kie/kogito/resource/exceptions/BaseExceptionHandlerTest.java
b/addons/common/rest-exception-handler/src/test/java/org/kie/kogito/resource/exceptions/BaseExceptionHandlerTest.java
index 0e249a4ebc..7e385e2183 100644
---
a/addons/common/rest-exception-handler/src/test/java/org/kie/kogito/resource/exceptions/BaseExceptionHandlerTest.java
+++
b/addons/common/rest-exception-handler/src/test/java/org/kie/kogito/resource/exceptions/BaseExceptionHandlerTest.java
@@ -30,6 +30,7 @@ import org.kie.kogito.process.VariableViolationException;
import org.kie.kogito.process.workitem.InvalidLifeCyclePhaseException;
import org.kie.kogito.process.workitem.InvalidTransitionException;
import org.kie.kogito.process.workitem.NotAuthorizedException;
+import org.kie.kogito.process.workitem.WorkItemExecutionException;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
@@ -140,4 +141,14 @@ class BaseExceptionHandlerTest {
"message"));
assertThat(response).isEqualTo(badRequestResponse);
}
+
+ @Test
+ void testMapWorkItemExecutionException() {
+ assertThat(tested.mapException(new WorkItemExecutionException("400",
"message"))).isEqualTo(badRequestResponse);
+ assertThat(tested.mapException(new WorkItemExecutionException("404",
"message"))).isEqualTo(notFoundResponse);
+ assertThat(tested.mapException(new WorkItemExecutionException("403",
"message"))).isEqualTo(forbiddenResponse);
+ assertThat(tested.mapException(new WorkItemExecutionException("409",
"message"))).isEqualTo(conflictResponse);
+ assertThat(tested.mapException(new WorkItemExecutionException("500",
"message"))).isEqualTo(internalErrorResponse);
+ assertThat(tested.mapException(new WorkItemExecutionException("One
error code"))).isEqualTo(internalErrorResponse);
+ }
}
diff --git
a/quarkus/addons/rest-exception-handler/src/main/java/org/kie/kogito/resource/exceptions/WorkItemExecutionExceptionMapper.java
b/quarkus/addons/rest-exception-handler/src/main/java/org/kie/kogito/resource/exceptions/WorkItemExecutionExceptionMapper.java
new file mode 100644
index 0000000000..73bdac8cc7
--- /dev/null
+++
b/quarkus/addons/rest-exception-handler/src/main/java/org/kie/kogito/resource/exceptions/WorkItemExecutionExceptionMapper.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.resource.exceptions;
+
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.Provider;
+
+import org.kie.kogito.process.workitem.WorkItemExecutionException;
+
+@Provider
+public class WorkItemExecutionExceptionMapper extends
BaseExceptionMapper<WorkItemExecutionException> {
+
+ @Override
+ public Response toResponse(WorkItemExecutionException exception) {
+ return exceptionsHandler.mapException(exception);
+ }
+}
diff --git
a/springboot/addons/rest-exception-handler/src/main/java/org/kie/kogito/resource/exceptions/springboot/ExceptionsHandler.java
b/springboot/addons/rest-exception-handler/src/main/java/org/kie/kogito/resource/exceptions/springboot/ExceptionsHandler.java
index 524b613ea8..49ab414f0a 100644
---
a/springboot/addons/rest-exception-handler/src/main/java/org/kie/kogito/resource/exceptions/springboot/ExceptionsHandler.java
+++
b/springboot/addons/rest-exception-handler/src/main/java/org/kie/kogito/resource/exceptions/springboot/ExceptionsHandler.java
@@ -28,6 +28,7 @@ import org.kie.kogito.process.VariableViolationException;
import org.kie.kogito.process.workitem.InvalidLifeCyclePhaseException;
import org.kie.kogito.process.workitem.InvalidTransitionException;
import org.kie.kogito.process.workitem.NotAuthorizedException;
+import org.kie.kogito.process.workitem.WorkItemExecutionException;
import org.kie.kogito.resource.exceptions.BaseExceptionsHandler;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
@@ -123,6 +124,11 @@ public class ExceptionsHandler extends
BaseExceptionsHandler<ResponseEntity> {
return mapException(exception);
}
+ @ExceptionHandler(WorkItemExecutionException.class)
+ public ResponseEntity toResponse(WorkItemExecutionException exception) {
+ return mapException(exception);
+ }
+
@ExceptionHandler(VariableViolationException.class)
public ResponseEntity toResponse(VariableViolationException exception) {
return mapException(exception);
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]