Skip to content

Return error response based on caught exception from postData #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
38 changes: 38 additions & 0 deletions src/main/java/net/authorize/util/HttpUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -26,6 +27,9 @@
import net.authorize.Environment;
import net.authorize.api.contract.v1.ANetApiRequest;
import net.authorize.api.contract.v1.ANetApiResponse;
import net.authorize.api.contract.v1.MessageTypeEnum;
import net.authorize.api.contract.v1.MessagesType;
import net.authorize.api.contract.v1.MessagesType.Message;

/**
* Helper methods for http calls
Expand Down Expand Up @@ -106,13 +110,47 @@ public static <T> ANetApiResponse postData(Environment env, ANetApiRequest reque
logger.debug(String.format("Response: '%s'", response));
} catch (InterruptedException ie) {
logger.error(String.format("Http call interrupted Message: '%s'", ie.getMessage()));
response = createErrorResponse(ie);
} catch (ExecutionException ee) {
logger.error(String.format("Execution error for http post Message: '%s'", ee.getMessage()));
response = createErrorResponse(ee);
}

return response;
}

/**
* Creates a failure response with information from the exception causing the failure
* @param exception causing failed response
* @return failed ANetApiResponse with information from exception
*/
private static ANetApiResponse createErrorResponse(Exception exception) {
ANetApiResponse response = new ANetApiResponse();

MessagesType aMessage = new MessagesType();
aMessage.setResultCode(MessageTypeEnum.ERROR);
response.setMessages(aMessage);

List<Message> messages = response.getMessages().getMessage();
// clear all messages
messages.clear();

if (null != exception) {
Message errorMessage = new Message();
messages.add(errorMessage);
String code = "Error";
String text = "Unknown Error";
code = exception.getClass().getCanonicalName();
// code = exception.getClass().getTypeName();// requires java1.8
text = exception.getMessage();

errorMessage.setCode(code);
errorMessage.setText(text);
}

return response;
}

/**
* Converts a response inputstream into a string.
*
Expand Down