From 139c2cfdf7d274a6e72d49f0673e7dc4ef38a327 Mon Sep 17 00:00:00 2001 From: Ayush Kataria <31301636+ayushKataria@users.noreply.github.com> Date: Wed, 23 Nov 2022 21:54:56 +0530 Subject: [PATCH] NotXContentException status code changed from 500 to 400 - commit squash 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> --- CHANGELOG.md | 1 + .../java/org/opensearch/ExceptionsHelper.java | 3 +++ .../org/opensearch/rest/RestControllerTests.java | 15 +++++++++++++++ 3 files changed, 19 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50196606d308c..7fd16ff6af292 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/server/src/main/java/org/opensearch/ExceptionsHelper.java b/server/src/main/java/org/opensearch/ExceptionsHelper.java index fbfc9beaea468..35e9d23c3502b 100644 --- a/server/src/main/java/org/opensearch/ExceptionsHelper.java +++ b/server/src/main/java/org/opensearch/ExceptionsHelper.java @@ -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; @@ -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; diff --git a/server/src/test/java/org/opensearch/rest/RestControllerTests.java b/server/src/test/java/org/opensearch/rest/RestControllerTests.java index bd4c7c9a4f824..03f6b7fca748e 100644 --- a/server/src/test/java/org/opensearch/rest/RestControllerTests.java +++ b/server/src/test/java/org/opensearch/rest/RestControllerTests.java @@ -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; @@ -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() {