Fixed exception mapper
This commit is contained in:
parent
e1af22962e
commit
1635ac0bd9
|
@ -1,14 +1,14 @@
|
|||
package org.gcube.service.helloworld.rest;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import jakarta.ws.rs.BadRequestException;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.NotFoundException;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.WebApplicationException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@Path("guest")
|
||||
public class ExcludeAuthorizationService {
|
||||
|
||||
|
@ -33,8 +33,15 @@ public class ExcludeAuthorizationService {
|
|||
}
|
||||
|
||||
@GET
|
||||
@Path("exception")
|
||||
public String exc() throws WebApplicationException {
|
||||
@Path("bad-request")
|
||||
public String badRequest() throws WebApplicationException {
|
||||
throw new BadRequestException();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("not-found")
|
||||
public String notFound() throws WebApplicationException {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
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.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import jakarta.ws.rs.WebApplicationException;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
|
@ -16,40 +20,40 @@ import jakarta.ws.rs.ext.Provider;
|
|||
@Provider
|
||||
public class ServiceExceptionMapper implements ExceptionMapper<Exception> {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(ServiceExceptionMapper.class);
|
||||
|
||||
@Override
|
||||
public Response toResponse(Exception exception) {
|
||||
|
||||
Status status = Status.INTERNAL_SERVER_ERROR;
|
||||
String exceptionMessage = exception.getMessage();
|
||||
|
||||
ResponseBean responseBean = null;
|
||||
|
||||
|
||||
try {
|
||||
if (exception.getCause() != null) {
|
||||
if(exception.getCause() != null) {
|
||||
exceptionMessage = exception.getCause().getMessage();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
} catch(Exception e) {
|
||||
exceptionMessage = exception.getMessage();
|
||||
}
|
||||
|
||||
MediaType mediaType = MediaType.TEXT_PLAIN_TYPE;
|
||||
|
||||
if (WebApplicationException.class.isAssignableFrom(exception.getClass())) {
|
||||
|
||||
if(WebApplicationException.class.isAssignableFrom(exception.getClass())) {
|
||||
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());
|
||||
}
|
||||
|
||||
if (responseBean == null) {
|
||||
responseBean = new ResponseBean();
|
||||
}
|
||||
|
||||
|
||||
ResponseBean responseBean = new ResponseBean();
|
||||
responseBean.setSuccess(false);
|
||||
responseBean.setMessage(exceptionMessage);
|
||||
|
||||
return Response.status(status).entity(responseBean).type(mediaType).build();
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
try {
|
||||
exceptionMessage = objectMapper.writeValueAsString(responseBean);
|
||||
} catch (JsonProcessingException e) {
|
||||
logger.warn("Error while serializing ResponseBean", e);
|
||||
}
|
||||
|
||||
return Response.status(status).entity(exceptionMessage).type(mediaType).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue