Skip to content

Commit

Permalink
[Fix apache#3677] Removing exception type from error message (apache#…
Browse files Browse the repository at this point in the history
…3679)

* [Fix apache#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 <gonzalo51429@gmail.com>

* [Fix apache#3677] Alternative approach

* [Fix apache#281] Adding specific exception handler

---------

Co-authored-by: Gonzalo Muñoz <gonzalo51429@gmail.com>
  • Loading branch information
2 people authored and rgdoliveira committed Oct 3, 2024
1 parent b20f0da commit d921c51
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,6 +67,7 @@ public Function<Exception, Function<R, T>> getResponseGenerator() {
}

private final FunctionHolder<T, Exception> defaultHolder = new FunctionHolder<>(ex -> ex, ex -> BaseExceptionsHandler.this::internalError);
private final FunctionHolder<T, ?> messageFunctionHolder = new FunctionHolder<>(ex -> Collections.singletonMap(MESSAGE, ex.getMessage()), ex -> BaseExceptionsHandler.this::badRequest);

protected BaseExceptionsHandler() {
mapper = new HashMap<>();
Expand Down Expand Up @@ -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 <R> Function<R, T> fromErrorCode(String errorCode) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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));
}
}

0 comments on commit d921c51

Please sign in to comment.