Skip to content

Commit

Permalink
feat: added improved exception handler
Browse files Browse the repository at this point in the history
  • Loading branch information
JordenReuter committed Apr 23, 2024
1 parent e6830e1 commit 45eaad7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import jakarta.validation.ConstraintViolationException;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.HttpHeaders;
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.quarkus.permission.service.PermissionResponse;
Expand Down Expand Up @@ -67,16 +67,6 @@ public Response getPermissions(GetPermissionsRequestDTO getPermissionsRequestDTO
return Response.status(Response.Status.OK).entity(mapper.map(rawPermission)).build();
}

@ServerExceptionMapper
public RestResponse<ProblemDetailResponseDTO> constraint(ConstraintViolationException ex) {
return exceptionMapper.constraint(ex);
}

@ServerExceptionMapper
public Response restException(WebApplicationException ex) {
return Response.status(ex.getResponse().getStatus()).build();
}

public PermissionResponse getPermissions(String productName, String appName, String token, String keySeparator) {
if (!config.permissions().cachingEnabled()) {
return getPermissionsLocal(productName, appName, token, keySeparator);
Expand All @@ -101,4 +91,14 @@ public PermissionResponse getPermissionsLocal(String productName, String appName
return PermissionResponse.create(result);
}
}

@ServerExceptionMapper
public RestResponse<ProblemDetailResponseDTO> constraint(ConstraintViolationException ex) {
return exceptionMapper.constraint(ex);
}

@ServerExceptionMapper
public Response restException(ClientWebApplicationException ex) {
return exceptionMapper.clientException(ex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
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.server.ServerExceptionMapper;
import org.tkit.onecx.shell.bff.rs.mappers.ExceptionMapper;
import org.tkit.onecx.shell.bff.rs.mappers.UserProfileMapper;
import org.tkit.quarkus.log.cdi.LogService;

Expand All @@ -27,6 +28,9 @@ public class UserProfileRestController implements UserProfileApiService {
@Inject
UserProfileMapper mapper;

@Inject
ExceptionMapper exceptionMapper;

@Override
public Response getUserProfile() {
try (Response response = userProfileClient.getUserProfile()) {
Expand All @@ -36,7 +40,7 @@ public Response getUserProfile() {
}

@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 @@ -14,6 +14,7 @@
import jakarta.ws.rs.core.UriInfo;

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.shell.bff.rs.RemoteComponentMockConfig;
Expand Down Expand Up @@ -198,7 +199,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
@@ -1,5 +1,7 @@
package org.tkit.onecx.shell.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 @@ -28,6 +31,20 @@ 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);
Expand Down

0 comments on commit 45eaad7

Please sign in to comment.