Skip to content

Commit 5c7c24d

Browse files
committed
Refactor error handling
1 parent 5bd3172 commit 5c7c24d

File tree

9 files changed

+87
-59
lines changed

9 files changed

+87
-59
lines changed

java-client/src/main/java/co/elastic/clients/base/ApiException.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

java-client/src/main/java/co/elastic/clients/base/SimpleEndpoint.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919

2020
package co.elastic.clients.base;
2121

22+
import co.elastic.clients.elasticsearch._types.ErrorResponse;
2223
import co.elastic.clients.json.JsonpDeserializer;
2324
import org.apache.http.client.utils.URLEncodedUtils;
2425

2526
import java.util.Collections;
2627
import java.util.Map;
2728
import java.util.function.Function;
2829

29-
public class SimpleEndpoint<RequestT, ResponseT> implements Endpoint<RequestT, ResponseT, ElasticsearchError> {
30+
public class SimpleEndpoint<RequestT, ResponseT> implements Endpoint<RequestT, ResponseT, ErrorResponse> {
3031

3132
private static final Function<?, Map<String, String>> EMPTY_MAP = x -> Collections.emptyMap();
3233

@@ -99,8 +100,8 @@ public boolean isError(int statusCode) {
99100
}
100101

101102
@Override
102-
public JsonpDeserializer<ElasticsearchError> errorParser(int statusCode) {
103-
return ElasticsearchError._DESERIALIZER;
103+
public JsonpDeserializer<ErrorResponse> errorParser(int statusCode) {
104+
return ErrorResponse._DESERIALIZER;
104105
}
105106

106107
public <NewResponseT> SimpleEndpoint<RequestT, NewResponseT> withResponseDeserializer(

java-client/src/main/java/co/elastic/clients/base/rest_client/RestClientTransport.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919

2020
package co.elastic.clients.base.rest_client;
2121

22-
import co.elastic.clients.base.ApiException;
2322
import co.elastic.clients.base.BooleanEndpoint;
2423
import co.elastic.clients.base.BooleanResponse;
2524
import co.elastic.clients.base.ElasticsearchCatRequest;
25+
import co.elastic.clients.elasticsearch.ElasticsearchException;
2626
import co.elastic.clients.base.Endpoint;
2727
import co.elastic.clients.base.Transport;
28+
import co.elastic.clients.elasticsearch._types.ErrorResponse;
2829
import co.elastic.clients.json.JsonpDeserializer;
2930
import co.elastic.clients.json.JsonpMapper;
3031
import co.elastic.clients.json.NdJsonpSerializable;
@@ -222,7 +223,8 @@ private <ResponseT, ErrorT> ResponseT getHighLevelResponse(
222223
error = errorParser.deserialize(parser, mapper);
223224
}
224225

225-
throw new ApiException(error);
226+
// TODO: have the endpoint provide the exception contructor
227+
throw new ElasticsearchException((ErrorResponse) error);
226228

227229
} else if (endpoint instanceof BooleanEndpoint) {
228230
BooleanEndpoint<?> bep = (BooleanEndpoint<?>)endpoint;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package co.elastic.clients.elasticsearch;
21+
22+
import co.elastic.clients.elasticsearch._types.ErrorCause;
23+
import co.elastic.clients.elasticsearch._types.ErrorResponse;
24+
25+
/**
26+
* Exception thrown by API client methods when Elasticsearch could not accept or process a request.
27+
* <p>
28+
* The {@link #error()} contains the error's type and reason along with additional details that depend on the error type and
29+
* the API endpoint that was called.
30+
*/
31+
public class ElasticsearchException extends RuntimeException {
32+
33+
private final ErrorResponse response;
34+
35+
public ElasticsearchException(ErrorResponse response) {
36+
super(response.error().reason());
37+
this.response = response;
38+
}
39+
40+
/**
41+
* The error response sent by Elasticsearch
42+
*/
43+
public ErrorResponse response() {
44+
return this.response;
45+
}
46+
47+
/**
48+
* The cause of the error. Shortcut for {@code response().error()}.
49+
*/
50+
public ErrorCause error() {
51+
return this.response.error();
52+
}
53+
54+
/**
55+
* Status code returned by Elasticsearch. Shortcut for {@code response().status()}.
56+
*/
57+
public int status() {
58+
return this.response.status();
59+
}
60+
}

java-client/src/main/java/co/elastic/clients/json/JsonDataImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ class JsonDataImpl implements JsonData {
3535
this.mapper = mapper;
3636
}
3737

38+
@Override
39+
public String toString() {
40+
return value.toString();
41+
}
42+
3843
@Override
3944
public JsonValue toJson() {
4045
return toJson(null);

java-client/src/test/java/co/elastic/clients/elasticsearch/end_to_end/RequestTest.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919

2020
package co.elastic.clients.elasticsearch.end_to_end;
2121

22-
import co.elastic.clients.base.ApiException;
2322
import co.elastic.clients.base.BooleanResponse;
24-
import co.elastic.clients.base.ElasticsearchError;
23+
import co.elastic.clients.elasticsearch.ElasticsearchException;
2524
import co.elastic.clients.base.rest_client.RestClientTransport;
2625
import co.elastic.clients.base.Transport;
2726
import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
@@ -257,10 +256,10 @@ public void testRefresh() throws IOException {
257256
GetResponse<String> response = client.get(
258257
_0 -> _0.index("doesnotexist").id("reallynot"), String.class
259258
);
260-
} catch(ApiException apie) {
261-
ElasticsearchError error = (ElasticsearchError) apie.error();
262-
assertEquals(404, error.status());
263-
assertEquals("index_not_found_exception", error.error().asJsonObject().getString("type"));
259+
} catch(ElasticsearchException ex) {
260+
assertEquals(404, ex.status());
261+
assertEquals("index_not_found_exception", ex.error().type());
262+
assertEquals("doesnotexist", ex.error().metadata().get("index").to(String.class));
264263
}
265264

266265
try {
@@ -269,10 +268,9 @@ public void testRefresh() throws IOException {
269268
_0 -> _0.index("doesnotexist").id("reallynot"), String.class
270269
).get();
271270
} catch(ExecutionException ee) {
272-
ApiException apie = ((ApiException) ee.getCause());
273-
ElasticsearchError error = (ElasticsearchError) apie.error();
274-
assertEquals(404, error.status());
275-
assertEquals("index_not_found_exception", error.error().asJsonObject().getString("type"));
271+
ElasticsearchException ex = ((ElasticsearchException) ee.getCause());
272+
assertEquals(404, ex.status());
273+
assertEquals("index_not_found_exception", ex.error().type());
276274
}
277275
}
278276

java-client/src/test/java/co/elastic/clients/elasticsearch/experiments/api/FooOptRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
package co.elastic.clients.elasticsearch.experiments.api;
2121

22-
import co.elastic.clients.base.ElasticsearchError;
2322
import co.elastic.clients.base.Endpoint;
2423
import co.elastic.clients.base.SimpleEndpoint;
24+
import co.elastic.clients.elasticsearch._types.ErrorResponse;
2525
import co.elastic.clients.json.JsonpMapper;
2626
import co.elastic.clients.json.JsonpSerializable;
2727
import co.elastic.clients.util.ObjectBuilder;
@@ -257,7 +257,7 @@ public FooOptRequest build() {
257257
// PARSER = new ObjectBuilderParser<>(op);
258258
// }
259259

260-
public static final Endpoint<FooOptRequest, FooResponse, ElasticsearchError> ENDPOINT =
260+
public static final Endpoint<FooOptRequest, FooResponse, ErrorResponse> ENDPOINT =
261261
new SimpleEndpoint<FooOptRequest, FooResponse>(
262262
r -> "POST",
263263
r -> "/foo",

java-client/src/test/java/co/elastic/clients/elasticsearch/experiments/api/FooRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
package co.elastic.clients.elasticsearch.experiments.api;
2121

22-
import co.elastic.clients.base.ElasticsearchError;
2322
import co.elastic.clients.base.Endpoint;
2423
import co.elastic.clients.base.SimpleEndpoint;
24+
import co.elastic.clients.elasticsearch._types.ErrorResponse;
2525
import co.elastic.clients.elasticsearch.experiments.api.query.Query;
2626
import co.elastic.clients.json.JsonpDeserializer;
2727
import co.elastic.clients.json.JsonpMapper;
@@ -297,7 +297,7 @@ public static JsonpDeserializer<FooRequest> parser() {
297297
// Endpoint
298298
//===========================================
299299

300-
public static final Endpoint<FooRequest, FooResponse, ElasticsearchError> ENDPOINT =
300+
public static final Endpoint<FooRequest, FooResponse, ErrorResponse> ENDPOINT =
301301
new SimpleEndpoint<>(
302302
r -> "POST",
303303
r -> "/foo",

java-client/src/test/java/co/elastic/clients/elasticsearch/experiments/generics/GenericClass.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
package co.elastic.clients.elasticsearch.experiments.generics;
2121

22-
import co.elastic.clients.base.ElasticsearchError;
2322
import co.elastic.clients.base.Endpoint;
2423
import co.elastic.clients.base.SimpleEndpoint;
24+
import co.elastic.clients.elasticsearch._types.ErrorResponse;
2525
import co.elastic.clients.json.DelegatingDeserializer;
2626
import co.elastic.clients.json.JsonpDeserializer;
2727
import co.elastic.clients.json.JsonpMapper;
@@ -100,7 +100,7 @@ private static <GenParam> void setupParser(DelegatingDeserializer<Builder<GenPar
100100
}
101101

102102

103-
public static <GenParam> Endpoint<Boolean, GenericClass<GenParam>, ElasticsearchError> endpoint(
103+
public static <GenParam> Endpoint<Boolean, GenericClass<GenParam>, ErrorResponse> endpoint(
104104
JsonpDeserializer<GenParam> genParamDeserializer
105105
) {
106106
return new SimpleEndpoint<>(

0 commit comments

Comments
 (0)