From 7d7b406699ec40a53278c2c2a7c319f0ca5ffbbc Mon Sep 17 00:00:00 2001 From: Francisco Javier Tirado Sarti <65240126+fjtirado@users.noreply.github.com> Date: Wed, 25 Sep 2024 11:08:25 +0200 Subject: [PATCH] [Fix #3677] Removing exception type from error message (#3679) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [Fix #3677] Removing exception type from error message * Update jbpm/jbpm-flow/src/main/java/org/jbpm/workflow/instance/impl/WorkflowProcessInstanceImpl.java Co-authored-by: Gonzalo Muñoz * [Fix #3677] Alternative approach * [Fix #281] Adding specific exception handler --------- Co-authored-by: Gonzalo Muñoz --- .../exceptions/BaseExceptionsHandler.java | 6 +++-- .../process/runtime/MessageException.java | 27 +++++++++++++++++++ .../impl/WorkflowProcessInstanceImpl.java | 3 ++- .../actions/ErrorExpressionAction.java | 7 ++--- 4 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 api/kogito-api/src/main/java/org/kie/kogito/internal/process/runtime/MessageException.java 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 cdd3b2ad524..3da864ee7eb 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 @@ -23,6 +23,7 @@ import java.util.Map; import java.util.function.Function; +import org.kie.kogito.internal.process.runtime.MessageException; import org.kie.kogito.internal.process.workitem.InvalidLifeCyclePhaseException; import org.kie.kogito.internal.process.workitem.InvalidTransitionException; import org.kie.kogito.internal.process.workitem.NotAuthorizedException; @@ -66,6 +67,7 @@ public Function> getResponseGenerator() { } private final FunctionHolder defaultHolder = new FunctionHolder<>(ex -> ex, ex -> BaseExceptionsHandler.this::internalError); + private final FunctionHolder messageFunctionHolder = new FunctionHolder<>(ex -> Collections.singletonMap(MESSAGE, ex.getMessage()), ex -> BaseExceptionsHandler.this::badRequest); protected BaseExceptionsHandler() { mapper = new HashMap<>(); @@ -144,8 +146,8 @@ protected BaseExceptionsHandler() { 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)); + mapper.put(IllegalArgumentException.class, messageFunctionHolder); + mapper.put(MessageException.class, messageFunctionHolder); } private Function fromErrorCode(String errorCode) { diff --git a/api/kogito-api/src/main/java/org/kie/kogito/internal/process/runtime/MessageException.java b/api/kogito-api/src/main/java/org/kie/kogito/internal/process/runtime/MessageException.java new file mode 100644 index 00000000000..5aaccdc1cea --- /dev/null +++ b/api/kogito-api/src/main/java/org/kie/kogito/internal/process/runtime/MessageException.java @@ -0,0 +1,27 @@ +/* + * 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.internal.process.runtime; + +public class MessageException extends RuntimeException { + private static final long serialVersionUID = 1L; + + public MessageException(String message) { + super(message); + } +} diff --git a/jbpm/jbpm-flow/src/main/java/org/jbpm/workflow/instance/impl/WorkflowProcessInstanceImpl.java b/jbpm/jbpm-flow/src/main/java/org/jbpm/workflow/instance/impl/WorkflowProcessInstanceImpl.java index 79ef9e1d070..7ae6edf3fb1 100755 --- a/jbpm/jbpm-flow/src/main/java/org/jbpm/workflow/instance/impl/WorkflowProcessInstanceImpl.java +++ b/jbpm/jbpm-flow/src/main/java/org/jbpm/workflow/instance/impl/WorkflowProcessInstanceImpl.java @@ -85,6 +85,7 @@ import org.kie.kogito.internal.process.runtime.KogitoNodeInstanceContainer; import org.kie.kogito.internal.process.runtime.KogitoProcessInstance; import org.kie.kogito.internal.process.runtime.KogitoWorkflowProcess; +import org.kie.kogito.internal.process.runtime.MessageException; import org.kie.kogito.jobs.DurationExpirationTime; import org.kie.kogito.jobs.JobsService; import org.kie.kogito.jobs.ProcessInstanceJobDescription; @@ -1164,7 +1165,7 @@ public void setErrorState(NodeInstance nodeInstanceInError, Exception e) { this.nodeInstanceIdInError = nodeInstanceInError.getId(); this.errorCause = Optional.of(e); Throwable rootException = getRootException(e); - this.errorMessage = rootException.getClass().getCanonicalName() + " - " + rootException.getMessage(); + this.errorMessage = rootException instanceof MessageException ? rootException.getMessage() : rootException.getClass().getCanonicalName() + " - " + rootException.getMessage(); setState(STATE_ERROR); logger.error("Unexpected error while executing node {} in process instance {}", nodeInstanceInError.getNode().getName(), this.getStringId(), e); ((InternalProcessRuntime) getKnowledgeRuntime().getProcessRuntime()).getProcessEventSupport().fireOnError(this, nodeInstanceInError, getKnowledgeRuntime(), e); diff --git a/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/actions/ErrorExpressionAction.java b/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/actions/ErrorExpressionAction.java index a4b1f1da11d..04a22c1e2c1 100644 --- a/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/actions/ErrorExpressionAction.java +++ b/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/actions/ErrorExpressionAction.java @@ -21,6 +21,7 @@ import org.jbpm.process.instance.ProcessInstance; import org.jbpm.workflow.instance.NodeInstance; import org.kie.kogito.internal.process.runtime.KogitoProcessContext; +import org.kie.kogito.internal.process.runtime.MessageException; import com.fasterxml.jackson.databind.JsonNode; @@ -40,11 +41,11 @@ public void execute(KogitoProcessContext context) throws Exception { } } } else { - setError(context, expr.toString()); + setError(context, "The expression used for generating error message is not a valid one: " + expr.asString()); } } - private void setError(KogitoProcessContext context, String error) { - ((ProcessInstance) context.getProcessInstance()).setErrorState((NodeInstance) context.getNodeInstance(), new IllegalArgumentException(error)); + private void setError(KogitoProcessContext context, String message) { + ((ProcessInstance) context.getProcessInstance()).setErrorState((NodeInstance) context.getNodeInstance(), new MessageException(message)); } }