Move adsal into its own subdirectory.
[controller.git] / opendaylight / adsal / northbound / commons / src / main / java / org / opendaylight / controller / northbound / commons / exception / GenericExceptionMapper.java
diff --git a/opendaylight/adsal/northbound/commons/src/main/java/org/opendaylight/controller/northbound/commons/exception/GenericExceptionMapper.java b/opendaylight/adsal/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();
+    }
+
+}