diff --git a/plugin/src/main/java/org/opensearch/ml/utils/error/ErrorMessage.java b/plugin/src/main/java/org/opensearch/ml/utils/error/ErrorMessage.java index 48307c50f2..0cb095cb06 100644 --- a/plugin/src/main/java/org/opensearch/ml/utils/error/ErrorMessage.java +++ b/plugin/src/main/java/org/opensearch/ml/utils/error/ErrorMessage.java @@ -44,7 +44,7 @@ public ErrorMessage(Throwable exception, int status) { } private String fetchType() { - return exception.getClass().getSimpleName(); + return exception == null ? "Unknown Exception" : exception.getClass().getSimpleName(); } protected String fetchReason() { @@ -52,6 +52,10 @@ protected String fetchReason() { } protected String fetchDetails() { + if (exception == null) { + return "No Exception Details"; + } + final String msg; // Prevent the method from exposing internal information such as internal ip address etc. that is a security concern. if (hasInternalInformation(exception)) { diff --git a/plugin/src/test/java/org/opensearch/ml/utils/error/ErrorMessageTests.java b/plugin/src/test/java/org/opensearch/ml/utils/error/ErrorMessageTests.java index 9e6e8586fe..14419a1ce9 100644 --- a/plugin/src/test/java/org/opensearch/ml/utils/error/ErrorMessageTests.java +++ b/plugin/src/test/java/org/opensearch/ml/utils/error/ErrorMessageTests.java @@ -144,4 +144,12 @@ public void getDetails() { assertEquals(errorMessage.getDetails(), "illegal state"); } + + @Test + public void ConstructNullException() { + ErrorMessage errorMessage = new ErrorMessage(null, SERVICE_UNAVAILABLE.getStatus()); + + assertEquals(errorMessage.getType(), "Unknown Exception"); + assertEquals(errorMessage.getDetails(), "No Exception Details"); + } }