DATAES-822 - Convert Reactive Client exceptions to ElasticsearchStatusException. (#453)

Original PR: #453
This commit is contained in:
Peter-Josef Meisch 2020-05-12 06:07:21 +02:00 committed by GitHub
parent c3bde816fe
commit fd713bfc8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 34 deletions

View File

@ -115,8 +115,6 @@ import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.reactive.function.BodyExtractors;
import org.springframework.web.reactive.function.client.ClientRequest;
import org.springframework.web.reactive.function.client.ClientResponse;
@ -725,9 +723,10 @@ public class DefaultReactiveElasticsearchClient implements ReactiveElasticsearch
private <T> Publisher<? extends T> handleServerError(Request request, ClientResponse response) {
return Mono.error(
new HttpServerErrorException(response.statusCode(), String.format("%s request to %s returned error code %s.",
request.getMethod(), request.getEndpoint(), response.statusCode().value())));
RestStatus status = RestStatus.fromCode(response.statusCode().value());
return Mono.error(new ElasticsearchStatusException(String.format("%s request to %s returned error code %s.",
request.getMethod(), request.getEndpoint(), response.statusCode().value()), status));
}
private <T> Publisher<? extends T> handleClientError(String logId, Request request, ClientResponse response,
@ -738,20 +737,19 @@ public class DefaultReactiveElasticsearchClient implements ReactiveElasticsearch
.flatMap(content -> {
String mediaType = response.headers().contentType().map(MediaType::toString)
.orElse(XContentType.JSON.mediaType());
RestStatus status = RestStatus.fromCode(response.statusCode().value());
try {
ElasticsearchException exception = getElasticsearchException(response, content, mediaType);
if (exception != null) {
StringBuilder sb = new StringBuilder();
buildExceptionMessages(sb, exception);
return Mono.error(new HttpClientErrorException(response.statusCode(), sb.toString()));
return Mono.error(new ElasticsearchStatusException(sb.toString(), status, exception));
}
} catch (Exception e) {
return Mono
.error(new ElasticsearchStatusException(content, RestStatus.fromCode(response.statusCode().value())));
return Mono.error(new ElasticsearchStatusException(content, status));
}
return Mono.just(content);
})
.doOnNext(it -> ClientLogger.logResponse(logId, response.statusCode(), it)) //
}).doOnNext(it -> ClientLogger.logResponse(logId, response.statusCode(), it)) //
.flatMap(content -> doDecode(response, responseType, content));
}

View File

@ -18,7 +18,6 @@ package org.springframework.data.elasticsearch.client.reactive;
import static org.assertj.core.api.Assertions.*;
import lombok.SneakyThrows;
import org.springframework.web.client.HttpClientErrorException;
import reactor.test.StepVerifier;
import java.io.IOException;
@ -149,7 +148,7 @@ public class ReactiveElasticsearchClientTests {
client.get(new GetRequest(INDEX_I, TYPE_I, "nonono")) //
.as(StepVerifier::create) //
.expectError(HttpClientErrorException.class) //
.expectError(ElasticsearchStatusException.class) //
.verify();
}
@ -321,7 +320,7 @@ public class ReactiveElasticsearchClientTests {
client.index(request) //
.as(StepVerifier::create) //
.verifyError(HttpClientErrorException.class);
.verifyError(ElasticsearchStatusException.class);
}
@Test // DATAES-488
@ -370,7 +369,7 @@ public class ReactiveElasticsearchClientTests {
client.update(request) //
.as(StepVerifier::create) //
.verifyError(HttpClientErrorException.class);
.verifyError(ElasticsearchStatusException.class);
}
@Test // DATAES-488
@ -537,7 +536,7 @@ public class ReactiveElasticsearchClientTests {
client.indices().createIndex(request -> request.index(INDEX_I)) //
.as(StepVerifier::create) //
.verifyError(HttpClientErrorException.class);
.verifyError(ElasticsearchStatusException.class);
}
@Test // DATAES-569
@ -557,7 +556,7 @@ public class ReactiveElasticsearchClientTests {
client.indices().deleteIndex(request -> request.indices(INDEX_I)) //
.as(StepVerifier::create) //
.verifyError(HttpClientErrorException.class);
.verifyError(ElasticsearchStatusException.class);
}
@Test // DATAES-569
@ -575,7 +574,7 @@ public class ReactiveElasticsearchClientTests {
client.indices().openIndex(request -> request.indices(INDEX_I)) //
.as(StepVerifier::create) //
.verifyError(HttpClientErrorException.class);
.verifyError(ElasticsearchStatusException.class);
}
@Test // DATAES-569
@ -593,7 +592,7 @@ public class ReactiveElasticsearchClientTests {
client.indices().closeIndex(request -> request.indices(INDEX_I)) //
.as(StepVerifier::create) //
.verifyError(HttpClientErrorException.class);
.verifyError(ElasticsearchStatusException.class);
}
@Test // DATAES-569
@ -611,7 +610,7 @@ public class ReactiveElasticsearchClientTests {
client.indices().refreshIndex(request -> request.indices(INDEX_I)) //
.as(StepVerifier::create) //
.verifyError(HttpClientErrorException.class);
.verifyError(ElasticsearchStatusException.class);
}
@Test // DATAES-569
@ -635,7 +634,7 @@ public class ReactiveElasticsearchClientTests {
client.indices().updateMapping(request -> request.indices(INDEX_I).type(TYPE_I).source(jsonMap)) //
.as(StepVerifier::create) //
.verifyError(HttpClientErrorException.class);
.verifyError(ElasticsearchStatusException.class);
}
@Test // DATAES-569
@ -653,7 +652,7 @@ public class ReactiveElasticsearchClientTests {
client.indices().flushIndex(request -> request.indices(INDEX_I)) //
.as(StepVerifier::create) //
.verifyError(HttpClientErrorException.class);
.verifyError(ElasticsearchStatusException.class);
}
@Test // DATAES-684

View File

@ -20,7 +20,6 @@ import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import static org.springframework.data.elasticsearch.client.reactive.ReactiveMockClientTestsUtils.MockWebClientProvider.Receive.*;
import org.springframework.web.client.HttpClientErrorException;
import org.elasticsearch.rest.RestStatus;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@ -465,7 +464,7 @@ public class ReactiveElasticsearchClientUnitTests {
client.update(new UpdateRequest("twitter", "doc", "1").doc(Collections.singletonMap("user", "cstrobl")))
.as(StepVerifier::create) //
.expectError(HttpClientErrorException.class) //
.expectError(ElasticsearchStatusException.class) //
.verify();
}

View File

@ -24,7 +24,6 @@ import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.web.client.HttpClientErrorException;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@ -209,7 +208,7 @@ public class ReactiveElasticsearchTemplateTests {
template.findById("foo", SampleEntity.class, "no-such-index") //
.as(StepVerifier::create) //
.expectError(HttpClientErrorException.class);
.expectError(ElasticsearchStatusException.class);
}
@Test // DATAES-504
@ -318,7 +317,7 @@ public class ReactiveElasticsearchTemplateTests {
template.find(new CriteriaQuery(Criteria.where("message").is("some message")), SampleEntity.class, "no-such-index") //
.as(StepVerifier::create) //
.expectError(HttpClientErrorException.class);
.expectError(ElasticsearchStatusException.class);
}
@Test // DATAES-504
@ -426,7 +425,7 @@ public class ReactiveElasticsearchTemplateTests {
template.find(queryWithInvalidPreference, SampleEntity.class) //
.as(StepVerifier::create) //
.expectError(HttpClientErrorException.class).verify();
.expectError(ElasticsearchStatusException.class).verify();
}
@Test // DATAES-504
@ -486,7 +485,7 @@ public class ReactiveElasticsearchTemplateTests {
template.count(SampleEntity.class) //
.as(StepVerifier::create) //
.expectError(HttpClientErrorException.class);
.expectError(ElasticsearchStatusException.class);
}
@Test // DATAES-504
@ -518,7 +517,7 @@ public class ReactiveElasticsearchTemplateTests {
template.deleteById("does-not-exists", SampleEntity.class, "no-such-index") //
.as(StepVerifier::create)//
.expectError(HttpClientErrorException.class);
.expectError(ElasticsearchStatusException.class);
}
@Test // DATAES-504

View File

@ -23,6 +23,7 @@ import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.elasticsearch.ElasticsearchStatusException;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@ -66,7 +67,6 @@ import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.StringUtils;
import org.springframework.web.client.HttpClientErrorException;
/**
* @author Christoph Strobl
@ -135,7 +135,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
public void findByIdShouldErrorIfIndexDoesNotExist() {
repository.findById("id-two") //
.as(StepVerifier::create) //
.expectError(HttpClientErrorException.class);
.expectError(ElasticsearchStatusException.class);
}
@Test // DATAES-519
@ -180,7 +180,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
@Test // DATAES-519, DATAES-767
public void findAllByIdByIdShouldCompleteIfIndexDoesNotExist() {
repository.findAllById(Arrays.asList("id-two", "id-two")).as(StepVerifier::create)
.expectError(HttpClientErrorException.class);
.expectError(ElasticsearchStatusException.class);
}
@ -211,7 +211,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
@Test // DATAES-519, DATAE-767
public void countShouldReturnZeroWhenIndexDoesNotExist() {
repository.count().as(StepVerifier::create).expectNext(0L).expectError(HttpClientErrorException.class);
repository.count().as(StepVerifier::create).expectNext(0L).expectError(ElasticsearchStatusException.class);
}
@ -302,7 +302,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
public void deleteByIdShouldErrorWhenIndexDoesNotExist() {
repository.deleteById("does-not-exist") //
.as(StepVerifier::create) //
.verifyError(HttpClientErrorException.class);
.verifyError(ElasticsearchStatusException.class);
;
}