Ray Milkey
Committed by Gerrit Code Review

ONOS-3369: log an exception on REST server errors

Change-Id: I407ed7576ff79b5781d2a688c78367266643aaea
......@@ -29,6 +29,11 @@ import static com.google.common.base.Strings.isNullOrEmpty;
public abstract class AbstractMapper<E extends Throwable> implements ExceptionMapper<E> {
/**
* Holds the current exception for use in subclasses.
*/
protected Throwable error;
/**
* Returns the response status to be given when the exception occurs.
*
* @return response status
......@@ -37,6 +42,7 @@ public abstract class AbstractMapper<E extends Throwable> implements ExceptionMa
@Override
public Response toResponse(E exception) {
error = exception;
return response(responseStatus(), exception).build();
}
......@@ -50,6 +56,7 @@ public abstract class AbstractMapper<E extends Throwable> implements ExceptionMa
*/
protected Response.ResponseBuilder response(Response.Status status,
Throwable exception) {
error = exception;
ObjectMapper mapper = new ObjectMapper();
String message = messageFrom(exception);
ObjectNode result = mapper.createObjectNode()
......
......@@ -18,13 +18,19 @@ package org.onosproject.rest.exceptions;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
import org.slf4j.Logger;
import static org.slf4j.LoggerFactory.getLogger;
/**
* Mapper for service not found exceptions to the INTERNAL_SERVER_ERROR response code.
*/
@Provider
public class ServerErrorMapper extends AbstractMapper<RuntimeException> {
private static final Logger log = getLogger(ServerErrorMapper.class);
@Override
protected Response.Status responseStatus() {
log.warn("Unhandled REST exception", error);
return Response.Status.INTERNAL_SERVER_ERROR;
}
}
......