package odata.jpa;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import odata.jpa.beans.ODataExceptionBean;

/**
 * I don't like the standard implementation: it gives printStackTrace() for
 * manually launched exceptions (which is useless) and not for uncatched
 * exceptions (which is very useful).
 * 
 * Response is returned in JSON. A better implementation would produce XML or
 * JSON according to accepted headers.
 */
@Provider
public class DefaultExceptionHandler implements ExceptionMapper<Exception> {

	@Override
	public Response toResponse(Exception e) {

		int status;
		String message;
		if (e instanceof WebApplicationException) {
			// don't print stack trace
			// Unluckily, someone prints it anyway.
			status = ((WebApplicationException) e).getResponse().getStatus();

			if (status == Status.INTERNAL_SERVER_ERROR.getStatusCode())
				e.printStackTrace();

			message = Status.fromStatusCode(status).getReasonPhrase();
			if (e.getMessage() != null)
				message += " - " + e.getMessage();
		} else {
			e.printStackTrace();
			status = Status.INTERNAL_SERVER_ERROR.getStatusCode();
			message = Status.INTERNAL_SERVER_ERROR.getReasonPhrase();
		}

		ODataExceptionBean errorMessage = new ODataExceptionBean(status, message);

		return Response.status(status).entity(errorMessage).type(MediaType.APPLICATION_JSON).build();
	}
}
@@ -13,16 +13,68 @@
 a.jpa;%0A%0A
+import javax.ejb.EJBTransactionRolledbackException;%0A
 import j
@@ -293,16 +293,74 @@
 vider;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 import o
@@ -804,93 +804,459 @@
 %7B%0A%0A%09
-@Override%0A%09public Response toResponse(Exception e) %7B%0A%0A%09%09int status;%0A%09%09String message;
+public static final Logger LOG = LoggerFactory.getLogger(DefaultExceptionHandler.class);%0A%0A%09@Override%0A%09public Response toResponse(Exception e) %7B%0A%0A%09%09int status;%0A%09%09String message;%0A%09%09%0A%09%09//EJBTransactionRolledbackException usually unuseful%0A%09%09if (e instanceof EJBTransactionRolledbackException && e.getCause() != null && e.getCause().getCause() != null%0A%09%09%09%09&& e.getCause().getCause() instanceof Exception) %7B%0A%09%09%09e = (Exception)e.getCause().getCause();%0A%09%09%7D%0A%09%09
 %0A%09%09i
@@ -1327,16 +1327,33 @@
 ck trace
+, if client issue
 %0A%09%09%09// U
@@ -1370,11 +1370,16 @@
 some
-one
+ library
  pri
@@ -1530,34 +1530,52 @@
 ())%0A%09%09%09%09
-e.printStackTrace(
+LOG.error(%22Internal server error%22, e
 );%0A%0A%09%09%09m
@@ -1702,16 +1702,20 @@
 sage();%0A
+%09%09%09%0A
 %09%09%7D else
@@ -1719,31 +1719,184 @@
 lse 
-%7B%0A%09%09%09e.printStackTrace(
+if (e instanceof IllegalArgumentException) %7B%0A%09%09%09status = Status.BAD_REQUEST.getStatusCode();%0A%09%09%09message = e.getMessage();%0A%09%09%09%0A%09%09%7D else %7B%0A%09%09%09LOG.error(%22Internal server error%22, e
 );%0A%09
@@ -2196,8 +2196,9 @@
 ();%0A%09%7D%0A%7D
+%0A

Update DefaultExceptionHandler.java