Skip to content

Commit 822d62b

Browse files
Support null exception in ErrorMessage (opensearch-project#2900) (opensearch-project#2920)
* null exception Signed-off-by: Jing Zhang <jngz@amazon.com> * address comments Signed-off-by: Jing Zhang <jngz@amazon.com> --------- Signed-off-by: Jing Zhang <jngz@amazon.com> (cherry picked from commit 55d28e0) Co-authored-by: Jing Zhang <jngz@amazon.com>
1 parent ed37690 commit 822d62b

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

plugin/src/main/java/org/opensearch/ml/utils/error/ErrorMessage.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,18 @@ public ErrorMessage(Throwable exception, int status) {
4444
}
4545

4646
private String fetchType() {
47-
return exception.getClass().getSimpleName();
47+
return exception == null ? "Unknown Exception" : exception.getClass().getSimpleName();
4848
}
4949

5050
protected String fetchReason() {
5151
return status == RestStatus.BAD_REQUEST.getStatus() ? "Invalid Request" : "System Error";
5252
}
5353

5454
protected String fetchDetails() {
55+
if (exception == null) {
56+
return "No Exception Details";
57+
}
58+
5559
final String msg;
5660
// Prevent the method from exposing internal information such as internal ip address etc. that is a security concern.
5761
if (hasInternalInformation(exception)) {

plugin/src/test/java/org/opensearch/ml/utils/error/ErrorMessageTests.java

+8
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,12 @@ public void getDetails() {
144144

145145
assertEquals(errorMessage.getDetails(), "illegal state");
146146
}
147+
148+
@Test
149+
public void ConstructNullException() {
150+
ErrorMessage errorMessage = new ErrorMessage(null, SERVICE_UNAVAILABLE.getStatus());
151+
152+
assertEquals(errorMessage.getType(), "Unknown Exception");
153+
assertEquals(errorMessage.getDetails(), "No Exception Details");
154+
}
147155
}

0 commit comments

Comments
 (0)