Skip to content

Commit b1c9112

Browse files
committed
Implement MeiliSearchApiException
1 parent 2f0d66d commit b1c9112

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed
Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,40 @@
1-
package com.meilisearch.sdk;
1+
package com.meilisearch.sdk.exceptions;
2+
3+
import com.meilisearch.sdk.json.GsonJsonHandler;
24

35
public class MeiliSearchApiException extends MeiliSearchException {
46

7+
/**
8+
* This is class wraps errors sent by MeiliSearch API
9+
*/
10+
11+
private GsonJsonHandler gson = new GsonJsonHandler();
12+
13+
private class ApiError {
14+
public String message;
15+
public String errorCode;
16+
public String errorType;
17+
public String errorLink;
18+
}
19+
20+
public MeiliSearchApiException (String errorMessage) throws Exception {
21+
super(errorMessage);
22+
ApiError error = this.gson.decode(
23+
errorMessage,
24+
ApiError.class
25+
);
26+
this.setErrorMessage(error.message);
27+
this.setErrorCode(error.errorCode);
28+
this.setErrorType(error.errorType);
29+
this.setErrorLink(error.errorLink);
30+
}
31+
32+
public String toString() {
33+
return this.getClass().getName()
34+
+ ". Error message: " + this.errorMessage
35+
+ ". Error code: " + this.errorCode
36+
+ ". Error documentation: " + this.errorLink
37+
+ ". Error type: " + this.errorType
38+
+ ".";
39+
}
540
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
package com.meilisearch.sdk;
1+
package com.meilisearch.sdk.exceptions;
22

33
public class MeiliSearchCommunicationException extends MeiliSearchException {
44

5+
public MeiliSearchCommunicationException (String errorMessage) {
6+
super(errorMessage);
7+
}
8+
59
}
Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1-
package com.meilisearch.sdk;
1+
package com.meilisearch.sdk.exceptions;
22

33
public class MeiliSearchException extends Exception {
44

55
/**
66
* This is a generic class for MeiliSearch Exception handling
77
*/
88

9-
String message;
9+
String errorMessage;
10+
String errorType;
1011
String errorCode;
1112
String errorLink;
1213

14+
public MeiliSearchException (String errorMessage) {
15+
super(errorMessage);
16+
this.setErrorMessage(errorMessage);
17+
}
18+
1319
public String getMessage () {
14-
return this.message;
20+
return this.errorMessage;
21+
}
22+
23+
public String getErrorType () {
24+
return this.errorType;
1525
}
1626

1727
public String getErrorCode () {
@@ -22,8 +32,12 @@ public String getErrorLink () {
2232
return this.errorLink;
2333
}
2434

25-
public void setMessage (String message) {
26-
this.message = message;
35+
public void setErrorMessage (String errorMessage) {
36+
this.errorMessage = errorMessage;
37+
}
38+
39+
public void setErrorType (String errorType) {
40+
this.errorType = errorType;
2741
}
2842

2943
public void setErrorCode (String errorCode) {
@@ -33,4 +47,8 @@ public void setErrorCode (String errorCode) {
3347
public void setErrorLink (String errorLink) {
3448
this.errorLink = errorLink;
3549
}
50+
51+
public String toString() {
52+
return this.getClass().getName() + ". Error message: " + this.errorMessage + ".";
53+
}
3654
}

0 commit comments

Comments
 (0)