mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 07:25:44 +01:00
Add fallback exception mapper
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package sonia.scm.api;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.slf4j.MDC;
|
||||
import sonia.scm.api.v2.resources.ErrorDto;
|
||||
import sonia.scm.api.v2.resources.ExceptionWithContextToErrorDtoMapper;
|
||||
import sonia.scm.web.VndMediaType;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.ext.ExceptionMapper;
|
||||
import javax.ws.rs.ext.Provider;
|
||||
import java.util.Collections;
|
||||
|
||||
@Provider
|
||||
public class FallbackExceptionMapper implements ExceptionMapper<Exception> {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(FallbackExceptionMapper.class);
|
||||
|
||||
private static final String ERROR_CODE = "CmR8GCJb31";
|
||||
|
||||
private final ExceptionWithContextToErrorDtoMapper mapper;
|
||||
|
||||
@Inject
|
||||
public FallbackExceptionMapper(ExceptionWithContextToErrorDtoMapper mapper) {
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response toResponse(Exception exception) {
|
||||
logger.debug("map {} to status code 500", exception);
|
||||
ErrorDto errorDto = new ErrorDto();
|
||||
errorDto.setMessage("internal server error");
|
||||
errorDto.setContext(Collections.emptyList());
|
||||
errorDto.setErrorCode(ERROR_CODE);
|
||||
errorDto.setTransactionId(MDC.get("transaction_id"));
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
|
||||
.entity(errorDto)
|
||||
.type(VndMediaType.ERROR_TYPE)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ public class ContextualExceptionMapper<E extends ExceptionWithContext> implement
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ContextualExceptionMapper.class);
|
||||
|
||||
private ExceptionWithContextToErrorDtoMapper mapper;
|
||||
private final ExceptionWithContextToErrorDtoMapper mapper;
|
||||
|
||||
private final Response.Status status;
|
||||
private final Class<E> type;
|
||||
|
||||
@@ -17,7 +17,7 @@ public class ErrorDto {
|
||||
private List<ContextEntry> context;
|
||||
private String message;
|
||||
|
||||
@XmlElement(name = "violation")
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
@XmlElementWrapper(name = "violations")
|
||||
private List<ConstraintViolationDto> violations;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user