-
Notifications
You must be signed in to change notification settings - Fork 25.6k
HLRC: Fix strict setting exception handling #37247
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2abe34a
HLRC: Fix strict setting exception handling
hub-cap 0b375cf
Merge remote-tracking branch 'upstream/master' into hlrc_strict
hub-cap 0b41895
Merge remote-tracking branch 'upstream/master' into hlrc_strict
hub-cap e11dbd5
Just throw the exception itself
hub-cap 96796d1
Merge remote-tracking branch 'upstream/master' into hlrc_strict
hub-cap 64f9e66
fixups test + review
hub-cap c53dd14
Merge remote-tracking branch 'upstream/master' into hlrc_strict
hub-cap 9ba7b44
move variable
hub-cap 5b5ac9e
Merge remote-tracking branch 'upstream/master' into hlrc_strict
hub-cap File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
client/rest-high-level/src/test/java/org/elasticsearch/client/MockRestHighLevelTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.client; | ||
|
|
||
| import org.apache.http.HttpHost; | ||
| import org.apache.http.ProtocolVersion; | ||
| import org.apache.http.RequestLine; | ||
| import org.apache.http.client.methods.HttpGet; | ||
| import org.apache.http.message.BasicRequestLine; | ||
| import org.apache.http.message.BasicStatusLine; | ||
| import org.elasticsearch.ElasticsearchStatusException; | ||
| import org.elasticsearch.test.ESTestCase; | ||
| import org.junit.Before; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| import static org.hamcrest.Matchers.equalTo; | ||
| import static org.mockito.Matchers.any; | ||
| import static org.mockito.Mockito.doThrow; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| public class MockRestHighLevelTests extends ESTestCase { | ||
| private RestHighLevelClient client; | ||
| private WarningFailureException expectedException; | ||
| private static final List<String> WARNINGS = Collections.singletonList("Some Warning"); | ||
|
|
||
| @Before | ||
| private void setupClient() throws IOException { | ||
| final RestClient mockClient = mock(RestClient.class); | ||
| final Response mockResponse = mock(Response.class); | ||
|
|
||
| when(mockResponse.getHost()).thenReturn(new HttpHost("localhost", 9200)); | ||
| when(mockResponse.getWarnings()).thenReturn(WARNINGS); | ||
|
|
||
| ProtocolVersion protocol = new ProtocolVersion("HTTP", 1, 1); | ||
| when(mockResponse.getStatusLine()).thenReturn(new BasicStatusLine(protocol, 200, "OK")); | ||
|
|
||
| RequestLine requestLine = new BasicRequestLine(HttpGet.METHOD_NAME, "/_blah", protocol); | ||
| when(mockResponse.getRequestLine()).thenReturn(requestLine); | ||
|
|
||
| expectedException = new WarningFailureException(mockResponse); | ||
| doThrow(expectedException).when(mockClient).performRequest(any()); | ||
|
|
||
| client = new RestHighLevelClient(mockClient, RestClient::close, Collections.emptyList()); | ||
| } | ||
|
|
||
| public void testWarningFailure() { | ||
| ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, | ||
| () -> client.info(RequestOptions.DEFAULT)); | ||
| assertThat(exception.getMessage(), equalTo("Warnings/Deprecations caused response to fail")); | ||
| assertThat(exception.getCause(), equalTo(expectedException)); | ||
| WarningFailureException warningFailureException = (WarningFailureException) exception.getCause(); | ||
| assertThat(warningFailureException.getResponse().getWarnings(), equalTo(WARNINGS)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
client/rest/src/main/java/org/elasticsearch/client/WarningFailureException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.client; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import static org.elasticsearch.client.ResponseException.buildMessage; | ||
|
|
||
| /** | ||
| * This exception is used to indicate that one or more {@link Response#getWarnings()} exist | ||
| * and is typically used when the {@link RestClient} is set to fail by setting | ||
| * {@link RestClientBuilder#setStrictDeprecationMode(boolean)} to `true`. | ||
| */ | ||
| // This class extends RuntimeException in order to deal with wrapping that is done in FutureUtils on exception. | ||
| // if the exception is not of type ElasticsearchException or RuntimeException it will be wrapped in a UncategorizedExecutionException | ||
| public class WarningFailureException extends RuntimeException { | ||
|
|
||
| private Response response; | ||
|
||
|
|
||
| public WarningFailureException(Response response) throws IOException { | ||
| super(buildMessage(response)); | ||
| this.response = response; | ||
| } | ||
|
|
||
| /** | ||
| * Wrap a {@linkplain WarningFailureException} with another one with the current | ||
| * stack trace. This is used during synchronous calls so that the caller | ||
| * ends up in the stack trace of the exception thrown. | ||
| */ | ||
| WarningFailureException(WarningFailureException e) { | ||
| super(e.getMessage(), e); | ||
| this.response = e.getResponse(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the {@link Response} that caused this exception to be thrown. | ||
| */ | ||
| public Response getResponse() { | ||
| return response; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that these changes to ResponseException are no longer needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doh, good call!