Skip to content

Commit

Permalink
clean up exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
elguardian committed Oct 29, 2024
1 parent 2472e81 commit 4ddf47b
Show file tree
Hide file tree
Showing 11 changed files with 424 additions and 255 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* 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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

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 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.ExceptionHandler.newExceptionHandler;

public abstract class AbstractExceptionsHandler<T> {

ExceptionHandler<? extends Exception, T> DEFAULT_HANDLER = newExceptionHandler(Exception.class, this::badRequest);

private Map<Class<? extends Throwable>, ExceptionHandler<? extends Throwable, T>> mapper;

private Consumer<Throwable> runPostErrorHandling;

protected AbstractExceptionsHandler() {
this(throwable -> {
});
}

protected AbstractExceptionsHandler(Consumer<Throwable> runPostErrorHandling) {
List<ExceptionHandler<? extends Throwable, T>> handlers = List.<ExceptionHandler<? 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 (ExceptionHandler<? extends Throwable, T> handler : handlers) {
this.mapper.put(handler.getType(), handler);
}
this.runPostErrorHandling = runPostErrorHandling;

}

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(Throwable exception) {
var handler = mapper.getOrDefault(exception.getClass(), DEFAULT_HANDLER);

Throwable rootCause = exception.getCause();
while (rootCause != null) {
if (mapper.containsKey(rootCause.getClass())) {
handler = mapper.get(rootCause.getClass());
exception = rootCause;
}
rootCause = rootCause.getCause();
}
runPostErrorHandling.accept(exception);
return handler.buildResponse(exception);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.HashMap;
import java.util.Map;

public class ExceptionBodyMessage {

public static final String MESSAGE = "message";
public static final String PROCESS_INSTANCE_ID = "processInstanceId";
public static final String TASK_ID = "taskId";
public static final String VARIABLE = "variable";
public static final String NODE_INSTANCE_ID = "nodeInstanceId";
public static final String NODE_ID = "nodeId";
public static final String FAILED_NODE_ID = "failedNodeId";
public static final String ID = "id";
public static final String ERROR_CODE = "errorCode";

private Map<String, String> body;

public ExceptionBodyMessage() {
body = new HashMap<>();
}

public ExceptionBodyMessage(Map<String, String> body) {
this.body = new HashMap<>(body);
}

public Map<String, String> getBody() {
return body;
}

public String getErrorCode() {
return body.getOrDefault(ERROR_CODE, "");
}
}
Loading

0 comments on commit 4ddf47b

Please sign in to comment.