mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-25 21:42:11 +00:00
DATAES-822 - Convert Reactive Client exceptions to ElasticsearchStatusException. (#453)
Original PR: #453
This commit is contained in:
parent
c3bde816fe
commit
fd713bfc8e
@ -115,8 +115,6 @@ import org.springframework.util.Assert;
|
|||||||
import org.springframework.util.ObjectUtils;
|
import org.springframework.util.ObjectUtils;
|
||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
import org.springframework.util.StringUtils;
|
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.BodyExtractors;
|
||||||
import org.springframework.web.reactive.function.client.ClientRequest;
|
import org.springframework.web.reactive.function.client.ClientRequest;
|
||||||
import org.springframework.web.reactive.function.client.ClientResponse;
|
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) {
|
private <T> Publisher<? extends T> handleServerError(Request request, ClientResponse response) {
|
||||||
|
|
||||||
return Mono.error(
|
RestStatus status = RestStatus.fromCode(response.statusCode().value());
|
||||||
new HttpServerErrorException(response.statusCode(), String.format("%s request to %s returned error code %s.",
|
|
||||||
request.getMethod(), request.getEndpoint(), 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,
|
private <T> Publisher<? extends T> handleClientError(String logId, Request request, ClientResponse response,
|
||||||
@ -738,20 +737,19 @@ public class DefaultReactiveElasticsearchClient implements ReactiveElasticsearch
|
|||||||
.flatMap(content -> {
|
.flatMap(content -> {
|
||||||
String mediaType = response.headers().contentType().map(MediaType::toString)
|
String mediaType = response.headers().contentType().map(MediaType::toString)
|
||||||
.orElse(XContentType.JSON.mediaType());
|
.orElse(XContentType.JSON.mediaType());
|
||||||
|
RestStatus status = RestStatus.fromCode(response.statusCode().value());
|
||||||
try {
|
try {
|
||||||
ElasticsearchException exception = getElasticsearchException(response, content, mediaType);
|
ElasticsearchException exception = getElasticsearchException(response, content, mediaType);
|
||||||
if (exception != null) {
|
if (exception != null) {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
buildExceptionMessages(sb, exception);
|
buildExceptionMessages(sb, exception);
|
||||||
return Mono.error(new HttpClientErrorException(response.statusCode(), sb.toString()));
|
return Mono.error(new ElasticsearchStatusException(sb.toString(), status, exception));
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return Mono
|
return Mono.error(new ElasticsearchStatusException(content, status));
|
||||||
.error(new ElasticsearchStatusException(content, RestStatus.fromCode(response.statusCode().value())));
|
|
||||||
}
|
}
|
||||||
return Mono.just(content);
|
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));
|
.flatMap(content -> doDecode(response, responseType, content));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,6 @@ package org.springframework.data.elasticsearch.client.reactive;
|
|||||||
import static org.assertj.core.api.Assertions.*;
|
import static org.assertj.core.api.Assertions.*;
|
||||||
|
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import org.springframework.web.client.HttpClientErrorException;
|
|
||||||
import reactor.test.StepVerifier;
|
import reactor.test.StepVerifier;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -149,7 +148,7 @@ public class ReactiveElasticsearchClientTests {
|
|||||||
|
|
||||||
client.get(new GetRequest(INDEX_I, TYPE_I, "nonono")) //
|
client.get(new GetRequest(INDEX_I, TYPE_I, "nonono")) //
|
||||||
.as(StepVerifier::create) //
|
.as(StepVerifier::create) //
|
||||||
.expectError(HttpClientErrorException.class) //
|
.expectError(ElasticsearchStatusException.class) //
|
||||||
.verify();
|
.verify();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -321,7 +320,7 @@ public class ReactiveElasticsearchClientTests {
|
|||||||
|
|
||||||
client.index(request) //
|
client.index(request) //
|
||||||
.as(StepVerifier::create) //
|
.as(StepVerifier::create) //
|
||||||
.verifyError(HttpClientErrorException.class);
|
.verifyError(ElasticsearchStatusException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAES-488
|
@Test // DATAES-488
|
||||||
@ -370,7 +369,7 @@ public class ReactiveElasticsearchClientTests {
|
|||||||
|
|
||||||
client.update(request) //
|
client.update(request) //
|
||||||
.as(StepVerifier::create) //
|
.as(StepVerifier::create) //
|
||||||
.verifyError(HttpClientErrorException.class);
|
.verifyError(ElasticsearchStatusException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAES-488
|
@Test // DATAES-488
|
||||||
@ -537,7 +536,7 @@ public class ReactiveElasticsearchClientTests {
|
|||||||
|
|
||||||
client.indices().createIndex(request -> request.index(INDEX_I)) //
|
client.indices().createIndex(request -> request.index(INDEX_I)) //
|
||||||
.as(StepVerifier::create) //
|
.as(StepVerifier::create) //
|
||||||
.verifyError(HttpClientErrorException.class);
|
.verifyError(ElasticsearchStatusException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAES-569
|
@Test // DATAES-569
|
||||||
@ -557,7 +556,7 @@ public class ReactiveElasticsearchClientTests {
|
|||||||
|
|
||||||
client.indices().deleteIndex(request -> request.indices(INDEX_I)) //
|
client.indices().deleteIndex(request -> request.indices(INDEX_I)) //
|
||||||
.as(StepVerifier::create) //
|
.as(StepVerifier::create) //
|
||||||
.verifyError(HttpClientErrorException.class);
|
.verifyError(ElasticsearchStatusException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAES-569
|
@Test // DATAES-569
|
||||||
@ -575,7 +574,7 @@ public class ReactiveElasticsearchClientTests {
|
|||||||
|
|
||||||
client.indices().openIndex(request -> request.indices(INDEX_I)) //
|
client.indices().openIndex(request -> request.indices(INDEX_I)) //
|
||||||
.as(StepVerifier::create) //
|
.as(StepVerifier::create) //
|
||||||
.verifyError(HttpClientErrorException.class);
|
.verifyError(ElasticsearchStatusException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAES-569
|
@Test // DATAES-569
|
||||||
@ -593,7 +592,7 @@ public class ReactiveElasticsearchClientTests {
|
|||||||
|
|
||||||
client.indices().closeIndex(request -> request.indices(INDEX_I)) //
|
client.indices().closeIndex(request -> request.indices(INDEX_I)) //
|
||||||
.as(StepVerifier::create) //
|
.as(StepVerifier::create) //
|
||||||
.verifyError(HttpClientErrorException.class);
|
.verifyError(ElasticsearchStatusException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAES-569
|
@Test // DATAES-569
|
||||||
@ -611,7 +610,7 @@ public class ReactiveElasticsearchClientTests {
|
|||||||
|
|
||||||
client.indices().refreshIndex(request -> request.indices(INDEX_I)) //
|
client.indices().refreshIndex(request -> request.indices(INDEX_I)) //
|
||||||
.as(StepVerifier::create) //
|
.as(StepVerifier::create) //
|
||||||
.verifyError(HttpClientErrorException.class);
|
.verifyError(ElasticsearchStatusException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAES-569
|
@Test // DATAES-569
|
||||||
@ -635,7 +634,7 @@ public class ReactiveElasticsearchClientTests {
|
|||||||
|
|
||||||
client.indices().updateMapping(request -> request.indices(INDEX_I).type(TYPE_I).source(jsonMap)) //
|
client.indices().updateMapping(request -> request.indices(INDEX_I).type(TYPE_I).source(jsonMap)) //
|
||||||
.as(StepVerifier::create) //
|
.as(StepVerifier::create) //
|
||||||
.verifyError(HttpClientErrorException.class);
|
.verifyError(ElasticsearchStatusException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAES-569
|
@Test // DATAES-569
|
||||||
@ -653,7 +652,7 @@ public class ReactiveElasticsearchClientTests {
|
|||||||
|
|
||||||
client.indices().flushIndex(request -> request.indices(INDEX_I)) //
|
client.indices().flushIndex(request -> request.indices(INDEX_I)) //
|
||||||
.as(StepVerifier::create) //
|
.as(StepVerifier::create) //
|
||||||
.verifyError(HttpClientErrorException.class);
|
.verifyError(ElasticsearchStatusException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAES-684
|
@Test // DATAES-684
|
||||||
|
@ -20,7 +20,6 @@ import static org.mockito.ArgumentMatchers.*;
|
|||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
import static org.springframework.data.elasticsearch.client.reactive.ReactiveMockClientTestsUtils.MockWebClientProvider.Receive.*;
|
import static org.springframework.data.elasticsearch.client.reactive.ReactiveMockClientTestsUtils.MockWebClientProvider.Receive.*;
|
||||||
|
|
||||||
import org.springframework.web.client.HttpClientErrorException;
|
|
||||||
import org.elasticsearch.rest.RestStatus;
|
import org.elasticsearch.rest.RestStatus;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
import reactor.test.StepVerifier;
|
import reactor.test.StepVerifier;
|
||||||
@ -465,7 +464,7 @@ public class ReactiveElasticsearchClientUnitTests {
|
|||||||
|
|
||||||
client.update(new UpdateRequest("twitter", "doc", "1").doc(Collections.singletonMap("user", "cstrobl")))
|
client.update(new UpdateRequest("twitter", "doc", "1").doc(Collections.singletonMap("user", "cstrobl")))
|
||||||
.as(StepVerifier::create) //
|
.as(StepVerifier::create) //
|
||||||
.expectError(HttpClientErrorException.class) //
|
.expectError(ElasticsearchStatusException.class) //
|
||||||
.verify();
|
.verify();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +24,6 @@ import lombok.Builder;
|
|||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import org.springframework.web.client.HttpClientErrorException;
|
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
import reactor.test.StepVerifier;
|
import reactor.test.StepVerifier;
|
||||||
|
|
||||||
@ -209,7 +208,7 @@ public class ReactiveElasticsearchTemplateTests {
|
|||||||
|
|
||||||
template.findById("foo", SampleEntity.class, "no-such-index") //
|
template.findById("foo", SampleEntity.class, "no-such-index") //
|
||||||
.as(StepVerifier::create) //
|
.as(StepVerifier::create) //
|
||||||
.expectError(HttpClientErrorException.class);
|
.expectError(ElasticsearchStatusException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAES-504
|
@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") //
|
template.find(new CriteriaQuery(Criteria.where("message").is("some message")), SampleEntity.class, "no-such-index") //
|
||||||
.as(StepVerifier::create) //
|
.as(StepVerifier::create) //
|
||||||
.expectError(HttpClientErrorException.class);
|
.expectError(ElasticsearchStatusException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAES-504
|
@Test // DATAES-504
|
||||||
@ -426,7 +425,7 @@ public class ReactiveElasticsearchTemplateTests {
|
|||||||
|
|
||||||
template.find(queryWithInvalidPreference, SampleEntity.class) //
|
template.find(queryWithInvalidPreference, SampleEntity.class) //
|
||||||
.as(StepVerifier::create) //
|
.as(StepVerifier::create) //
|
||||||
.expectError(HttpClientErrorException.class).verify();
|
.expectError(ElasticsearchStatusException.class).verify();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAES-504
|
@Test // DATAES-504
|
||||||
@ -486,7 +485,7 @@ public class ReactiveElasticsearchTemplateTests {
|
|||||||
|
|
||||||
template.count(SampleEntity.class) //
|
template.count(SampleEntity.class) //
|
||||||
.as(StepVerifier::create) //
|
.as(StepVerifier::create) //
|
||||||
.expectError(HttpClientErrorException.class);
|
.expectError(ElasticsearchStatusException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAES-504
|
@Test // DATAES-504
|
||||||
@ -518,7 +517,7 @@ public class ReactiveElasticsearchTemplateTests {
|
|||||||
|
|
||||||
template.deleteById("does-not-exists", SampleEntity.class, "no-such-index") //
|
template.deleteById("does-not-exists", SampleEntity.class, "no-such-index") //
|
||||||
.as(StepVerifier::create)//
|
.as(StepVerifier::create)//
|
||||||
.expectError(HttpClientErrorException.class);
|
.expectError(ElasticsearchStatusException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAES-504
|
@Test // DATAES-504
|
||||||
|
@ -23,6 +23,7 @@ import lombok.AllArgsConstructor;
|
|||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
import org.elasticsearch.ElasticsearchStatusException;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
import reactor.test.StepVerifier;
|
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.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.web.client.HttpClientErrorException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Christoph Strobl
|
* @author Christoph Strobl
|
||||||
@ -135,7 +135,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
|
|||||||
public void findByIdShouldErrorIfIndexDoesNotExist() {
|
public void findByIdShouldErrorIfIndexDoesNotExist() {
|
||||||
repository.findById("id-two") //
|
repository.findById("id-two") //
|
||||||
.as(StepVerifier::create) //
|
.as(StepVerifier::create) //
|
||||||
.expectError(HttpClientErrorException.class);
|
.expectError(ElasticsearchStatusException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAES-519
|
@Test // DATAES-519
|
||||||
@ -180,7 +180,7 @@ public class SimpleReactiveElasticsearchRepositoryTests {
|
|||||||
@Test // DATAES-519, DATAES-767
|
@Test // DATAES-519, DATAES-767
|
||||||
public void findAllByIdByIdShouldCompleteIfIndexDoesNotExist() {
|
public void findAllByIdByIdShouldCompleteIfIndexDoesNotExist() {
|
||||||
repository.findAllById(Arrays.asList("id-two", "id-two")).as(StepVerifier::create)
|
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
|
@Test // DATAES-519, DATAE-767
|
||||||
public void countShouldReturnZeroWhenIndexDoesNotExist() {
|
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() {
|
public void deleteByIdShouldErrorWhenIndexDoesNotExist() {
|
||||||
repository.deleteById("does-not-exist") //
|
repository.deleteById("does-not-exist") //
|
||||||
.as(StepVerifier::create) //
|
.as(StepVerifier::create) //
|
||||||
.verifyError(HttpClientErrorException.class);
|
.verifyError(ElasticsearchStatusException.class);
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user