Skip to content

Commit

Permalink
NotXContentException status code changed from 500 to 400 - commit squ…
Browse files Browse the repository at this point in the history
…ash for DCO fix (#4773)

Signed-off-by: Ayush Kataria <31301636+ayushKataria@users.noreply.github.com>

Signed-off-by: Ayush Kataria <31301636+ayushKataria@users.noreply.github.com>
  • Loading branch information
ayushKataria committed Nov 23, 2022
1 parent 741dc49 commit 139c2cf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Use ReplicationFailedException instead of OpensearchException in ReplicationTarget ([#4725](https://github.com/opensearch-project/OpenSearch/pull/4725))
- Migrate client transports to Apache HttpClient / Core 5.x ([#4459](https://github.com/opensearch-project/OpenSearch/pull/4459))
- Support remote translog transfer for request level durability([#4480](https://github.com/opensearch-project/OpenSearch/pull/4480))
- Changed http code on create index API with bad input raising NotXContentException from 500 to 400 ([#4773](https://github.com/opensearch-project/OpenSearch/pull/4773))

### Deprecated

Expand Down
3 changes: 3 additions & 0 deletions server/src/main/java/org/opensearch/ExceptionsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apache.lucene.index.IndexFormatTooOldException;
import org.opensearch.action.ShardOperationFailedException;
import org.opensearch.common.Nullable;
import org.opensearch.common.compress.NotXContentException;
import org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException;
import org.opensearch.index.Index;
import org.opensearch.rest.RestStatus;
Expand Down Expand Up @@ -94,6 +95,8 @@ public static RestStatus status(Throwable t) {
return RestStatus.BAD_REQUEST;
} else if (t instanceof OpenSearchRejectedExecutionException) {
return RestStatus.TOO_MANY_REQUESTS;
} else if (t instanceof NotXContentException) {
return RestStatus.BAD_REQUEST;
}
}
return RestStatus.INTERNAL_SERVER_ERROR;
Expand Down
15 changes: 15 additions & 0 deletions server/src/test/java/org/opensearch/rest/RestControllerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.opensearch.http.HttpServerTransport;
import org.opensearch.http.HttpStats;
import org.opensearch.indices.breaker.HierarchyCircuitBreakerService;
import org.opensearch.rest.action.admin.indices.RestCreateIndexAction;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.test.client.NoOpNodeClient;
import org.opensearch.test.rest.FakeRestRequest;
Expand Down Expand Up @@ -562,6 +563,20 @@ public void testHandleBadRequestWithHtmlSpecialCharsInUri() {
assertThat(channel.getRestResponse().content().utf8ToString(), containsString("invalid uri has been requested"));
}

public void testHandleBadInputWithCreateIndex() {
final FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withPath("/foo")
.withMethod(RestRequest.Method.PUT)
.withContent(new BytesArray("ddd"), XContentType.JSON)
.build();
final AssertingChannel channel = new AssertingChannel(fakeRestRequest, true, RestStatus.BAD_REQUEST);
restController.registerHandler(RestRequest.Method.PUT, "/foo", new RestCreateIndexAction());
restController.dispatchRequest(fakeRestRequest, channel, client.threadPool().getThreadContext());
assertEquals(
channel.getRestResponse().content().utf8ToString(),
"{\"error\":{\"root_cause\":[{\"type\":\"not_x_content_exception\",\"reason\":\"Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes\"}],\"type\":\"not_x_content_exception\",\"reason\":\"Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes\"},\"status\":400}"
);
}

public void testDispatchUnsupportedHttpMethod() {
final boolean hasContent = randomBoolean();
final RestRequest request = RestRequest.request(xContentRegistry(), new HttpRequest() {
Expand Down

0 comments on commit 139c2cf

Please sign in to comment.