Skip to content

Commit

Permalink
Change IAE to ValidationException
Browse files Browse the repository at this point in the history
  • Loading branch information
cbuescher committed Sep 9, 2024
1 parent 3691008 commit 4c2d668
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
13 changes: 10 additions & 3 deletions server/src/main/java/org/elasticsearch/rest/RestRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.ValidationException;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.unit.ByteSizeValue;
Expand Down Expand Up @@ -321,11 +322,17 @@ public final BytesReference requiredContent() {
if (hasContent() == false) {
throw new ElasticsearchParseException("request body is required");
} else if (xContentType.get() == null) {
throw new IllegalArgumentException("unknown content type");
throwValidationException("unknown content type");
}
return content();
}

private static void throwValidationException(String msg) {
ValidationException unknownContentType = new ValidationException();
unknownContentType.addValidationError(msg);
throw unknownContentType;
}

/**
* Get the value of the header or {@code null} if not found. This method only retrieves the first header value if multiple values are
* sent. Use of {@link #getAllHeaderValues(String)} should be preferred
Expand Down Expand Up @@ -585,12 +592,12 @@ public final Tuple<XContentType, BytesReference> contentOrSourceParam() {
String source = param("source");
String typeParam = param("source_content_type");
if (source == null || typeParam == null) {
throw new IllegalArgumentException("source and source_content_type parameters are required");
throwValidationException("source and source_content_type parameters are required");
}
BytesArray bytes = new BytesArray(source);
final XContentType xContentType = parseContentType(Collections.singletonList(typeParam));
if (xContentType == null) {
throw new IllegalArgumentException("Unknown value for source_content_type [" + typeParam + "]");
throwValidationException("Unknown value for source_content_type [" + typeParam + "]");
}
return new Tuple<>(xContentType, bytes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.elasticsearch.rest;

import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.ValidationException;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.core.CheckedConsumer;
Expand All @@ -33,6 +34,7 @@
import static java.util.Collections.singletonMap;
import static org.elasticsearch.rest.RestRequest.OPERATOR_REQUEST;
import static org.elasticsearch.rest.RestRequest.SERVERLESS_REQUEST;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -130,8 +132,8 @@ public void testContentOrSourceParam() throws IOException {
.contentOrSourceParam()
.v2()
);
e = expectThrows(IllegalArgumentException.class, () -> contentRestRequest("", Map.of("source", "stuff2")).contentOrSourceParam());
assertEquals("source and source_content_type parameters are required", e.getMessage());
e = expectThrows(ValidationException.class, () -> contentRestRequest("", Map.of("source", "stuff2")).contentOrSourceParam());
assertThat(e.getMessage(), containsString("source and source_content_type parameters are required"));
}

public void testHasContentOrSourceParam() throws IOException {
Expand Down Expand Up @@ -246,8 +248,8 @@ public void testRequiredContent() {
.requiredContent()
);
assertEquals("request body is required", e.getMessage());
e = expectThrows(IllegalArgumentException.class, () -> contentRestRequest("test", null, Collections.emptyMap()).requiredContent());
assertEquals("unknown content type", e.getMessage());
e = expectThrows(ValidationException.class, () -> contentRestRequest("test", null, Collections.emptyMap()).requiredContent());
assertThat(e.getMessage(), containsString("unknown content type"));
}

public void testIsServerlessRequest() {
Expand Down

0 comments on commit 4c2d668

Please sign in to comment.