2020import com .sparkpost .Client ;
2121import com .sparkpost .exception .SparkPostException ;
2222import com .sparkpost .exception .SparkPostIllegalServerResponseException ;
23+ import com .sparkpost .exception .SparkPostAccessForbiddenException ;
24+ import com .sparkpost .exception .SparkPostAuthorizationFailedException ;
2325import com .sparkpost .model .responses .Response ;
2426
2527/**
@@ -37,6 +39,9 @@ public class RestConnection {
3739 private static final Base64 BASE64 = new Base64 ();
3840 private static final String DEFAULT_CHARSET = "UTF-8" ;
3941
42+ private static final int UNAUTHORIZED_RESPONSE_STATUS_CODE = 401 ;
43+ private static final int ACCESS_FORBIDDEN_RESPONSE_STATUS_CODE = 403 ;
44+
4045 /**
4146 * Default endpoint to use for connections :
4247 * https://api.sparkpost.com/api/v1/
@@ -262,7 +267,14 @@ private Response receiveResponse(HttpURLConnection conn, Response response) thro
262267 if (logger .isDebugEnabled ()) {
263268 logger .error ("Server Response:" + response );
264269 }
265- throw new SparkPostException ("Error reading server response: " + ex .toString () + ": " + sb .toString () + "(" + response .getResponseMessage () + ")" );
270+
271+ if (response .getResponseCode () == UNAUTHORIZED_RESPONSE_STATUS_CODE ) {
272+ throw new SparkPostAuthorizationFailedException ();
273+ } else if (response .getResponseCode () == ACCESS_FORBIDDEN_RESPONSE_STATUS_CODE ) {
274+ throw new SparkPostAccessForbiddenException ();
275+ } else {
276+ throw new SparkPostException ("Error reading server response: " + ex .toString () + ": " + sb .toString () + "(" + response .getResponseMessage () + ")" );
277+ }
266278 }
267279 return response ;
268280
0 commit comments