package milo.utils;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import java.util.logging.Level;
import java.util.logging.Logger;

@Provider
public class RestExceptionMapper implements ExceptionMapper<Throwable> {

	private static final Logger log = Logger.getLogger(RestExceptionMapper.class.getName());

	@Override
	public Response toResponse(Throwable exception) {
		log.log(Level.WARNING, "toResponse() caught exception", exception);
		return Response.status(getStatusCode(exception)).entity(getResponseBody(exception)).build();
	}

	private int getStatusCode(Throwable exception) {
		if (exception instanceof WebApplicationException) {
			return ((WebApplicationException) exception).getResponse().getStatus();
		}
		return Response.Status.INTERNAL_SERVER_ERROR.getStatusCode();
	}

	private String getResponseBody(Throwable exception) {
		if (exception instanceof WebApplicationException) {
			return ((WebApplicationException) exception).getResponse().getStatusInfo().toString();
		}
		return "Internal server error, check logs for more info.";
	}

}
@@ -31,37 +31,116 @@
 vax.
-ws.rs.WebApplicationException
+servlet.http.HttpServletRequest;%0Aimport javax.ws.rs.WebApplicationException;%0Aimport javax.ws.rs.core.Context
 ;%0Aim
@@ -394,16 +394,64 @@
 ble%3E %7B%0A%0A
+%09@Context%0A%09private HttpServletRequest request;%0A%0A
 %09private
@@ -625,20 +625,27 @@
 G, %22
-to
 Res
-ponse()
+tExceptionMapper
  cau
@@ -657,17 +657,69 @@
 xception
-%22
+ at %22 + getUrl() + %22 : %22%0A%09%09%09%09+ exception.getMessage()
 , except
@@ -1269,32 +1269,32 @@
 toString();%0A%09%09%7D%0A
-
 %09%09return %22Intern
@@ -1342,9 +1342,108 @@
 .%22;%0A%09%7D%0A%0A
+%09private String getUrl() %7B%0A%09%09return request != null ? request.getRequestURI() : %22Unknown URI%22;%0A%09%7D%0A%0A
 %7D

improve exception mapper log