Skip to content

Commit c27adfd

Browse files
committed
implement error handing for HttpClient
1 parent aea2026 commit c27adfd

File tree

2 files changed

+139
-68
lines changed

2 files changed

+139
-68
lines changed

src/main/java/com/opentok/exception/RequestException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ public RequestException(String message) {
88
super(message);
99
}
1010

11+
public RequestException(String message, Throwable cause) {
12+
super(message, cause);
13+
}
14+
1115
}

src/main/java/com/opentok/util/HttpClient.java

Lines changed: 135 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import com.ning.http.client.filter.RequestFilter;
1616

1717
import com.opentok.constants.Version;
18+
import com.opentok.exception.OpenTokException;
19+
import com.opentok.exception.RequestException;
1820

1921
public class HttpClient extends AsyncHttpClient {
2022

@@ -27,7 +29,7 @@ private HttpClient(Builder builder) {
2729
this.apiUrl = builder.apiUrl;
2830
}
2931

30-
public String createSession(Map<String, Collection<String>> params) {
32+
public String createSession(Map<String, Collection<String>> params) throws RequestException {
3133
Future<Response> request = null;
3234
String responseString = null;
3335
Response response = null;
@@ -38,58 +40,73 @@ public String createSession(Map<String, Collection<String>> params) {
3840
.setParameters(paramsString)
3941
.execute();
4042
} catch (IOException e) {
41-
// TODO: throw OpenTokException
42-
e.printStackTrace();
43+
throw new RequestException("Could not create an OpenTok Session", e);
4344
}
4445

4546
try {
4647
response = request.get();
47-
// TODO: check response code
48-
responseString = response.getResponseBody();
48+
switch (response.getStatusCode()) {
49+
case 200:
50+
responseString = response.getResponseBody();
51+
break;
52+
default:
53+
throw new RequestException("Could not create an OpenTok Session. The server response was invalid." +
54+
" response code: " + response.getStatusCode());
55+
}
56+
57+
// if we only wanted Java 7 and above, we could DRY this into one catch clause
4958
} catch (InterruptedException e) {
50-
// TODO: throw OpenTokException
51-
e.printStackTrace();
59+
throw new RequestException("Could not create an OpenTok Session", e);
5260
} catch (ExecutionException e) {
53-
// TODO: throw OpenTokException
54-
e.printStackTrace();
61+
throw new RequestException("Could not create an OpenTok Session", e);
5562
} catch (IOException e) {
56-
// TODO: throw OpenTokException
57-
e.printStackTrace();
63+
throw new RequestException("Could not create an OpenTok Session", e);
5864
}
5965
return responseString;
6066
}
6167

62-
public String getArchive(String archiveId) {
68+
public String getArchive(String archiveId) throws RequestException {
6369
String responseString = null;
6470
Future<Response> request = null;
6571
String url = this.apiUrl + "/v2/partner/" + this.apiKey + "/archive/" + archiveId;
6672

6773
try {
6874
request = this.prepareGet(url).execute();
6975
} catch (IOException e) {
70-
// TODO: throw OpenTokException
71-
e.printStackTrace();
76+
throw new RequestException("Could not get an OpenTok Archive", e);
7277
}
7378

7479
try {
7580
Response response = request.get();
76-
// TODO: check response code
77-
responseString = response.getResponseBody();
81+
switch (response.getStatusCode()) {
82+
case 200:
83+
responseString = response.getResponseBody();
84+
break;
85+
case 400:
86+
throw new RequestException("Could not get an OpenTok Archive. The archiveId was invalid. " +
87+
"archiveId: " + archiveId);
88+
case 403:
89+
throw new RequestException("Could not get an OpenTok Archive. The request was not authorized.");
90+
case 500:
91+
throw new RequestException("Could not get an OpenTok Archive. A server error occurred.");
92+
default:
93+
throw new RequestException("Could not get an OpenTok Archive. The server response was invalid." +
94+
" response code: " + response.getStatusCode());
95+
}
96+
97+
// if we only wanted Java 7 and above, we could DRY this into one catch clause
7898
} catch (InterruptedException e) {
79-
// TODO: throw OpenTokException
80-
e.printStackTrace();
99+
throw new RequestException("Could not get an OpenTok Archive", e);
81100
} catch (ExecutionException e) {
82-
// TODO: throw OpenTokException
83-
e.printStackTrace();
101+
throw new RequestException("Could not get an OpenTok Archive", e);
84102
} catch (IOException e) {
85-
// TODO: throw OpenTokException
86-
e.printStackTrace();
103+
throw new RequestException("Could not get an OpenTok Archive", e);
87104
}
88105

89106
return responseString;
90107
}
91108

92-
public String getArchives(int offset, int count) {
109+
public String getArchives(int offset, int count) throws RequestException {
93110
String responseString = null;
94111
Future<Response> request = null;
95112
// TODO: maybe use a StringBuilder?
@@ -107,29 +124,37 @@ public String getArchives(int offset, int count) {
107124
try {
108125
request = this.prepareGet(url).execute();
109126
} catch (IOException e) {
110-
// TODO: throw OpenTokException
111-
e.printStackTrace();
127+
throw new RequestException("Could not get OpenTok Archives", e);
112128
}
113129

114130
try {
115131
Response response = request.get();
116-
// TODO: check response code
117-
responseString = response.getResponseBody();
132+
switch (response.getStatusCode()) {
133+
case 200:
134+
responseString = response.getResponseBody();
135+
break;
136+
case 403:
137+
throw new RequestException("Could not get OpenTok Archives. The request was not authorized.");
138+
case 500:
139+
throw new RequestException("Could not get OpenTok Archives. A server error occurred.");
140+
default:
141+
throw new RequestException("Could not get an OpenTok Archive. The server response was invalid." +
142+
" response code: " + response.getStatusCode());
143+
}
144+
145+
// if we only wanted Java 7 and above, we could DRY this into one catch clause
118146
} catch (InterruptedException e) {
119-
// TODO: throw OpenTokException
120-
e.printStackTrace();
147+
throw new RequestException("Could not get OpenTok Archives", e);
121148
} catch (ExecutionException e) {
122-
// TODO: throw OpenTokException
123-
e.printStackTrace();
149+
throw new RequestException("Could not get OpenTok Archives", e);
124150
} catch (IOException e) {
125-
// TODO: throw OpenTokException
126-
e.printStackTrace();
151+
throw new RequestException("Could not get OpenTok Archives", e);
127152
}
128153

129154
return responseString;
130155
}
131156

132-
public String startArchive(String sessionId, String name) {
157+
public String startArchive(String sessionId, String name) throws OpenTokException, RequestException {
133158
String responseString = null;
134159
Future<Response> request = null;
135160
String requestBody = null;
@@ -145,37 +170,50 @@ public String startArchive(String sessionId, String name) {
145170
try {
146171
requestBody = mapper.writeValueAsString(jsonBody);
147172
} catch (JsonProcessingException e) {
148-
// TODO: throw OpenTokException
149-
e.printStackTrace();
173+
throw new OpenTokException("Could not start an OpenTok Archive. The JSON body encoding failed.", e);
150174
}
151175
try {
152176
request = this.preparePost(url)
153177
.setBody(requestBody)
154178
.setHeader("Content-Type", "application/json")
155179
.execute();
156180
} catch (IOException e) {
157-
// TODO: throw OpenTokException
158-
e.printStackTrace();
181+
throw new RequestException("Could not start an OpenTok Archive.", e);
159182
}
160183

161184
try {
162185
Response response = request.get();
163-
// TODO: check response code
164-
responseString = response.getResponseBody();
186+
switch (response.getStatusCode()) {
187+
case 200:
188+
responseString = response.getResponseBody();
189+
break;
190+
case 403:
191+
throw new RequestException("Could not start an OpenTok Archive. The request was not authorized.");
192+
case 404:
193+
throw new RequestException("Could not start an OpenTok Archive. The sessionId does not exist. " +
194+
"sessionId = " + sessionId);
195+
case 409:
196+
throw new RequestException("Could not start an OpenTok Archive. The session is either " +
197+
"peer-to-peer or already recording. sessionId = " + sessionId);
198+
case 500:
199+
throw new RequestException("Could not start an OpenTok Archive. A server error occurred.");
200+
default:
201+
throw new RequestException("Could not start an OpenTok Archive. The server response was invalid." +
202+
" response code: " + response.getStatusCode());
203+
}
204+
205+
// if we only wanted Java 7 and above, we could DRY this into one catch clause
165206
} catch (InterruptedException e) {
166-
// TODO: throw OpenTokException
167-
e.printStackTrace();
207+
throw new RequestException("Could not start an OpenTok Archive.", e);
168208
} catch (ExecutionException e) {
169-
// TODO: throw OpenTokException
170-
e.printStackTrace();
209+
throw new RequestException("Could not start an OpenTok Archive.", e);
171210
} catch (IOException e) {
172-
// TODO: throw OpenTokException
173-
e.printStackTrace();
211+
throw new RequestException("Could not start an OpenTok Archive.", e);
174212
}
175213
return responseString;
176214
}
177215

178-
public String stopArchive(String archiveId) {
216+
public String stopArchive(String archiveId) throws RequestException {
179217
String responseString = null;
180218
Future<Response> request = null;
181219
// TODO: maybe use a StringBuilder?
@@ -184,52 +222,81 @@ public String stopArchive(String archiveId) {
184222
try {
185223
request = this.preparePost(url).execute();
186224
} catch (IOException e) {
187-
// TODO: throw OpenTokException
188-
e.printStackTrace();
225+
throw new RequestException("Could not stop an OpenTok Archive. archiveId = " + archiveId, e);
189226
}
190227

191228
try {
192229
Response response = request.get();
193-
// TODO: check response code
194-
responseString = response.getResponseBody();
230+
switch (response.getStatusCode()) {
231+
case 200:
232+
responseString = response.getResponseBody();
233+
break;
234+
case 400:
235+
// NOTE: the REST api spec talks about sessionId and action, both of which aren't required.
236+
// see: https://github.com/opentok/OpenTok-2.0-archiving-samples/blob/master/REST-API.md#stop_archive
237+
throw new RequestException("Could not stop an OpenTok Archive.");
238+
case 403:
239+
throw new RequestException("Could not stop an OpenTok Archive. The request was not authorized.");
240+
case 404:
241+
throw new RequestException("Could not stop an OpenTok Archive. The archiveId does not exist. " +
242+
"archiveId = " + archiveId);
243+
case 409:
244+
throw new RequestException("Could not stop an OpenTok Archive. The archive is not being recorded. " +
245+
"archiveId = " + archiveId);
246+
case 500:
247+
throw new RequestException("Could not stop an OpenTok Archive. A server error occurred.");
248+
default:
249+
throw new RequestException("Could not stop an OpenTok Archive. The server response was invalid." +
250+
" response code: " + response.getStatusCode());
251+
}
252+
253+
// if we only wanted Java 7 and above, we could DRY this into one catch clause
195254
} catch (InterruptedException e) {
196-
// TODO: throw OpenTokException
197-
e.printStackTrace();
255+
throw new RequestException("Could not stop an OpenTok Archive.", e);
198256
} catch (ExecutionException e) {
199-
// TODO: throw OpenTokException
200-
e.printStackTrace();
257+
throw new RequestException("Could not stop an OpenTok Archive.", e);
201258
} catch (IOException e) {
202-
// TODO: throw OpenTokException
203-
e.printStackTrace();
259+
throw new RequestException("Could not stop an OpenTok Archive.", e);
204260
}
205261
return responseString;
206262
}
207263

208-
public String deleteArchive(String archiveId) {
264+
public String deleteArchive(String archiveId) throws RequestException {
209265
String responseString = null;
210266
Future<Response> request = null;
211267
String url = this.apiUrl + "/v2/partner/" + this.apiKey + "/archive/" + archiveId;
212268

213269
try {
214270
request = this.prepareDelete(url).execute();
215271
} catch (IOException e) {
216-
// TODO: throw OpenTokException
217-
e.printStackTrace();
272+
throw new RequestException("Could not delete an OpenTok Archive. archiveId = " + archiveId, e);
218273
}
219274

220275
try {
221276
Response response = request.get();
222-
// TODO: check response code
223-
responseString = response.getResponseBody();
277+
switch (response.getStatusCode()) {
278+
case 204:
279+
responseString = response.getResponseBody();
280+
break;
281+
case 403:
282+
throw new RequestException("Could not delete an OpenTok Archive. The request was not authorized.");
283+
case 409:
284+
throw new RequestException("Could not delete an OpenTok Archive. The status was not \"uploaded\"," +
285+
" \"available\", or \"deleted\". archiveId = " + archiveId);
286+
case 500:
287+
throw new RequestException("Could not delete an OpenTok Archive. A server error occurred.");
288+
default:
289+
throw new RequestException("Could not get an OpenTok Archive. The server response was invalid." +
290+
" response code: " + response.getStatusCode());
291+
}
292+
293+
// if we only wanted Java 7 and above, we could DRY this into one catch clause
224294
} catch (InterruptedException e) {
225-
// TODO: throw OpenTokException
226-
e.printStackTrace();
295+
throw new RequestException("Could not delete an OpenTok Archive. archiveId = " + archiveId, e);
227296
} catch (ExecutionException e) {
228-
// TODO: throw OpenTokException
229-
e.printStackTrace();
297+
throw new RequestException("Could not delete an OpenTok Archive. archiveId = " + archiveId, e);
230298
} catch (IOException e) {
231-
// TODO: throw OpenTokException
232-
e.printStackTrace();
299+
throw new RequestException("Could not delete an OpenTok Archive. archiveId = " + archiveId, e);
233300
}
234301

235302
return responseString;

0 commit comments

Comments
 (0)