forked from Petersoj/alpaca-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlpacaClientException.java
138 lines (115 loc) · 4.07 KB
/
AlpacaClientException.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package net.jacobpeterson.alpaca.rest;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import okhttp3.Response;
import okhttp3.ResponseBody;
public class AlpacaClientException extends Exception {
private static final String CODE_KEY = "code";
private static final String MESSAGE_KEY = "message";
private String responseBody;
private Integer responseStatusCode;
private String responseStatusMessage;
private Integer apiResponseCode;
private String apiResponseMessage;
/**
* Instantiates a new {@link AlpacaClientException}.
*
* @param message the message
*/
public AlpacaClientException(String message) {
super(message);
}
/**
* Instantiates a new {@link AlpacaClientException}.
*
* @param cause the cause {@link Throwable}
*/
public AlpacaClientException(Throwable cause) {
super(cause);
}
/**
* Instantiates a new {@link AlpacaClientException}.
*
* @param message the message
* @param cause the cause {@link Throwable}
*/
public AlpacaClientException(String message, Throwable cause) {
super(message, cause);
}
/**
* Instantiates a new {@link AlpacaClientException}.
*
* @param response the {@link Response} containing an API response code and message JSON
*/
protected AlpacaClientException(Response response) {
responseStatusCode = response.code();
responseStatusMessage = response.message();
try (ResponseBody clientResponseBody = response.body()) {
if (clientResponseBody != null) {
this.responseBody = clientResponseBody.string();
}
} catch (Exception ignored) {}
}
@Override
public String getMessage() {
if (responseBody == null) {
return super.getMessage();
} else {
return parseAPIErrorResponse();
}
}
/**
* Parses an API error response of the following format:
* <pre>
* {
* "code": 505,
* "message": "Error message"
* }
* </pre>
* and sets {@link #apiResponseCode} and {@link #apiResponseMessage} accordingly.
*
* @return a formatted message of the error response
*/
private String parseAPIErrorResponse() {
try {
JsonElement responseJsonElement = JsonParser.parseString(responseBody);
if (responseJsonElement instanceof JsonObject) {
JsonObject responseJsonObject = responseJsonElement.getAsJsonObject();
if (responseJsonObject.has(CODE_KEY)) {
apiResponseCode = responseJsonObject.get(CODE_KEY).getAsInt();
}
if (responseJsonObject.has(MESSAGE_KEY)) {
apiResponseMessage = responseJsonObject.get(MESSAGE_KEY).getAsString();
}
}
// Just use the response JSON if the message could not be parsed.
if (apiResponseMessage == null) {
apiResponseMessage = responseJsonElement.toString();
}
} catch (Exception ignored) {}
// Build message
StringBuilder messageBuilder = new StringBuilder();
messageBuilder.append("Status Code: ").append(responseStatusCode);
messageBuilder.append(", Status Message: \"").append(responseStatusMessage).append("\"");
if (apiResponseCode != null) {
messageBuilder.append(", API Response Code: ").append(apiResponseCode);
}
if (apiResponseMessage != null) {
messageBuilder.append(", API Response Message: \"").append(apiResponseMessage).append("\"");
}
return messageBuilder.toString();
}
public Integer getResponseStatusCode() {
return responseStatusCode;
}
public String getResponseStatusMessage() {
return responseStatusMessage;
}
public Integer getAPIResponseCode() {
return apiResponseCode;
}
public String getAPIResponseMessage() {
return apiResponseMessage;
}
}