-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* system error handling Signed-off-by: Jing Zhang <[email protected]> * remove NullPointerException from client error Signed-off-by: Jing Zhang <[email protected]> * replace jsonobject with ObjectMapper Signed-off-by: Jing Zhang <[email protected]> * add more UT Signed-off-by: Jing Zhang <[email protected]> * fix format issue Signed-off-by: Jing Zhang <[email protected]> * spotless Signed-off-by: Jing Zhang <[email protected]> --------- Signed-off-by: Jing Zhang <[email protected]> (cherry picked from commit c2a1d82)
- Loading branch information
1 parent
f41d8a0
commit 3033174
Showing
6 changed files
with
354 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
plugin/src/main/java/org/opensearch/ml/utils/error/ErrorMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.utils.error; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.opensearch.core.rest.RestStatus; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import lombok.Getter; | ||
import lombok.SneakyThrows; | ||
|
||
/** Error Message. */ | ||
public class ErrorMessage { | ||
|
||
protected Throwable exception; | ||
|
||
private final int status; | ||
|
||
@Getter | ||
private final String type; | ||
|
||
@Getter | ||
private final String reason; | ||
|
||
@Getter | ||
private final String details; | ||
|
||
/** Error Message Constructor. */ | ||
public ErrorMessage(Throwable exception, int status) { | ||
this.exception = exception; | ||
this.status = status; | ||
|
||
this.type = fetchType(); | ||
this.reason = fetchReason(); | ||
this.details = fetchDetails(); | ||
} | ||
|
||
private String fetchType() { | ||
return exception.getClass().getSimpleName(); | ||
} | ||
|
||
protected String fetchReason() { | ||
return status == RestStatus.BAD_REQUEST.getStatus() ? "Invalid Request" : "System Error"; | ||
} | ||
|
||
protected String fetchDetails() { | ||
// Some exception prints internal information (full class name) which is security concern | ||
return emptyStringIfNull(exception.getLocalizedMessage()); | ||
} | ||
|
||
private String emptyStringIfNull(String str) { | ||
return str != null ? str : ""; | ||
} | ||
|
||
@SneakyThrows | ||
@Override | ||
public String toString() { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
Map<String, Object> errorContent = new HashMap<>(); | ||
errorContent.put("type", type); | ||
errorContent.put("reason", reason); | ||
errorContent.put("details", details); | ||
Map<String, Object> errMessage = new HashMap<>(); | ||
errMessage.put("status", status); | ||
errMessage.put("error", errorContent); | ||
|
||
return objectMapper.writeValueAsString(errMessage); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
plugin/src/main/java/org/opensearch/ml/utils/error/ErrorMessageFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.utils.error; | ||
|
||
import org.opensearch.OpenSearchException; | ||
|
||
import lombok.experimental.UtilityClass; | ||
|
||
@UtilityClass | ||
public class ErrorMessageFactory { | ||
/** | ||
* Create error message based on the exception type. | ||
* | ||
* @param e exception to create error message | ||
* @param status exception status code | ||
* @return error message | ||
*/ | ||
public static ErrorMessage createErrorMessage(Throwable e, int status) { | ||
Throwable t = e; | ||
int st = status; | ||
if (t instanceof OpenSearchException) { | ||
st = ((OpenSearchException) t).status().getStatus(); | ||
} else { | ||
t = unwrapCause(e); | ||
} | ||
|
||
return new ErrorMessage(t, st); | ||
} | ||
|
||
protected static Throwable unwrapCause(Throwable t) { | ||
Throwable result = t; | ||
if (result instanceof OpenSearchException) { | ||
return result; | ||
} | ||
if (result.getCause() == null) { | ||
return result; | ||
} | ||
result = unwrapCause(result.getCause()); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
plugin/src/test/java/org/opensearch/ml/utils/error/ErrorMessageFactoryTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.utils.error; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.junit.Test; | ||
import org.opensearch.OpenSearchException; | ||
import org.opensearch.core.rest.RestStatus; | ||
|
||
public class ErrorMessageFactoryTests { | ||
|
||
private Throwable nonOpenSearchThrowable = new Throwable(); | ||
private Throwable openSearchThrowable = new OpenSearchException(nonOpenSearchThrowable); | ||
|
||
@Test | ||
public void openSearchExceptionShouldCreateEsErrorMessage() { | ||
Exception exception = new OpenSearchException(nonOpenSearchThrowable); | ||
ErrorMessage msg = ErrorMessageFactory.createErrorMessage(exception, RestStatus.BAD_REQUEST.getStatus()); | ||
assertTrue(msg.exception instanceof OpenSearchException); | ||
} | ||
|
||
@Test | ||
public void nonOpenSearchExceptionShouldCreateGenericErrorMessage() { | ||
Exception exception = new Exception(nonOpenSearchThrowable); | ||
ErrorMessage msg = ErrorMessageFactory.createErrorMessage(exception, RestStatus.BAD_REQUEST.getStatus()); | ||
assertFalse(msg.exception instanceof OpenSearchException); | ||
} | ||
|
||
@Test | ||
public void nonOpenSearchExceptionWithWrappedEsExceptionCauseShouldCreateEsErrorMessage() { | ||
Exception exception = (Exception) openSearchThrowable; | ||
ErrorMessage msg = ErrorMessageFactory.createErrorMessage(exception, RestStatus.BAD_REQUEST.getStatus()); | ||
assertTrue(msg.exception instanceof OpenSearchException); | ||
} | ||
|
||
@Test | ||
public void nonOpenSearchExceptionWithMultiLayerWrappedEsExceptionCauseShouldCreateEsErrorMessage() { | ||
Exception exception = new Exception(new Throwable(new Throwable(openSearchThrowable))); | ||
ErrorMessage msg = ErrorMessageFactory.createErrorMessage(exception, RestStatus.BAD_REQUEST.getStatus()); | ||
assertTrue(msg.exception instanceof OpenSearchException); | ||
} | ||
} |
Oops, something went wrong.