Return HTTP 400 code with useful message when a ParamException occurs from API call.

This commit is contained in:
catbref 2019-04-04 12:30:28 +01:00
parent 315ebff61d
commit 97142fdde8

View File

@ -10,6 +10,7 @@ import javax.ws.rs.ext.Provider;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.glassfish.jersey.server.ParamException;
@Provider
public class ApiExceptionMapper implements ExceptionMapper<RuntimeException> {
@ -26,6 +27,11 @@ public class ApiExceptionMapper implements ExceptionMapper<RuntimeException> {
public Response toResponse(RuntimeException e) {
LOGGER.info(String.format("Exception %s during API call: %s", e.getClass().getCanonicalName(), request.getRequestURI()));
if (e instanceof ParamException) {
ParamException pe = (ParamException) e;
return Response.status(Response.Status.BAD_REQUEST).entity("Bad parameter \"" + pe.getParameterName() + "\"").build();
}
if (e instanceof WebApplicationException)
return ((WebApplicationException) e).getResponse();