package sonia.scm.api.rest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import sonia.scm.ExceptionWithContext; import sonia.scm.api.v2.resources.ExceptionWithContextToErrorDtoMapper; import sonia.scm.web.VndMediaType; import javax.ws.rs.core.Response; import javax.ws.rs.ext.ExceptionMapper; public class ContextualExceptionMapper implements ExceptionMapper { private static final Logger logger = LoggerFactory.getLogger(ContextualExceptionMapper.class); private final ExceptionWithContextToErrorDtoMapper mapper; private final Response.Status status; private final Class type; public ContextualExceptionMapper(Class type, Response.Status status, ExceptionWithContextToErrorDtoMapper mapper) { this.mapper = mapper; this.type = type; this.status = status; } @Override public Response toResponse(E exception) { logger.debug("map {} to status code {}", type.getSimpleName(), status.getStatusCode()); return Response.status(status) .entity(mapper.map(exception)) .type(VndMediaType.ERROR_TYPE) .build(); } }