Add generic exception mapper 63/8863/3
authormrajvaid <mrajvaid@cisco.com>
Wed, 9 Jul 2014 15:44:49 +0000 (08:44 -0700)
committerMeena Rajvaidya <mrajvaid@cisco.com>
Wed, 9 Jul 2014 19:21:16 +0000 (19:21 +0000)
Change-Id: Ia3fbf4bcea6095e5c0eb6e56e8c886b38e66cd65
Signed-off-by: mrajvaid <mrajvaid@cisco.com>
opendaylight/northbound/commons/src/main/java/org/opendaylight/controller/northbound/commons/NorthboundApplication.java
opendaylight/northbound/commons/src/main/java/org/opendaylight/controller/northbound/commons/exception/GenericExceptionMapper.java [new file with mode: 0644]

index 87f51364ba1c5b59f3b9326a518a07c99c72129c..cf48729113ce7b1304f2a0f7b3f2e4a7c0e05dfc 100644 (file)
@@ -20,6 +20,7 @@ import javax.xml.bind.JAXBException;
 import javax.xml.bind.annotation.XmlRootElement;
 
 import org.opendaylight.controller.northbound.bundlescanner.IBundleScanService;
+import org.opendaylight.controller.northbound.commons.exception.GenericExceptionMapper;
 import org.opendaylight.controller.northbound.commons.query.QueryContextProvider;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
@@ -60,6 +61,7 @@ public class NorthboundApplication extends Application {
         _singletons.add(getJsonProvider());
         _singletons.add(new JacksonJsonProcessingExceptionMapper());
         _singletons.add(new QueryContextProvider());
+        _singletons.add(new GenericExceptionMapper());
     }
 
     ////////////////////////////////////////////////////////////////
diff --git a/opendaylight/northbound/commons/src/main/java/org/opendaylight/controller/northbound/commons/exception/GenericExceptionMapper.java b/opendaylight/northbound/commons/src/main/java/org/opendaylight/controller/northbound/commons/exception/GenericExceptionMapper.java
new file mode 100644 (file)
index 0000000..d2bbfea
--- /dev/null
@@ -0,0 +1,24 @@
+package org.opendaylight.controller.northbound.commons.exception;
+
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+
+@Provider
+public class GenericExceptionMapper implements ExceptionMapper<Exception> {
+
+    @Override
+    public Response toResponse(Exception exception) {
+        //check if WebApplicationException and reuse status code
+        if (exception instanceof WebApplicationException) {
+            WebApplicationException ex = (WebApplicationException) exception;
+            return Response.status(ex.getResponse().getStatus()).
+                    entity(ex.getResponse().getEntity()).build();
+        }
+        // throw 500 for all other errors
+        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).
+                entity(exception.getMessage()).build();
+    }
+
+}