Skip to content

Commit 6cf54cd

Browse files
committed
Merge pull request #33 from kalnik-a-a/master
Separate exceptions for authorization and permission errors (401 and 403 response codes).
2 parents 48137e2 + 6250697 commit 6cf54cd

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.sparkpost.exception;
2+
3+
public class SparkPostAccessForbiddenException extends SparkPostException {
4+
5+
private static final long serialVersionUID = 4127644290615861545L;
6+
7+
private static final String MESSAGE = "API key has no required permission to perform this action.";
8+
9+
public SparkPostAccessForbiddenException() {
10+
super(MESSAGE);
11+
}
12+
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.sparkpost.exception;
2+
3+
public class SparkPostAuthorizationFailedException extends SparkPostException {
4+
5+
private static final long serialVersionUID = 3695537080454452130L;
6+
7+
private static final String MESSAGE = "Authorization failed. Check your API key.";
8+
9+
public SparkPostAuthorizationFailedException() {
10+
super(MESSAGE);
11+
}
12+
}

libs/sparkpost-lib/src/main/java/com/sparkpost/transport/RestConnection.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.sparkpost.Client;
2121
import com.sparkpost.exception.SparkPostException;
2222
import com.sparkpost.exception.SparkPostIllegalServerResponseException;
23+
import com.sparkpost.exception.SparkPostAccessForbiddenException;
24+
import com.sparkpost.exception.SparkPostAuthorizationFailedException;
2325
import 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

Comments
 (0)