Skip to content

Commit

Permalink
feat: bugfix and improved exception mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
JordenReuter committed Apr 23, 2024
1 parent 6663d23 commit c60f85b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import jakarta.validation.ConstraintViolationException;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Response;

import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.jboss.resteasy.reactive.ClientWebApplicationException;
import org.jboss.resteasy.reactive.RestResponse;
import org.jboss.resteasy.reactive.server.ServerExceptionMapper;
import org.tkit.onecx.announcement.bff.rs.mappers.AnnouncementMapper;
Expand Down Expand Up @@ -137,7 +137,7 @@ public RestResponse<ProblemDetailResponseDTO> constraint(ConstraintViolationExce
}

@ServerExceptionMapper
public Response restException(WebApplicationException ex) {
return Response.status(ex.getResponse().getStatus()).build();
public Response restException(ClientWebApplicationException ex) {
return exceptionMapper.clientException(ex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ default ActiveAnnouncementsPageResultDTO filterAndSort(ActiveAnnouncementsPageRe
if (activeAnnouncementsSearchCriteriaDTO.getPageSize() != 100) {
endIndex = startIndex + activeAnnouncementsSearchCriteriaDTO.getPageSize();
}
if (!(startIndex <= pageResult.getStream().size())) {
if (startIndex <= pageResult.getStream().size()) {
pageResult.setStream(
pageResult.getStream().subList(startIndex, Math.min(endIndex, pageResult.getStream().size())));
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.tkit.onecx.announcement.bff.rs.mappers;

import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;

import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -9,6 +11,7 @@
import jakarta.validation.Path;
import jakarta.ws.rs.core.Response;

import org.jboss.resteasy.reactive.ClientWebApplicationException;
import org.jboss.resteasy.reactive.RestResponse;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
Expand All @@ -17,6 +20,7 @@
import gen.org.tkit.onecx.announcement.bff.rs.internal.model.ProblemDetailInvalidParamDTO;
import gen.org.tkit.onecx.announcement.bff.rs.internal.model.ProblemDetailParamDTO;
import gen.org.tkit.onecx.announcement.bff.rs.internal.model.ProblemDetailResponseDTO;
import gen.org.tkit.onecx.announcement.client.model.ProblemDetailResponse;

@Mapper(uses = { OffsetDateTimeMapper.class })
public interface ExceptionMapper {
Expand All @@ -27,6 +31,24 @@ default RestResponse<ProblemDetailResponseDTO> constraint(ConstraintViolationExc
return RestResponse.status(Response.Status.BAD_REQUEST, dto);
}

default Response clientException(ClientWebApplicationException ex) {
if (ex.getResponse().getStatus() == 500) {
return Response.status(400).build();
} else {
if (ex.getResponse().getMediaType() != null
&& ex.getResponse().getMediaType().toString().equals(APPLICATION_JSON)) {
return Response.status(ex.getResponse().getStatus())
.entity(map(ex.getResponse().readEntity(ProblemDetailResponse.class))).build();
} else {
return Response.status(ex.getResponse().getStatus()).build();
}
}
}

@Mapping(target = "removeParamsItem", ignore = true)
@Mapping(target = "removeInvalidParamsItem", ignore = true)
ProblemDetailResponseDTO map(ProblemDetailResponse problemDetailResponse);

@Mapping(target = "removeParamsItem", ignore = true)
@Mapping(target = "params", ignore = true)
@Mapping(target = "invalidParams", ignore = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,8 @@ void searchActiveAnnouncements_shouldReturnAnnouncementPageResults() {

// Assertions
Assertions.assertNotNull(response);
Assertions.assertEquals(3,response.getStream().size());
Assertions.assertEquals( AnnouncementPriorityType.IMPORTANT, response.getStream().get(0).getPriority());
Assertions.assertEquals(3, response.getStream().size());
Assertions.assertEquals(AnnouncementPriorityType.IMPORTANT, response.getStream().get(0).getPriority());
Assertions.assertNull(response.getStream().get(0).getWorkspaceName());
Assertions.assertEquals(AnnouncementPriorityType.IMPORTANT, response.getStream().get(1).getPriority());
Assertions.assertEquals("w1", response.getStream().get(1).getWorkspaceName());
Expand Down

0 comments on commit c60f85b

Please sign in to comment.