forked from apache/incubator-kie-kogito-runtimes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[incubator-kie-issues-1550] Add Transaction error handling when trans…
…action is enabled (apache#3740) * [incubator-kie-issues-1550] Add Transaction error handling when transaction is enabled * ProcessGenerationIT fix tests * clean up exception handling * add exception handler * handlers creation for spring boot and quarkus * fix throwable * fix compilation errors * fix ProcessGenerationIT * fix java standalone no tx * fix quarkus exceptions handling * ProcessResourceGeneratorTest fix * fix resources * fix quarkus error handling * fix spring boot unitof work * fix transactions templates * fix template class name * fix * fix composed messages based on the exceptions processed
- Loading branch information
1 parent
97d526c
commit 54875f8
Showing
53 changed files
with
933 additions
and
328 deletions.
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
...n-handler/src/main/java/org/kie/kogito/resource/exceptions/AbstractExceptionsHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* 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 java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.kie.kogito.handler.ExceptionHandler; | ||
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; | ||
import org.kie.kogito.internal.process.workitem.WorkItemExecutionException; | ||
import org.kie.kogito.internal.process.workitem.WorkItemNotFoundException; | ||
import org.kie.kogito.process.NodeInstanceNotFoundException; | ||
import org.kie.kogito.process.NodeNotFoundException; | ||
import org.kie.kogito.process.ProcessInstanceDuplicatedException; | ||
import org.kie.kogito.process.ProcessInstanceExecutionException; | ||
import org.kie.kogito.process.ProcessInstanceNotFoundException; | ||
import org.kie.kogito.process.VariableViolationException; | ||
import org.kie.kogito.usertask.UserTaskInstanceNotAuthorizedException; | ||
import org.kie.kogito.usertask.UserTaskInstanceNotFoundException; | ||
import org.kie.kogito.usertask.lifecycle.UserTaskTransitionException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import static org.kie.kogito.resource.exceptions.ExceptionBodyMessageFunctions.nodeInstanceNotFoundMessageException; | ||
import static org.kie.kogito.resource.exceptions.ExceptionBodyMessageFunctions.nodeNotFoundMessageException; | ||
import static org.kie.kogito.resource.exceptions.ExceptionBodyMessageFunctions.processInstanceDuplicatedMessageException; | ||
import static org.kie.kogito.resource.exceptions.ExceptionBodyMessageFunctions.processInstanceExecutionMessageException; | ||
import static org.kie.kogito.resource.exceptions.ExceptionBodyMessageFunctions.processInstanceNotFoundExceptionMessageException; | ||
import static org.kie.kogito.resource.exceptions.ExceptionBodyMessageFunctions.variableViolationMessageException; | ||
import static org.kie.kogito.resource.exceptions.ExceptionBodyMessageFunctions.workItemExecutionMessageException; | ||
import static org.kie.kogito.resource.exceptions.ExceptionBodyMessageFunctions.workItemNotFoundMessageException; | ||
import static org.kie.kogito.resource.exceptions.RestExceptionHandler.newExceptionHandler; | ||
|
||
public abstract class AbstractExceptionsHandler<T> { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(AbstractExceptionsHandler.class); | ||
|
||
RestExceptionHandler<? extends Exception, T> DEFAULT_HANDLER = newExceptionHandler(Exception.class, this::badRequest); | ||
|
||
private Map<Class<? extends Throwable>, RestExceptionHandler<? extends Throwable, T>> mapper; | ||
|
||
private List<ExceptionHandler> errorHandlers; | ||
|
||
protected AbstractExceptionsHandler() { | ||
this(Collections.emptyList()); | ||
} | ||
|
||
protected AbstractExceptionsHandler(Iterable<ExceptionHandler> errorHandlers) { | ||
List<RestExceptionHandler<? extends Throwable, T>> handlers = List.<RestExceptionHandler<? extends Throwable, T>> of( | ||
newExceptionHandler(InvalidLifeCyclePhaseException.class, this::badRequest), | ||
newExceptionHandler(UserTaskTransitionException.class, this::badRequest), | ||
newExceptionHandler(UserTaskInstanceNotFoundException.class, this::notFound), | ||
newExceptionHandler(UserTaskInstanceNotAuthorizedException.class, this::forbidden), | ||
newExceptionHandler(InvalidTransitionException.class, this::badRequest), | ||
newExceptionHandler(NodeInstanceNotFoundException.class, nodeInstanceNotFoundMessageException(), this::notFound), | ||
newExceptionHandler(NodeNotFoundException.class, nodeNotFoundMessageException(), this::notFound), | ||
newExceptionHandler(NotAuthorizedException.class, this::forbidden), | ||
newExceptionHandler(ProcessInstanceDuplicatedException.class, processInstanceDuplicatedMessageException(), this::conflict), | ||
newExceptionHandler(ProcessInstanceExecutionException.class, processInstanceExecutionMessageException(), this::internalError), | ||
newExceptionHandler(ProcessInstanceNotFoundException.class, processInstanceNotFoundExceptionMessageException(), this::notFound), | ||
newExceptionHandler(WorkItemNotFoundException.class, workItemNotFoundMessageException(), this::notFound), | ||
newExceptionHandler(VariableViolationException.class, variableViolationMessageException(), this::badRequest), | ||
newExceptionHandler(WorkItemExecutionException.class, workItemExecutionMessageException(), this::fromErrorCode), | ||
newExceptionHandler(IllegalArgumentException.class, this::badRequest), | ||
newExceptionHandler(MessageException.class, this::badRequest)); | ||
|
||
this.mapper = new HashMap<>(); | ||
for (RestExceptionHandler<? extends Throwable, T> handler : handlers) { | ||
this.mapper.put(handler.getType(), handler); | ||
} | ||
this.errorHandlers = new ArrayList<>(); | ||
errorHandlers.iterator().forEachRemaining(this.errorHandlers::add); | ||
|
||
} | ||
|
||
private T fromErrorCode(ExceptionBodyMessage message) { | ||
switch (message.getErrorCode()) { | ||
case "400": | ||
return badRequest(message); | ||
case "403": | ||
return forbidden(message); | ||
case "404": | ||
return notFound(message); | ||
case "409": | ||
return conflict(message); | ||
default: | ||
return internalError(message); | ||
} | ||
} | ||
|
||
protected abstract T badRequest(ExceptionBodyMessage body); | ||
|
||
protected abstract T conflict(ExceptionBodyMessage body); | ||
|
||
protected abstract T internalError(ExceptionBodyMessage body); | ||
|
||
protected abstract T notFound(ExceptionBodyMessage body); | ||
|
||
protected abstract T forbidden(ExceptionBodyMessage body); | ||
|
||
public T mapException(Exception exceptionThrown) { | ||
|
||
var handler = mapper.getOrDefault(exceptionThrown.getClass(), DEFAULT_HANDLER); | ||
ExceptionBodyMessage message = handler.getContent(exceptionThrown); | ||
|
||
Throwable rootCause = exceptionThrown.getCause(); | ||
while (rootCause != null) { | ||
if (mapper.containsKey(rootCause.getClass())) { | ||
handler = mapper.get(rootCause.getClass()); | ||
message.merge(handler.getContent(rootCause)); | ||
} | ||
rootCause = rootCause.getCause(); | ||
} | ||
// we invoked the error handlers | ||
errorHandlers.forEach(e -> e.handle(exceptionThrown)); | ||
T response = handler.buildResponse(message); | ||
LOG.debug("mapping exception {} with response {}", exceptionThrown, message.getBody()); | ||
return response; | ||
} | ||
} |
203 changes: 0 additions & 203 deletions
203
...ption-handler/src/main/java/org/kie/kogito/resource/exceptions/BaseExceptionsHandler.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.