Skip to content

Commit 54f300c

Browse files
vamsimanoharacarbonetto
authored andcommitted
Improved error codes in case of security exception (opensearch-project#1753)
Signed-off-by: Vamsi Manohar <[email protected]>
1 parent 666cd58 commit 54f300c

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

datasources/src/main/java/org/opensearch/sql/datasources/exceptions/ErrorMessage.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package org.opensearch.sql.datasources.exceptions;
88

99
import com.google.gson.Gson;
10+
import com.google.gson.GsonBuilder;
1011
import com.google.gson.JsonObject;
1112
import lombok.Getter;
1213
import org.opensearch.rest.RestStatus;
@@ -65,7 +66,8 @@ public String toString() {
6566
JsonObject jsonObject = new JsonObject();
6667
jsonObject.addProperty("status", status);
6768
jsonObject.add("error", getErrorAsJson());
68-
return new Gson().toJson(jsonObject);
69+
Gson gson = new GsonBuilder().setPrettyPrinting().create();
70+
return gson.toJson(jsonObject);
6971
}
7072

7173
private JsonObject getErrorAsJson() {

datasources/src/main/java/org/opensearch/sql/datasources/rest/RestDataSourceQueryAction.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Locale;
2222
import org.apache.logging.log4j.LogManager;
2323
import org.apache.logging.log4j.Logger;
24+
import org.opensearch.OpenSearchException;
2425
import org.opensearch.action.ActionListener;
2526
import org.opensearch.client.node.NodeClient;
2627
import org.opensearch.rest.BaseRestHandler;
@@ -224,6 +225,9 @@ public void onFailure(Exception e) {
224225
private void handleException(Exception e, RestChannel restChannel) {
225226
if (e instanceof DataSourceNotFoundException) {
226227
reportError(restChannel, e, NOT_FOUND);
228+
} else if (e instanceof OpenSearchException) {
229+
OpenSearchException exception = (OpenSearchException) e;
230+
reportError(restChannel, exception, exception.status());
227231
} else {
228232
LOG.error("Error happened during request handling", e);
229233
if (isClientError(e)) {

0 commit comments

Comments
 (0)