Refactor global exception handler 50/138050/1 master
authorFiete Ostkamp <Fiete.Ostkamp@telekom.de>
Thu, 23 May 2024 14:14:13 +0000 (16:14 +0200)
committerFiete Ostkamp <Fiete.Ostkamp@telekom.de>
Thu, 23 May 2024 14:14:13 +0000 (16:14 +0200)
- extract similar code to methods
- return early
- bump snapshot version to 1.13.6

Issue-ID: AAI-3692
Change-Id: I2835a936c3bc60b8f14fd9014de46bdb2367a9fc
Signed-off-by: Fiete Ostkamp <Fiete.Ostkamp@telekom.de>
aai-resources/pom.xml
aai-resources/src/main/java/org/onap/aai/rest/ExceptionHandler.java
pom.xml

index abfd928..b69b93c 100644 (file)
@@ -28,7 +28,7 @@
     <parent>
         <groupId>org.onap.aai.resources</groupId>
         <artifactId>resources</artifactId>
-        <version>1.13.5-SNAPSHOT</version>
+        <version>1.13.6-SNAPSHOT</version>
     </parent>
     <properties>
         <java.version>1.8</java.version>
index ed57a7a..81c49b9 100644 (file)
@@ -45,6 +45,8 @@ import org.onap.aai.logging.ErrorLogHelper;
 @Provider
 public class ExceptionHandler implements ExceptionMapper<Exception> {
 
+    private static final String AAI_4007 = "AAI_4007";
+
     @Context
     private HttpServletRequest request;
 
@@ -57,70 +59,64 @@ public class ExceptionHandler implements ExceptionMapper<Exception> {
     @Override
     public Response toResponse(Exception exception) {
 
-        Response response = null;
-        ArrayList<String> templateVars = new ArrayList<String>();
-
         // the general case is that cxf will give us a WebApplicationException
         // with a linked exception
         if (exception instanceof WebApplicationException) {
-            WebApplicationException e = (WebApplicationException) exception;
-            if (e.getCause() != null) {
-                if (e.getCause() instanceof SAXParseException2) {
-                    templateVars.add("UnmarshalException");
-                    AAIException ex = new AAIException("AAI_4007", exception);
-                    response = Response
-                            .status(400).entity(ErrorLogHelper
-                                    .getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars))
-                            .build();
-                }
+            if (exception.getCause() instanceof SAXParseException2) {
+                return buildInputParsingErrorResponse(exception, "UnmarshalException");
+            } else {
+                return ((WebApplicationException)exception).getResponse();
             }
+
         } else if (exception instanceof JsonParseException) {
             // jackson does it differently so we get the direct JsonParseException
-            templateVars.add("JsonParseException");
-            AAIException ex = new AAIException("AAI_4007", exception);
-            response = Response.status(400)
-                    .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars))
-                    .build();
+            return buildInputParsingErrorResponse(exception);
         } else if (exception instanceof JsonMappingException) {
-            // jackson does it differently so we get the direct JsonParseException
-            templateVars.add("JsonMappingException");
-            AAIException ex = new AAIException("AAI_4007", exception);
-            response = Response.status(400)
-                    .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars))
-                    .build();
+            return buildInputParsingErrorResponse(exception);
+        } else {
+            return defaultException(exception);
         }
+    }
 
-        // it didn't get set above, we wrap a general fault here
-        if (response == null) {
+    private Response buildInputParsingErrorResponse(Exception exception) {
+        ArrayList<String> templateVars = new ArrayList<>();
+        templateVars.add(exception.getClass().getSimpleName());
+        return buildInputParsingErrorResponse(exception, templateVars);
+    }
 
-            Exception actual_e = exception;
-            if (exception instanceof WebApplicationException) {
-                WebApplicationException e = (WebApplicationException) exception;
-                response = e.getResponse();
-            } else {
-                templateVars.add(request.getMethod());
-                templateVars.add("unknown");
-                AAIException ex = new AAIException("AAI_4000", actual_e);
-                List<MediaType> mediaTypes = headers.getAcceptableMediaTypes();
-                int setError = 0;
-
-                for (MediaType mediaType : mediaTypes) {
-                    if (MediaType.APPLICATION_XML_TYPE.isCompatible(mediaType)) {
-                        response = Response
-                                .status(400).type(MediaType.APPLICATION_XML_TYPE).entity(ErrorLogHelper
-                                        .getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars))
-                                .build();
-                        setError = 1;
-                    }
-                }
-                if (setError == 0) {
-                    response = Response
-                            .status(400).type(MediaType.APPLICATION_JSON_TYPE).entity(ErrorLogHelper
-                                    .getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars))
-                            .build();
-                }
-            }
-        }
-        return response;
+    private Response buildInputParsingErrorResponse(Exception exception, String customExceptionName) {
+        ArrayList<String> templateVars = new ArrayList<>();
+        templateVars.add(customExceptionName);
+        return buildInputParsingErrorResponse(exception, templateVars);
+    }
+
+    private Response buildInputParsingErrorResponse(Exception exception, ArrayList<String> templateVars) {
+        AAIException ex = new AAIException(AAI_4007, exception);
+        return Response
+            .status(400).entity(ErrorLogHelper
+                .getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars))
+            .build();
     }
+
+    private Response defaultException(Exception exception) {
+        ArrayList<String> templateVars = new ArrayList<>();
+        templateVars.add(request.getMethod());
+        templateVars.add("unknown");
+        AAIException ex = new AAIException("AAI_4000", exception);
+        // prefer xml, use json otherwise
+        return headers.getAcceptableMediaTypes().stream()
+            .filter(MediaType.APPLICATION_ATOM_XML_TYPE::isCompatible)
+            .findAny()
+            .map(xmlType -> 
+                Response.status(400).type(MediaType.APPLICATION_XML_TYPE)
+                    .entity(ErrorLogHelper.getRESTAPIErrorResponse(
+                        headers.getAcceptableMediaTypes(), ex, templateVars))
+                    .build())
+            .orElseGet(() -> 
+                Response.status(400).type(MediaType.APPLICATION_JSON_TYPE)
+                .entity(ErrorLogHelper.getRESTAPIErrorResponse(
+                    headers.getAcceptableMediaTypes(), ex, templateVars))
+                .build());
+    }
+
 }
diff --git a/pom.xml b/pom.xml
index a6f2876..5add3bb 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -30,7 +30,7 @@
     </parent>
     <groupId>org.onap.aai.resources</groupId>
     <artifactId>resources</artifactId>
-    <version>1.13.5-SNAPSHOT</version>
+    <version>1.13.6-SNAPSHOT</version>
     <name>aai-resources</name>
     <packaging>pom</packaging>
     <modules>