Skip to content

Commit

Permalink
Improved error codes in case of security exception (#1753) (#1811)
Browse files Browse the repository at this point in the history
Signed-off-by: Vamsi Manohar <[email protected]>
(cherry picked from commit bb020ac)
  • Loading branch information
vamsi-amazon authored Jul 11, 2023
1 parent 9b26fa2 commit ee62371
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.opensearch.sql.datasources.exceptions;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import lombok.Getter;
import org.opensearch.rest.RestStatus;
Expand Down Expand Up @@ -65,7 +66,8 @@ public String toString() {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("status", status);
jsonObject.add("error", getErrorAsJson());
return new Gson().toJson(jsonObject);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
return gson.toJson(jsonObject);
}

private JsonObject getErrorAsJson() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Locale;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.OpenSearchException;
import org.opensearch.action.ActionListener;
import org.opensearch.client.node.NodeClient;
import org.opensearch.rest.BaseRestHandler;
Expand Down Expand Up @@ -224,6 +225,9 @@ public void onFailure(Exception e) {
private void handleException(Exception e, RestChannel restChannel) {
if (e instanceof DataSourceNotFoundException) {
reportError(restChannel, e, NOT_FOUND);
} else if (e instanceof OpenSearchException) {
OpenSearchException exception = (OpenSearchException) e;
reportError(restChannel, exception, exception.status());
} else {
LOG.error("Error happened during request handling", e);
if (isClientError(e)) {
Expand Down

0 comments on commit ee62371

Please sign in to comment.