Skip to content

Commit

Permalink
Support null exception in ErrorMessage (opensearch-project#2900)
Browse files Browse the repository at this point in the history
* null exception

Signed-off-by: Jing Zhang <[email protected]>

* address comments

Signed-off-by: Jing Zhang <[email protected]>

---------

Signed-off-by: Jing Zhang <[email protected]>
  • Loading branch information
jngz-es authored Sep 9, 2024
1 parent a9c6a6e commit 55d28e0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,18 @@ public ErrorMessage(Throwable exception, int status) {
}

private String fetchType() {
return exception.getClass().getSimpleName();
return exception == null ? "Unknown Exception" : exception.getClass().getSimpleName();
}

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

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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

0 comments on commit 55d28e0

Please sign in to comment.