Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed http code on create index API with bad input raising NotXContentException to 400 #4773

Merged
merged 1 commit into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
dblock marked this conversation as resolved.
Show resolved Hide resolved

### 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
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