Skip to content

Commit

Permalink
[incubator-kie-issues-1520] Proper user task exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
elguardian committed Oct 8, 2024
1 parent 30cf1bd commit f83306b
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
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;

public abstract class BaseExceptionsHandler<T> {

Expand Down Expand Up @@ -74,6 +77,15 @@ protected BaseExceptionsHandler() {
mapper.put(InvalidLifeCyclePhaseException.class, new FunctionHolder<>(
ex -> Collections.singletonMap(MESSAGE, ex.getMessage()), ex -> BaseExceptionsHandler.this::badRequest));

mapper.put(UserTaskTransitionException.class, new FunctionHolder<>(
ex -> Collections.singletonMap(MESSAGE, ex.getMessage()), ex -> BaseExceptionsHandler.this::badRequest));

mapper.put(UserTaskInstanceNotFoundException.class, new FunctionHolder<>(
ex -> Collections.singletonMap(MESSAGE, ex.getMessage()), ex -> BaseExceptionsHandler.this::notFound));

mapper.put(UserTaskInstanceNotAuthorizedException.class, new FunctionHolder<>(
ex -> Collections.singletonMap(MESSAGE, ex.getMessage()), ex -> BaseExceptionsHandler.this::forbidden));

mapper.put(InvalidTransitionException.class, new FunctionHolder<>(
ex -> Collections.singletonMap(MESSAGE, ex.getMessage()), ex -> BaseExceptionsHandler.this::badRequest));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.usertask;

public class UserTaskInstanceNotAuthorizedException extends RuntimeException {

private static final long serialVersionUID = 6224742517975146469L;

public UserTaskInstanceNotAuthorizedException() {
// do nothing
}

public UserTaskInstanceNotAuthorizedException(String msg) {
super(msg);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.usertask;

public class UserTaskInstanceNotFoundException extends RuntimeException {

private static final long serialVersionUID = 6224742517975146469L;

public UserTaskInstanceNotFoundException() {
// do nothing
}

public UserTaskInstanceNotFoundException(String id) {
super("User task Instance " + id + " + not found");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import java.util.Set;

import org.kie.kogito.auth.IdentityProvider;
import org.kie.kogito.internal.process.workitem.NotAuthorizedException;
import org.kie.kogito.usertask.UserTaskAssignmentStrategy;
import org.kie.kogito.usertask.UserTaskInstance;
import org.kie.kogito.usertask.UserTaskInstanceNotAuthorizedException;
import org.kie.kogito.usertask.impl.DefaultUserTaskInstance;
import org.kie.kogito.usertask.lifecycle.UserTaskLifeCycle;
import org.kie.kogito.usertask.lifecycle.UserTaskState;
Expand Down Expand Up @@ -205,7 +205,7 @@ private void checkPermission(UserTaskInstance userTaskInstance, IdentityProvider
}
}

throw new NotAuthorizedException("user " + user + " with roles " + roles + " not autorized to perform an operation on user task " + userTaskInstance.getId());
throw new UserTaskInstanceNotAuthorizedException("user " + user + " with roles " + roles + " not autorized to perform an operation on user task " + userTaskInstance.getId());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.kie.kogito.usertask.UserTask;
import org.kie.kogito.usertask.UserTaskConfig;
import org.kie.kogito.usertask.UserTaskInstance;
import org.kie.kogito.usertask.UserTaskInstanceNotAuthorizedException;
import org.kie.kogito.usertask.UserTasks;
import org.kie.kogito.usertask.impl.lifecycle.DefaultUserTaskLifeCycle;

Expand Down Expand Up @@ -396,7 +397,7 @@ public void testBasicUserTaskProcessClaimAndCompleteWrongUser() throws Exception
List<UserTaskInstance> userTaskInstances = userTasks.instances().findByIdentity(IdentityProviders.of("john"));
assertThat(userTaskInstances).isNotNull().hasSize(1);
UserTaskInstance utInvalid = userTaskInstances.get(0);
assertThatExceptionOfType(NotAuthorizedException.class).isThrownBy(() -> utInvalid.transition(CLAIM, emptyMap(), IdentityProviders.of("invalid")));
assertThatExceptionOfType(UserTaskInstanceNotAuthorizedException.class).isThrownBy(() -> utInvalid.transition(CLAIM, emptyMap(), IdentityProviders.of("invalid")));

assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.List;
import java.util.Collection;

import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.UserTaskInstanceNotFoundException;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;
Expand All @@ -45,6 +45,7 @@
import org.kie.kogito.process.WorkItem;
import org.kie.kogito.process.impl.Sig;
import org.kie.kogito.services.uow.UnitOfWorkExecutor;
import org.kie.kogito.usertask.UserTaskInstanceNotFoundException;
import org.kie.kogito.usertask.UserTaskService;
import org.kie.kogito.usertask.view.UserTaskView;
import org.kie.kogito.usertask.view.UserTaskTransitionView;
Expand All @@ -69,7 +70,7 @@ public List<UserTaskView> list(@QueryParam("user") String user, @QueryParam("gro
@Path("/{taskId}")
@Produces(MediaType.APPLICATION_JSON)
public UserTaskView find(@PathParam("taskId") String taskId, @QueryParam("user") String user, @QueryParam("group") List<String> groups) {
return userTaskService.getUserTaskInstance(taskId, IdentityProviders.of(user, groups)).orElseThrow(NotFoundException::new);
return userTaskService.getUserTaskInstance(taskId, IdentityProviders.of(user, groups)).orElseThrow(UserTaskInstanceNotFoundException::new);
}

@POST
Expand All @@ -81,7 +82,7 @@ public UserTaskView transition(
@QueryParam("transitionId") String transitionId,
@QueryParam("user") String user,
@QueryParam("group") List<String> groups, Map<String, Object> data) {
return userTaskService.transition(taskId, transitionId, data, IdentityProviders.of(user, groups)).orElseThrow(NotFoundException::new);
return userTaskService.transition(taskId, transitionId, data, IdentityProviders.of(user, groups)).orElseThrow(UserTaskInstanceNotFoundException::new);
}

@GET
Expand All @@ -102,7 +103,7 @@ public UserTaskView setOutput(
@QueryParam("user") String user,
@QueryParam("group") List<String> groups,
Map<String, Object> data) {
return userTaskService.setOutputs(taskId, data, IdentityProviders.of(user, groups)).orElseThrow(NotFoundException::new);
return userTaskService.setOutputs(taskId, data, IdentityProviders.of(user, groups)).orElseThrow(UserTaskInstanceNotFoundException::new);
}

@PUT
Expand All @@ -113,7 +114,7 @@ public UserTaskView setOutput(@PathParam("id") String id,
@QueryParam("user") String user,
@QueryParam("group") List<String> groups,
Map<String, Object> data) {
return userTaskService.setInputs(taskId, data, IdentityProviders.of(user, groups)).orElseThrow(NotFoundException::new);
return userTaskService.setInputs(taskId, data, IdentityProviders.of(user, groups)).orElseThrow(UserTaskInstanceNotFoundException::new);
}

@GET
Expand All @@ -137,7 +138,7 @@ public Comment addComment(
CommentInfo commentInfo) {
Comment comment = new Comment(null, user);
comment.setContent(commentInfo.getComment());
return userTaskService.addComment(taskId, comment, IdentityProviders.of(user, groups)).orElseThrow(NotFoundException::new);
return userTaskService.addComment(taskId, comment, IdentityProviders.of(user, groups)).orElseThrow(UserTaskInstanceNotFoundException::new);
}

@GET
Expand All @@ -149,7 +150,7 @@ public Comment getComment(
@QueryParam("user") String user,
@QueryParam("group") List<String> groups) {
return userTaskService.getComment(taskId, commentId, IdentityProviders.of(user, groups))
.orElseThrow(() -> new NotFoundException("Comment " + commentId + " not found"));
.orElseThrow(() -> new UserTaskInstanceNotFoundException("Comment " + commentId + " not found"));
}

@PUT
Expand All @@ -165,7 +166,7 @@ public Comment updateComment(
Comment comment = new Comment(commentId, user);
comment.setContent(commentInfo.getComment());
return userTaskService.updateComment(taskId, comment, IdentityProviders.of(user, groups))
.orElseThrow(NotFoundException::new);
.orElseThrow(UserTaskInstanceNotFoundException::new);
}

@DELETE
Expand All @@ -176,7 +177,7 @@ public Comment deleteComment(
@QueryParam("user") String user,
@QueryParam("group") List<String> groups) {
return userTaskService.removeComment(taskId, commentId, IdentityProviders.of(user, groups))
.orElseThrow(NotFoundException::new);
.orElseThrow(UserTaskInstanceNotFoundException::new);
}

@GET
Expand All @@ -202,7 +203,7 @@ public Attachment addAttachment(
attachment.setName(attachmentInfo.getName());
attachment.setContent(attachmentInfo.getUri());
return userTaskService.addAttachment(taskId, attachment, IdentityProviders.of(user, groups))
.orElseThrow(NotFoundException::new);
.orElseThrow(UserTaskInstanceNotFoundException::new);
}

@PUT
Expand All @@ -219,7 +220,7 @@ public Attachment updateAttachment(
attachment.setName(attachmentInfo.getName());
attachment.setContent(attachmentInfo.getUri());
return userTaskService.updateAttachment(taskId, attachment, IdentityProviders.of(user, groups))
.orElseThrow(NotFoundException::new);
.orElseThrow(UserTaskInstanceNotFoundException::new);
}

@DELETE
Expand All @@ -230,7 +231,7 @@ public Attachment deleteAttachment(
@QueryParam("user") String user,
@QueryParam("group") List<String> groups) {
return userTaskService.removeAttachment(taskId, attachmentId, IdentityProviders.of(user, groups))
.orElseThrow(NotFoundException::new);
.orElseThrow(UserTaskInstanceNotFoundException::new);
}

@GET
Expand All @@ -242,7 +243,7 @@ public Attachment getAttachment(
@QueryParam("user") String user,
@QueryParam("group") List<String> groups) {
return userTaskService.getAttachment(taskId, attachmentId, IdentityProviders.of(user, groups))
.orElseThrow(() -> new NotFoundException("Attachment " + attachmentId + " not found"));
.orElseThrow(() -> new UserTaskInstanceNotFoundException("Attachment " + attachmentId + " not found"));
}

}

0 comments on commit f83306b

Please sign in to comment.