-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStreamException.java
More file actions
198 lines (175 loc) · 6.41 KB
/
Copy pathStreamException.java
File metadata and controls
198 lines (175 loc) · 6.41 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package io.getstream.exceptions;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.Map;
import lombok.Data;
import lombok.Getter;
import okhttp3.Response;
import okhttp3.ResponseBody;
public class StreamException extends Exception {
private static final long serialVersionUID = 1L;
@Getter private ResponseData responseData;
public StreamException(String message, ResponseData responseData) {
super(message);
this.responseData = responseData;
}
public StreamException(String message, Throwable t) {
super(message, t);
}
public StreamException(Throwable t) {
super(t);
}
// Package-private: subclasses construct via this with a parsed ResponseData mirror.
StreamException(String message, Throwable cause, ResponseData responseData) {
super(message, cause);
this.responseData = responseData;
}
/**
* Builds a StreamException to signal an issue
*
* @param issue the issue
* @return the StreamException
*/
public static StreamException build(String issue) {
return new StreamException(issue, (Throwable) null);
}
/**
* Builds a typed API exception from the Stream API error body.
*
* <p>Parseable {@code APIError} envelope → {@link StreamApiException}; unparseable body but HTTP
* layer succeeded → {@code StreamApiException} with {@code code=0} and the raw body preserved.
*
* <p>This overload does not see the status code; callers that have a {@link Response} should
* prefer {@link #build(Response)} so 429 is routed to {@link StreamRateLimitException} and {@code
* statusCode} is preserved.
*/
public static StreamException build(ResponseBody responseBody) {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
try {
String responseBodyString = responseBody.string();
try {
ResponseData responseData = objectMapper.readValue(responseBodyString, ResponseData.class);
int status = responseData.getStatusCode() != null ? responseData.getStatusCode() : 0;
return apiExceptionFromResponseData(responseData, responseBodyString, status, null);
} catch (JsonProcessingException e) {
return new StreamApiException(
"failed to parse error response", 0, 0, null, false, responseBodyString, null, null, e);
}
} catch (IOException e) {
return new StreamException(e);
}
}
/**
* Builds a typed API exception from an HTTP response. 429 → {@link StreamRateLimitException} with
* {@code Retry-After} parsed per RFC 7231 §7.1.3 (integer seconds or HTTP-date). Other 4xx/5xx →
* {@link StreamApiException}.
*/
public static StreamException build(Response httpResponse) {
int status = httpResponse.code();
String bodyString = "";
ResponseData parsed = null;
Throwable parseCause = null;
ResponseBody errorBody = httpResponse.body();
if (errorBody != null) {
try {
bodyString = errorBody.string();
} catch (IOException e) {
// Body unreadable — treat as empty.
parseCause = e;
}
if (!bodyString.isEmpty()) {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
try {
parsed = objectMapper.readValue(bodyString, ResponseData.class);
} catch (JsonProcessingException e) {
parseCause = e;
}
}
}
if (parsed == null) {
String msg =
parseCause != null
? "failed to parse error response"
: String.format("Unexpected server response code %d", status);
if (status == 429) {
return new StreamRateLimitException(
msg,
status,
0,
null,
false,
bodyString,
null,
null,
RetryAfterParser.parse(httpResponse.header("Retry-After")),
parseCause);
}
return new StreamApiException(
msg, status, 0, null, false, bodyString, null, null, parseCause);
}
if (status == 429) {
return new StreamRateLimitException(
parsed.getMessage() != null ? parsed.getMessage() : "rate limited",
status,
parsed.getCode() != null ? parsed.getCode() : 0,
parsed.getExceptionFields(),
parsed.getUnrecoverable() != null ? parsed.getUnrecoverable() : false,
bodyString,
parsed.getMoreInfo(),
parsed.getDetails(),
RetryAfterParser.parse(httpResponse.header("Retry-After")),
null);
}
return apiExceptionFromResponseData(parsed, bodyString, status, null);
}
/**
* Builds a StreamException when an exception occurs calling the API.
*
* <p>Historic factory preserved for back-compat. The HTTP-call path now classifies transport
* failures directly via {@link StreamTransportException#fromIOException(IOException)} so this
* factory no longer auto-routes; it simply wraps the cause.
*
* @param t the underlying exception
* @return the StreamException
*/
public static StreamException build(Throwable t) {
return new StreamException(t);
}
private static StreamApiException apiExceptionFromResponseData(
ResponseData rd, String rawBody, int statusCode, Throwable cause) {
return new StreamApiException(
rd.getMessage() != null ? rd.getMessage() : "",
statusCode,
rd.getCode() != null ? rd.getCode() : 0,
rd.getExceptionFields(),
rd.getUnrecoverable() != null ? rd.getUnrecoverable() : false,
rawBody != null ? rawBody : "",
rd.getMoreInfo(),
rd.getDetails(),
cause);
}
@Data
public static class ResponseData {
@JsonProperty("code")
private Integer code;
@JsonProperty("message")
private String message;
@JsonProperty("exception_fields")
private Map<String, String> exceptionFields;
@JsonProperty("StatusCode")
private Integer statusCode;
@JsonProperty("duration")
private String duration;
@JsonProperty("more_info")
private String moreInfo;
@JsonProperty("details")
private Object details;
@JsonProperty("unrecoverable")
private Boolean unrecoverable;
}
}