ONOS-3369: log an exception on REST server errors

Change-Id: I407ed7576ff79b5781d2a688c78367266643aaea
This commit is contained in:
Ray Milkey 2015-11-17 09:23:24 -08:00 committed by Gerrit Code Review
parent 864333ae70
commit 8fba1c81d0
2 changed files with 13 additions and 0 deletions

View File

@ -28,6 +28,11 @@ import static com.google.common.base.Strings.isNullOrEmpty;
*/ */
public abstract class AbstractMapper<E extends Throwable> implements ExceptionMapper<E> { 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. * Returns the response status to be given when the exception occurs.
* *
@ -37,6 +42,7 @@ public abstract class AbstractMapper<E extends Throwable> implements ExceptionMa
@Override @Override
public Response toResponse(E exception) { public Response toResponse(E exception) {
error = exception;
return response(responseStatus(), exception).build(); 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, protected Response.ResponseBuilder response(Response.Status status,
Throwable exception) { Throwable exception) {
error = exception;
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
String message = messageFrom(exception); String message = messageFrom(exception);
ObjectNode result = mapper.createObjectNode() ObjectNode result = mapper.createObjectNode()

View File

@ -18,13 +18,19 @@ package org.onosproject.rest.exceptions;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider; 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. * Mapper for service not found exceptions to the INTERNAL_SERVER_ERROR response code.
*/ */
@Provider @Provider
public class ServerErrorMapper extends AbstractMapper<RuntimeException> { public class ServerErrorMapper extends AbstractMapper<RuntimeException> {
private static final Logger log = getLogger(ServerErrorMapper.class);
@Override @Override
protected Response.Status responseStatus() { protected Response.Status responseStatus() {
log.warn("Unhandled REST exception", error);
return Response.Status.INTERNAL_SERVER_ERROR; return Response.Status.INTERNAL_SERVER_ERROR;
} }
} }