Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.sparkpost.exception;

public class SparkPostAccessForbiddenException extends SparkPostException {

private static final long serialVersionUID = 4127644290615861545L;

private static final String MESSAGE = "API key has no required permission to perform this action.";

public SparkPostAccessForbiddenException() {
super(MESSAGE);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.sparkpost.exception;

public class SparkPostAuthorizationFailedException extends SparkPostException {

private static final long serialVersionUID = 3695537080454452130L;

private static final String MESSAGE = "Authorization failed. Check your API key.";

public SparkPostAuthorizationFailedException() {
super(MESSAGE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.sparkpost.Client;
import com.sparkpost.exception.SparkPostException;
import com.sparkpost.exception.SparkPostIllegalServerResponseException;
import com.sparkpost.exception.SparkPostAccessForbiddenException;
import com.sparkpost.exception.SparkPostAuthorizationFailedException;
import com.sparkpost.model.responses.Response;

/**
Expand All @@ -37,6 +39,9 @@ public class RestConnection {
private static final Base64 BASE64 = new Base64();
private static final String DEFAULT_CHARSET = "UTF-8";

private static final int UNAUTHORIZED_RESPONSE_STATUS_CODE = 401;
private static final int ACCESS_FORBIDDEN_RESPONSE_STATUS_CODE = 403;

/**
* Default endpoint to use for connections :
* https://api.sparkpost.com/api/v1/
Expand Down Expand Up @@ -262,7 +267,14 @@ private Response receiveResponse(HttpURLConnection conn, Response response) thro
if (logger.isDebugEnabled()) {
logger.error("Server Response:" + response);
}
throw new SparkPostException("Error reading server response: " + ex.toString() + ": " + sb.toString() + "(" + response.getResponseMessage() + ")");

if (response.getResponseCode() == UNAUTHORIZED_RESPONSE_STATUS_CODE) {
throw new SparkPostAuthorizationFailedException();
} else if (response.getResponseCode() == ACCESS_FORBIDDEN_RESPONSE_STATUS_CODE) {
throw new SparkPostAccessForbiddenException();
} else {
throw new SparkPostException("Error reading server response: " + ex.toString() + ": " + sb.toString() + "(" + response.getResponseMessage() + ")");
}
}
return response;

Expand Down