Add an exception mapper 60/76760/2
authorStephen Kitt <skitt@redhat.com>
Mon, 8 Oct 2018 15:38:51 +0000 (17:38 +0200)
committerMichael Vorburger <vorburger@redhat.com>
Mon, 8 Oct 2018 16:20:56 +0000 (18:20 +0200)
Change-Id: I16a73b43627e09b4bbebbf6fbb467e572ad61d30
Signed-off-by: Stephen Kitt <skitt@redhat.com>
Signed-off-by: Michael Vorburger <vorburger@redhat.com>
northbound-api/src/main/java/org/opendaylight/neutron/northbound/api/NeutronNorthboundRSApplication.java

index e5b4096e5117bc125d65974362d82d30e8e8fa4c..0fdd9b77422230ab5711b44fdee8dce38b44bb37 100644 (file)
@@ -16,7 +16,11 @@ import java.util.Set;
 import javax.inject.Inject;
 import javax.inject.Singleton;
 import javax.ws.rs.core.Application;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
 import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * This class is an instance of javax.ws.rs.core.Application and is used to return the classes
@@ -25,6 +29,9 @@ import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider;
  */
 @Singleton
 public final class NeutronNorthboundRSApplication extends Application {
+
+    private static final Logger LOG = LoggerFactory.getLogger(NeutronNorthboundRSApplication.class);
+
     private static final int HASHMAP_SIZE = 3;
 
     private final NeutronNetworksNorthbound neutronNetworksNorthbound;
@@ -167,6 +174,7 @@ public final class NeutronNorthboundRSApplication extends Application {
                 .add(neutronTrunksNorthbound)
                 .add(neutronTapServiceNorthbound)
                 .add(neutronTapFlowNorthbound)
+                .add(new LoggingExceptionMapper())
                 .build();
     }
 
@@ -189,4 +197,19 @@ public final class NeutronNorthboundRSApplication extends Application {
 
         return moxyJsonProvider;
     }
+
+    // do not inline this as a lambda; for some (strange) reason, the HK2 DI thing used by Jersey (v2.25.1) does
+    // not like it: "WARNING: The following warnings have been detected: WARNING: Unknown HK2 failure detected:
+    //  javax.ws.rs.ProcessingException: Could not find exception type for given ExceptionMapper class:
+    //  class org.opendaylight.neutron.northbound.api.NeutronNorthboundRSApplication$$Lambda$157/5987161."
+    public static class LoggingExceptionMapper implements ExceptionMapper<Exception> {
+        @Override
+        public Response toResponse(Exception exception) {
+            LOG.error("Error processing response", exception);
+            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
+            // In order to return the exception in the Response, it would need mapping.
+            // This could be brittle, and potentially insecure to provide internal exception externally.
+            // We thus intentionally chose to only return a generic 500 with details (only) in the log.
+        }
+    }
 }