From 90bf7768d58cfa45fbe4e32918d827c59f1a8109 Mon Sep 17 00:00:00 2001 From: thomas_berger Date: Thu, 7 Dec 2017 10:42:13 +0100 Subject: [PATCH] #83: Fixed error handling when send request fails with an exception. --- .../model/MandrillRequestDispatcher.java | 35 ++++++++----------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/microtripit/mandrillapp/lutung/model/MandrillRequestDispatcher.java b/src/main/java/com/microtripit/mandrillapp/lutung/model/MandrillRequestDispatcher.java index 16a2bff..5ad3a02 100644 --- a/src/main/java/com/microtripit/mandrillapp/lutung/model/MandrillRequestDispatcher.java +++ b/src/main/java/com/microtripit/mandrillapp/lutung/model/MandrillRequestDispatcher.java @@ -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; @@ -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; @@ -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; @@ -70,7 +68,7 @@ public final class MandrillRequestDispatcher { public static final T execute(final RequestModel requestModel) throws MandrillApiError, IOException { HttpResponse response = null; - String responseString = null; + String responseString; try { // use proxy? final ProxyData proxyData = detectProxyServer(requestModel.getUrl()); @@ -91,17 +89,17 @@ public static final T execute(final RequestModel 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); @@ -113,18 +111,15 @@ public static final T execute(final RequestModel 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()); } } }