Fixed exception mapper

This commit is contained in:
Luca Frosini 2024-05-24 17:10:42 +02:00
parent e1af22962e
commit 1635ac0bd9
2 changed files with 34 additions and 23 deletions

View File

@ -1,14 +1,14 @@
package org.gcube.service.helloworld.rest; package org.gcube.service.helloworld.rest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jakarta.ws.rs.BadRequestException; import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.GET; import jakarta.ws.rs.GET;
import jakarta.ws.rs.NotFoundException; import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.Path; import jakarta.ws.rs.Path;
import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.WebApplicationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Path("guest") @Path("guest")
public class ExcludeAuthorizationService { public class ExcludeAuthorizationService {
@ -33,8 +33,15 @@ public class ExcludeAuthorizationService {
} }
@GET @GET
@Path("exception") @Path("bad-request")
public String exc() throws WebApplicationException { public String badRequest() throws WebApplicationException {
throw new BadRequestException(); throw new BadRequestException();
} }
@GET
@Path("not-found")
public String notFound() throws WebApplicationException {
throw new NotFoundException();
}
} }

View File

@ -1,7 +1,11 @@
package org.gcube.service.helloworld.rest; package org.gcube.service.helloworld.rest;
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.service.helloworld.beans.ResponseBean; import org.gcube.service.helloworld.beans.ResponseBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.MediaType;
@ -16,40 +20,40 @@ import jakarta.ws.rs.ext.Provider;
@Provider @Provider
public class ServiceExceptionMapper implements ExceptionMapper<Exception> { public class ServiceExceptionMapper implements ExceptionMapper<Exception> {
private final Logger logger = LoggerFactory.getLogger(ServiceExceptionMapper.class);
@Override @Override
public Response toResponse(Exception exception) { public Response toResponse(Exception exception) {
Status status = Status.INTERNAL_SERVER_ERROR; Status status = Status.INTERNAL_SERVER_ERROR;
String exceptionMessage = exception.getMessage(); String exceptionMessage = exception.getMessage();
ResponseBean responseBean = null;
try { try {
if (exception.getCause() != null) { if(exception.getCause() != null) {
exceptionMessage = exception.getCause().getMessage(); exceptionMessage = exception.getCause().getMessage();
} }
} catch (Exception e) { } catch(Exception e) {
exceptionMessage = exception.getMessage(); exceptionMessage = exception.getMessage();
} }
MediaType mediaType = MediaType.TEXT_PLAIN_TYPE; MediaType mediaType = MediaType.TEXT_PLAIN_TYPE;
if (WebApplicationException.class.isAssignableFrom(exception.getClass())) { if(WebApplicationException.class.isAssignableFrom(exception.getClass())) {
Response gotResponse = ((WebApplicationException) exception).getResponse(); Response gotResponse = ((WebApplicationException) exception).getResponse();
Object entity = gotResponse.getEntity();
if (entity != null && ResponseBean.class.isAssignableFrom(entity.getClass())) {
responseBean = (ResponseBean) entity;
}
status = Status.fromStatusCode(gotResponse.getStatusInfo().getStatusCode()); status = Status.fromStatusCode(gotResponse.getStatusInfo().getStatusCode());
} }
if (responseBean == null) {
responseBean = new ResponseBean(); ResponseBean responseBean = new ResponseBean();
}
responseBean.setSuccess(false); responseBean.setSuccess(false);
responseBean.setMessage(exceptionMessage); responseBean.setMessage(exceptionMessage);
ObjectMapper objectMapper = new ObjectMapper();
return Response.status(status).entity(responseBean).type(mediaType).build(); try {
exceptionMessage = objectMapper.writeValueAsString(responseBean);
} catch (JsonProcessingException e) {
logger.warn("Error while serializing ResponseBean", e);
}
return Response.status(status).entity(exceptionMessage).type(mediaType).build();
} }
} }