Skip to content

Commit 83131f3

Browse files
committed
Normalize exception
1 parent 6fcca4d commit 83131f3

9 files changed

+110
-49
lines changed

src/main/java/com/meilisearch/sdk/KeysHandler.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.meilisearch.sdk;
22

3-
import com.meilisearch.sdk.exceptions.MeiliSearchApiException;
43
import com.meilisearch.sdk.model.Key;
54
import com.meilisearch.sdk.model.Result;
65

@@ -28,7 +27,7 @@ public KeysHandler(Config config) {
2827
* @return Key instance
2928
* @throws Exception if client request causes an error
3029
*/
31-
public Key getKey(String uid) throws Exception, MeiliSearchApiException {
30+
public Key getKey(String uid) throws Exception {
3231
String urlPath = "/keys/" + uid;
3332
return meilisearchHttpRequest.jsonHandler.decode(
3433
this.meilisearchHttpRequest.get(urlPath), Key.class);

src/main/java/com/meilisearch/sdk/MeiliSearchHttpRequest.java

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.meilisearch.sdk.exceptions.APIError;
44
import com.meilisearch.sdk.exceptions.MeiliSearchApiException;
5+
import com.meilisearch.sdk.exceptions.MeiliSearchCommunicationException;
56
import com.meilisearch.sdk.http.AbstractHttpClient;
67
import com.meilisearch.sdk.http.DefaultHttpClient;
78
import com.meilisearch.sdk.http.factory.BasicRequestFactory;
@@ -10,6 +11,7 @@
1011
import com.meilisearch.sdk.http.response.HttpResponse;
1112
import com.meilisearch.sdk.json.GsonJsonHandler;
1213
import com.meilisearch.sdk.json.JsonHandler;
14+
import java.io.IOException;
1315
import java.util.Collections;
1416

1517
/** The HTTP requests for the different functions to be done through Meilisearch */
@@ -63,14 +65,19 @@ public String get(String api) throws Exception, MeiliSearchApiException {
6365
* @throws MeiliSearchApiException if the response is an error
6466
*/
6567
String get(String api, String param) throws Exception, MeiliSearchApiException {
66-
HttpResponse<?> httpResponse =
67-
this.client.get(
68-
factory.create(HttpMethod.GET, api + param, Collections.emptyMap(), null));
69-
if (httpResponse.getStatusCode() >= 400) {
70-
throw new MeiliSearchApiException(
71-
jsonHandler.decode(httpResponse.getContent(), APIError.class));
68+
try {
69+
HttpResponse<?> httpResponse =
70+
this.client.get(
71+
factory.create(
72+
HttpMethod.GET, api + param, Collections.emptyMap(), null));
73+
if (httpResponse.getStatusCode() >= 400) {
74+
throw new MeiliSearchApiException(
75+
jsonHandler.decode(httpResponse.getContent(), APIError.class));
76+
}
77+
return new String(httpResponse.getContentAsBytes());
78+
} catch (IOException e) {
79+
throw new MeiliSearchCommunicationException(e);
7280
}
73-
return new String(httpResponse.getContentAsBytes());
7481
}
7582

7683
/**
@@ -83,14 +90,18 @@ String get(String api, String param) throws Exception, MeiliSearchApiException {
8390
* @throws MeiliSearchApiException if the response is an error
8491
*/
8592
<T> String post(String api, T body) throws Exception, MeiliSearchApiException {
86-
HttpResponse<?> httpResponse =
87-
this.client.post(
88-
factory.create(HttpMethod.POST, api, Collections.emptyMap(), body));
89-
if (httpResponse.getStatusCode() >= 400) {
90-
throw new MeiliSearchApiException(
91-
jsonHandler.decode(httpResponse.getContent(), APIError.class));
93+
try {
94+
HttpResponse<?> httpResponse =
95+
this.client.post(
96+
factory.create(HttpMethod.POST, api, Collections.emptyMap(), body));
97+
if (httpResponse.getStatusCode() >= 400) {
98+
throw new MeiliSearchApiException(
99+
jsonHandler.decode(httpResponse.getContent(), APIError.class));
100+
}
101+
return new String(httpResponse.getContentAsBytes());
102+
} catch (IOException e) {
103+
throw new MeiliSearchCommunicationException(e);
92104
}
93-
return new String(httpResponse.getContentAsBytes());
94105
}
95106

96107
/**
@@ -103,13 +114,18 @@ <T> String post(String api, T body) throws Exception, MeiliSearchApiException {
103114
* @throws MeiliSearchApiException if the response is an error
104115
*/
105116
<T> String put(String api, T body) throws Exception, MeiliSearchApiException {
106-
HttpResponse<?> httpResponse =
107-
this.client.put(factory.create(HttpMethod.PUT, api, Collections.emptyMap(), body));
108-
if (httpResponse.getStatusCode() >= 400) {
109-
throw new MeiliSearchApiException(
110-
jsonHandler.decode(httpResponse.getContent(), APIError.class));
117+
try {
118+
HttpResponse<?> httpResponse =
119+
this.client.put(
120+
factory.create(HttpMethod.PUT, api, Collections.emptyMap(), body));
121+
if (httpResponse.getStatusCode() >= 400) {
122+
throw new MeiliSearchApiException(
123+
jsonHandler.decode(httpResponse.getContent(), APIError.class));
124+
}
125+
return new String(httpResponse.getContentAsBytes());
126+
} catch (IOException e) {
127+
throw new MeiliSearchCommunicationException(e);
111128
}
112-
return new String(httpResponse.getContentAsBytes());
113129
}
114130

115131
/**
@@ -121,13 +137,17 @@ <T> String put(String api, T body) throws Exception, MeiliSearchApiException {
121137
* @throws MeiliSearchApiException if the response is an error
122138
*/
123139
String delete(String api) throws Exception, MeiliSearchApiException {
124-
HttpResponse<?> httpResponse =
125-
this.client.put(
126-
factory.create(HttpMethod.DELETE, api, Collections.emptyMap(), null));
127-
if (httpResponse.getStatusCode() >= 400) {
128-
throw new MeiliSearchApiException(
129-
jsonHandler.decode(httpResponse.getContent(), APIError.class));
140+
try {
141+
HttpResponse<?> httpResponse =
142+
this.client.put(
143+
factory.create(HttpMethod.DELETE, api, Collections.emptyMap(), null));
144+
if (httpResponse.getStatusCode() >= 400) {
145+
throw new MeiliSearchApiException(
146+
jsonHandler.decode(httpResponse.getContent(), APIError.class));
147+
}
148+
return new String(httpResponse.getContentAsBytes());
149+
} catch (IOException e) {
150+
throw new MeiliSearchCommunicationException(e);
130151
}
131-
return new String(httpResponse.getContentAsBytes());
132152
}
133153
}

src/main/java/com/meilisearch/sdk/TasksHandler.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.meilisearch.sdk;
22

3-
import com.meilisearch.sdk.exceptions.MeiliSearchApiException;
43
import com.meilisearch.sdk.model.Result;
54
import com.meilisearch.sdk.model.Task;
65
import java.util.Date;
@@ -61,7 +60,7 @@ public Result<Task> getTasks(String indexUid) throws Exception {
6160
* @return Task instance
6261
* @throws Exception if client request causes an error
6362
*/
64-
public Task getTask(int taskUid) throws Exception, MeiliSearchApiException {
63+
public Task getTask(int taskUid) throws Exception {
6564
String urlPath = "/tasks/" + taskUid;
6665
return meilisearchHttpRequest.jsonHandler.decode(
6766
this.meilisearchHttpRequest.get(urlPath), Task.class);

src/main/java/com/meilisearch/sdk/exceptions/APIError.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,22 @@ public APIError(String message, String code, String type, String link) {
2121
this.type = type;
2222
this.link = link;
2323
}
24+
25+
@Override
26+
public String toString() {
27+
return "APIError{"
28+
+ "message='"
29+
+ message
30+
+ '\''
31+
+ ", code='"
32+
+ code
33+
+ '\''
34+
+ ", type='"
35+
+ type
36+
+ '\''
37+
+ ", link='"
38+
+ link
39+
+ '\''
40+
+ '}';
41+
}
2442
}

src/main/java/com/meilisearch/sdk/exceptions/MeiliSearchApiException.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.meilisearch.sdk.exceptions;
22

3-
public class MeiliSearchApiException extends MeiliSearchRuntimeException {
3+
public class MeiliSearchApiException extends MeiliSearchException {
44

55
private final APIError error;
66

@@ -24,4 +24,9 @@ public String getType() {
2424
public String getLink() {
2525
return error.getLink();
2626
}
27+
28+
@Override
29+
public String toString() {
30+
return "MeiliSearch ApiException: {" + "error=" + error + '}';
31+
}
2732
}
Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,29 @@
11
package com.meilisearch.sdk.exceptions;
22

3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
@Getter
7+
@Setter
38
/** This is class wraps MeiliSearchExceptions dealing with Communication errors */
49
public class MeiliSearchCommunicationException extends MeiliSearchException {
510

6-
public MeiliSearchCommunicationException(String errorMessage) {
7-
super(errorMessage);
11+
String error;
12+
13+
public MeiliSearchCommunicationException() {}
14+
15+
public MeiliSearchCommunicationException(Exception e) {
16+
super(e);
17+
this.error = e.toString();
18+
}
19+
20+
public MeiliSearchCommunicationException(String error) {
21+
super(error);
22+
this.setError(error);
23+
}
24+
25+
@Override
26+
public String toString() {
27+
return "MeiliSearch CommunicationException: {" + "error=" + this.error + '}';
828
}
929
}

src/main/java/com/meilisearch/sdk/exceptions/MeiliSearchException.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,22 @@ public class MeiliSearchException extends Exception {
1414
String code;
1515
String link;
1616

17+
public MeiliSearchException() {}
18+
1719
public MeiliSearchException(String message) {
1820
super(message);
1921
this.setMessage(message);
2022
}
23+
24+
public MeiliSearchException(Exception e) {
25+
super(e);
26+
}
27+
28+
public MeiliSearchException(Throwable cause) {
29+
super(cause);
30+
}
31+
32+
public String toString() {
33+
return this.getClass().getName() + ". Error message: " + this.message + ".";
34+
}
2135
}

src/main/java/com/meilisearch/sdk/exceptions/MeiliSearchRuntimeException.java

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

src/main/java/com/meilisearch/sdk/http/factory/BasicRequestFactory.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.meilisearch.sdk.http.factory;
22

3-
import com.meilisearch.sdk.exceptions.MeiliSearchRuntimeException;
43
import com.meilisearch.sdk.http.request.BasicHttpRequest;
54
import com.meilisearch.sdk.http.request.HttpMethod;
65
import com.meilisearch.sdk.http.request.HttpRequest;
@@ -24,7 +23,7 @@ public <T> HttpRequest<?> create(
2423
headers,
2524
content == null ? null : this.jsonHandler.encode(content));
2625
} catch (Exception e) {
27-
throw new MeiliSearchRuntimeException(e);
26+
throw new RuntimeException(e);
2827
}
2928
}
3029
}

0 commit comments

Comments
 (0)