users@jersey.java.net

[Jersey] Change to allow any Exception to be mapped

From: <chadnickbok_at_gmail.com>
Date: Wed, 1 Aug 2012 22:43:07 +0000 (GMT)

This is my first post to these lists, so I apologize if I'm sending
this to the wrong one.

I recently ran into an issue trying to map NotFoundException (and
friends) to my own custom exception mapper. The goal of this exercise
was to always return JSON-Formatted content, even on exceptions, to
allow for easier processing client-side.

This is also important for logging purposes - For example, taking a
custom action when a user requests a URI that isn't currently mapped,
and returning something other than the default 404.

I ended up making the following change to support my desired behavior,
but I'd love to know if theres a more standard way of achieving this.

---
a/jersey/jersey-server/src/main/java/com/sun/jersey/spi/container/Conta
inerResponse.java
+++
b/jersey/jersey-server/src/main/java/com/sun/jersey/spi/container/Conta
inerResponse.java
@@ -417,12 +417,8 @@ public class ContainerResponse implements
HttpResponseContext {
      * @param e the web application exception.
      */
     public void mapWebApplicationException(WebApplicationException e)
{
-	 if (e.getResponse().getEntity() != null) {
+	 if (!mapException(e)) {
	     onException(e, e.getResponse(), false);
-	 } else {
-	     if (!mapException(e)) {
-		 onException(e, e.getResponse(), false);
-	     }
	 }
     }