@@ -40,6 +40,7 @@ public class RestConnection {
40
40
private static final Base64 BASE64 = new Base64 ();
41
41
private static final String DEFAULT_CHARSET = "UTF-8" ;
42
42
43
+ private static final int SUCCESS_RESPONSE_STATUS_CODE = 200 ;
43
44
private static final int UNAUTHORIZED_RESPONSE_STATUS_CODE = 401 ;
44
45
private static final int ACCESS_FORBIDDEN_RESPONSE_STATUS_CODE = 403 ;
45
46
@@ -221,6 +222,12 @@ private void sendRequest(HttpURLConnection conn, String data, Response response)
221
222
String msg = conn .getResponseMessage ();
222
223
response .setResponseMessage (msg );
223
224
225
+ if (code == UNAUTHORIZED_RESPONSE_STATUS_CODE ) {
226
+ throw new SparkPostAuthorizationFailedException ();
227
+ } else if (code == ACCESS_FORBIDDEN_RESPONSE_STATUS_CODE ) {
228
+ throw new SparkPostAccessForbiddenException ();
229
+ }
230
+
224
231
} catch (IOException ex ) {
225
232
throw new SparkPostException ("Connection error:" + ex .toString ());
226
233
}
@@ -241,8 +248,13 @@ private Response receiveResponse(HttpURLConnection conn, Response response) thro
241
248
sb .append (line );
242
249
}
243
250
244
- response .setResponseBody (sb .toString ());
245
- response .setRequestId (conn .getHeaderField ("X-SparkPost-Request-Id" ));
251
+ if (response .getResponseCode () == SUCCESS_RESPONSE_STATUS_CODE ) {
252
+ response .setResponseBody (sb .toString ());
253
+ response .setRequestId (conn .getHeaderField ("X-SparkPost-Request-Id" ));
254
+ } else {
255
+ throw new SparkPostErrorServerResponseException (sb .toString (), response .getResponseCode ());
256
+ }
257
+
246
258
} catch (FileNotFoundException ex ) {
247
259
// We get here if the connection was closed:
248
260
// There are cases in REST where the server won't return a response
@@ -269,16 +281,10 @@ private Response receiveResponse(HttpURLConnection conn, Response response) thro
269
281
logger .error ("Server Response:" + response );
270
282
}
271
283
272
- if (response .getResponseCode () == UNAUTHORIZED_RESPONSE_STATUS_CODE ) {
273
- throw new SparkPostAuthorizationFailedException ();
274
- } else if (response .getResponseCode () == ACCESS_FORBIDDEN_RESPONSE_STATUS_CODE ) {
275
- throw new SparkPostAccessForbiddenException ();
276
- } else {
277
- throw new SparkPostErrorServerResponseException (
278
- "Error reading server response: " + ex .toString () + ": " + sb .toString () + "(" + response .getResponseMessage () + ")" ,
279
- response .getResponseCode ()
280
- );
281
- }
284
+ throw new SparkPostErrorServerResponseException (
285
+ "Error reading server response: " + ex .toString () + ": " + sb .toString () + "(" + response .getResponseMessage () + ")" ,
286
+ response .getResponseCode ()
287
+ );
282
288
}
283
289
return response ;
284
290
0 commit comments