Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AAE-30844 Improve error handling for ConversionFailedException #1680

Merged
merged 3 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
import jakarta.servlet.http.HttpServletResponse;
import org.activiti.api.model.shared.model.ActivitiErrorMessage;
import org.activiti.api.runtime.model.impl.ActivitiErrorMessageImpl;
import org.activiti.cloud.common.error.attributes.ErrorAttributesMessageSanitizer;
import org.activiti.core.common.spring.security.policies.ActivitiForbiddenException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.hateoas.EntityModel;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand All @@ -30,6 +34,8 @@
@RestControllerAdvice
public class CommonExceptionHandlerQuery {

private static final Logger LOGGER = LoggerFactory.getLogger(CommonExceptionHandlerQuery.class);

// TODO: 12/04/2019 this exception handler should be moved to activiti-cloud-service-common
@ExceptionHandler(ActivitiForbiddenException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
Expand All @@ -41,7 +47,7 @@ public EntityModel<ActivitiErrorMessage> handleAppException(
return EntityModel.of(new ActivitiErrorMessageImpl(HttpStatus.FORBIDDEN.value(), ex.getMessage()));
}

@ExceptionHandler(IllegalStateException.class)
@ExceptionHandler({ IllegalStateException.class })
@ResponseStatus(HttpStatus.BAD_REQUEST)
public EntityModel<ActivitiErrorMessage> handleAppException(
IllegalStateException ex,
Expand All @@ -51,6 +57,22 @@ public EntityModel<ActivitiErrorMessage> handleAppException(
return EntityModel.of(new ActivitiErrorMessageImpl(HttpStatus.BAD_REQUEST.value(), ex.getMessage()));
}

@ExceptionHandler({ ConversionFailedException.class })
@ResponseStatus(HttpStatus.BAD_REQUEST)
public EntityModel<ActivitiErrorMessage> handleAppException(
ConversionFailedException ex,
HttpServletResponse response
) {
LOGGER.error(ex.getMessage(), ex);
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
return EntityModel.of(
new ActivitiErrorMessageImpl(
HttpStatus.BAD_REQUEST.value(),
ErrorAttributesMessageSanitizer.ERROR_NOT_DISCLOSED_MESSAGE
)
);
}

@ExceptionHandler(EntityNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public EntityModel<ActivitiErrorMessage> handleAppException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@

import com.querydsl.core.types.Predicate;
import jakarta.persistence.EntityManagerFactory;
import java.util.*;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.activiti.api.runtime.conf.impl.CommonModelAutoConfiguration;
import org.activiti.api.runtime.shared.security.SecurityManager;
import org.activiti.cloud.alfresco.config.AlfrescoWebAutoConfiguration;
import org.activiti.cloud.common.error.attributes.ErrorAttributesMessageSanitizer;
import org.activiti.cloud.conf.QueryRestWebMvcAutoConfiguration;
import org.activiti.cloud.services.query.app.repository.EntityFinder;
import org.activiti.cloud.services.query.app.repository.ProcessDefinitionRepository;
Expand All @@ -52,12 +55,17 @@
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.data.domain.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.config.EnableSpringDataWebSupport;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

@WebMvcTest(ProcessInstanceController.class)
@Import(
Expand Down Expand Up @@ -200,4 +208,16 @@ void shouldReturnProcessInstanceById() throws Exception {
.andExpect(jsonPath("$.entry.id").value(processInstanceEntity.getId()))
.andExpect(jsonPath("$.entry.status").value(processInstanceEntity.getStatus().name()));
}

@Test
void should_returnBadRequestError_when_invalidProcessInstanceEnum() throws Exception {
MvcResult result = mockMvc
.perform(get("/v1/process-instances?status=ASSIGNED").accept(MediaType.APPLICATION_JSON))
//then
.andExpect(status().isBadRequest())
.andReturn();

assertThat(result.getResponse().getContentAsString())
.contains(ErrorAttributesMessageSanitizer.ERROR_NOT_DISCLOSED_MESSAGE);
}
}
Loading