From b28ac7660ccfdb1e67987afbb668119af836b0f5 Mon Sep 17 00:00:00 2001
From: AJoshi19 <ajoshi19@slb.com>
Date: Thu, 16 Jan 2025 12:05:29 -0600
Subject: [PATCH] cleaner refactor

---
 .../middleware/GlobalExceptionMapper.java     | 37 +++++++------------
 1 file changed, 13 insertions(+), 24 deletions(-)

diff --git a/search-core/src/main/java/org/opengroup/osdu/search/middleware/GlobalExceptionMapper.java b/search-core/src/main/java/org/opengroup/osdu/search/middleware/GlobalExceptionMapper.java
index c68862b8a..f0c7f598d 100644
--- a/search-core/src/main/java/org/opengroup/osdu/search/middleware/GlobalExceptionMapper.java
+++ b/search-core/src/main/java/org/opengroup/osdu/search/middleware/GlobalExceptionMapper.java
@@ -138,17 +138,10 @@ public class GlobalExceptionMapper extends ResponseEntityExceptionHandler {
 
     private ResponseEntity<Object> getErrorResponse(AppException e) {
 
-        String exceptionMsg = e.getError().getMessage();
+        String exceptionMsgToLog = getExceptionMessageToLog(e.getError().getMessage());
 
-        if (canLogException(e)) {
-            logErrorOrWarning(exceptionMsg, e);
-
-            // log suppressed exception from Elastic's ResponseException if any
-            this.logSuppressedElasticException(e);
-        } else {
-            String loggingMsg = exceptionMsg.length() < configurationProperties.getMaxExceptionLogMessageLength() ? exceptionMsg : exceptionMsg.substring(0, configurationProperties.getMaxExceptionLogMessageLength());
-            logErrorOrWarning(loggingMsg, new AppException(e.getError().getCode(), e.getError().getReason(), loggingMsg));
-        }
+        logErrorOrWarning(exceptionMsgToLog, e);
+        logSuppressedElasticException(e);
 
         // Support for non-standard HttpStatus Codes
         HttpStatus httpStatus = HttpStatus.resolve(e.getError().getCode());
@@ -167,19 +160,6 @@ public class GlobalExceptionMapper extends ResponseEntityExceptionHandler {
         }
     }
 
-    private boolean canLogException(AppException e) {
-        if (e.getMessage().length() > configurationProperties.getMaxExceptionLogMessageLength()) {
-            return false;
-        }
-        Exception cause = e.getOriginalException();
-        if (cause != null && cause.getSuppressed() != null) {
-            for (Throwable t : cause.getSuppressed()) {
-                if (t instanceof ResponseException && t.getMessage() != null && ((ResponseException) t).getResponse().getEntity().getContentLength() > configurationProperties.getMaxExceptionLogMessageLength()) return false;
-            }
-        }
-        return true;
-    }
-
     private ObjectNode getValidationResponse(List<String> errors) {
         ObjectMapper mapper = new ObjectMapper();
         ObjectNode node = mapper.createObjectNode();
@@ -200,9 +180,18 @@ public class GlobalExceptionMapper extends ResponseEntityExceptionHandler {
         Exception cause = e.getOriginalException();
         if (cause != null && cause.getSuppressed() != null) {
             for (Throwable t : cause.getSuppressed()) {
-                if (t instanceof ResponseException) this.jaxRsDpsLogger.error(t.getMessage(), (ResponseException) t);
+                if (t instanceof ResponseException) {
+                    this.jaxRsDpsLogger.error(getExceptionMessageToLog(t.getMessage()), (ResponseException) t);
+                }
             }
         }
     }
+
+    private String getExceptionMessageToLog(String originalExceptionMessage) {
+        if (originalExceptionMessage.length() > configurationProperties.getMaxExceptionLogMessageLength()) {
+            return originalExceptionMessage.substring(0, configurationProperties.getMaxExceptionLogMessageLength());
+        }
+        return originalExceptionMessage;
+    }
 }
 
-- 
GitLab