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

#83: Fixed error handling when send request fails with an exception. #84

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
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/**
*
*
*/
package com.microtripit.mandrillapp.lutung.model;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
Expand All @@ -19,7 +18,6 @@
import com.microtripit.mandrillapp.lutung.model.MandrillApiError.MandrillError;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
Expand Down Expand Up @@ -48,8 +46,8 @@ public final class MandrillRequestDispatcher {
* The value is expressed in milliseconds.
* */
public static int CONNECTION_TIMEOUT_MILLIS = 0;


private static CloseableHttpClient httpClient;
private static PoolingHttpClientConnectionManager connexionManager;
private static RequestConfig defaultRequestConfig;
Expand All @@ -70,7 +68,7 @@ public final class MandrillRequestDispatcher {
public static final <T> T execute(final RequestModel<T> requestModel) throws MandrillApiError, IOException {

HttpResponse response = null;
String responseString = null;
String responseString;
try {
// use proxy?
final ProxyData proxyData = detectProxyServer(requestModel.getUrl());
Expand All @@ -91,17 +89,17 @@ public static final <T> T execute(final RequestModel<T> requestModel) throws Man
if( requestModel.validateResponseStatus(status.getStatusCode()) ) {
try {
return requestModel.handleResponse( responseString );

} catch(final HandleResponseException e) {
throw new IOException(
"Failed to parse response from request '"
"Failed to parse response from request '"
+requestModel.getUrl()+ "'", e);

}

} else {
// ==> compile mandrill error!
MandrillError error = null;
MandrillError error;
try {
error = LutungGsonUtils.getGson()
.fromJson(responseString, MandrillError.class);
Expand All @@ -113,18 +111,15 @@ public static final <T> T execute(final RequestModel<T> requestModel) throws Man
}

throw new MandrillApiError(
"Unexpected http status in response: "
+status.getStatusCode()+ " ("
"Unexpected http status in response: "
+status.getStatusCode()+ " ("
+status.getReasonPhrase()+ ")").withError(error);

}

} finally {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException e) {
log.error("Error consuming entity", e);
throw e;
if (null != response) {
EntityUtils.consumeQuietly(response.getEntity());
}
}
}
Expand Down