Skip to content
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

Return specific error messages from the portico gateway #37

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
32 changes: 29 additions & 3 deletions src/main/java/com/global/api/gateways/Gateway.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.global.api.gateways;

import com.global.api.entities.exceptions.ApiException;
import com.global.api.entities.exceptions.GatewayException;
import com.global.api.entities.gpApi.GpApiRequest;
import com.global.api.logging.IRequestLogger;
import com.global.api.logging.RequestConsoleLogger;
import com.global.api.logging.RequestFileLogger;
import com.global.api.utils.Element;
import com.global.api.utils.ElementTree;
import com.global.api.utils.IOUtils;
import com.global.api.utils.StringUtils;
import lombok.Getter;
Expand Down Expand Up @@ -127,7 +130,13 @@ else if (this.enableLogging || this.requestLogger != null) {
logEntry.append("Request Params: ").append(queryString).append(lSChar);
}

InputStream responseStream = conn.getInputStream();
InputStream responseStream;
if (conn.getResponseCode() == 200) {
responseStream = conn.getInputStream();
} else {
responseStream = conn.getErrorStream();
}

String rawResponse = getRawResponse(responseStream);
responseStream.close();

Expand All @@ -142,6 +151,10 @@ else if (this.enableLogging || this.requestLogger != null) {
generateResponseLog();
}

if (conn.getResponseCode() != 200) {
throw new GatewayException(rawResponse, Integer.toString(conn.getResponseCode()), rawResponse);
}

GatewayResponse response = new GatewayResponse();
response.setStatusCode(conn.getResponseCode());
response.setRawResponse(rawResponse);
Expand Down Expand Up @@ -225,7 +238,7 @@ protected GatewayResponse sendRequest(String endpoint, MultipartEntity content)
conn.addRequestProperty("Content-Type", content.getContentType().getValue());
conn.addRequestProperty("Content-Length", String.valueOf(content.getContentLength()));

try(InputStream responseStream = conn.getInputStream();
try(InputStream responseStream = conn.getResponseCode() == 200 ? conn.getInputStream() : conn.getErrorStream();
OutputStream out = conn.getOutputStream();) {

if (this.enableLogging || this.requestLogger != null) {
Expand All @@ -243,6 +256,11 @@ protected GatewayResponse sendRequest(String endpoint, MultipartEntity content)
generateResponseLog();
}

if (conn.getResponseCode() != 200) {
String error = parseErrorMessage(rawResponse);
throw new GatewayException(error, Integer.toString(conn.getResponseCode()), error);
}

GatewayResponse response = new GatewayResponse();
response.setStatusCode(conn.getResponseCode());
response.setRawResponse(rawResponse);
Expand Down Expand Up @@ -271,6 +289,14 @@ private String buildQueryString(HashMap<String, String> queryStringParams) throw
}
return sb.toString();
}

private String parseErrorMessage(String rawResponse) throws ApiException {
ElementTree tree = ElementTree.parse(rawResponse);
Element root = tree.get("soap:Fault");
String error = root.getString("soap:Reason");
return error;
}


private void setRequestMethod(final HttpURLConnection c, final String value) {
try {
Expand Down Expand Up @@ -389,4 +415,4 @@ private void generateResponseLog() {
logEntry.delete(0, logEntry.length());
}

}
}