io.rtr.alchemy.service.exceptions.RuntimeExceptionMapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of alchemy-service Show documentation
Show all versions of alchemy-service Show documentation
REST service for hosting Alchemy as a service
The newest version!
package io.rtr.alchemy.service.exceptions;
import com.google.common.collect.Maps;
import java.util.Map;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
/** Maps common runtime exceptions to their respective HTTP status codes */
public class RuntimeExceptionMapper implements ExceptionMapper {
private static final Map, Response.Status> EXCEPTION_MAPPINGS;
static {
EXCEPTION_MAPPINGS = Maps.newLinkedHashMap();
// 400 - Bad Request
register(
Response.Status.BAD_REQUEST,
IllegalArgumentException.class,
NullPointerException.class);
}
private static void register(Response.Status status, Class>... types) {
for (Class> type : types) {
EXCEPTION_MAPPINGS.put(type, status);
}
}
@Override
public Response toResponse(RuntimeException e) {
if (e instanceof WebApplicationException) {
return ((WebApplicationException) e).getResponse();
}
final Response.Status status = EXCEPTION_MAPPINGS.get(e.getClass());
if (status == null) {
throw new WebApplicationException(e);
}
return Response.status(status)
.type(MediaType.APPLICATION_JSON)
.entity(e.getMessage())
.build();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy