@@ -169,12 +169,9 @@ private HttpURLConnection createConnectionObject(String path, Method method) thr
169169 // Send HTTP data (payload) to server
170170 private void sendData (HttpURLConnection conn , String data ) throws SparkPostException {
171171 byte [] bytes = null ;
172- try
173- {
172+ try {
174173 bytes = data .getBytes (DEFAULT_CHARSET );
175- }
176- catch (UnsupportedEncodingException e )
177- {
174+ } catch (UnsupportedEncodingException e ) {
178175 // This should never happen. UTF-8 should always be available but we
179176 // have to catch it so pass it on if it fails.
180177 throw new SparkPostException (e );
@@ -247,10 +244,28 @@ private Response receiveResponse(HttpURLConnection conn, Response response) thro
247244 // an error.
248245 response .setResponseBody ("" );
249246 } catch (IOException ex ) {
247+ String line = "" ;
248+ try (BufferedReader rd = new BufferedReader (new InputStreamReader (conn .getErrorStream (), DEFAULT_CHARSET ))) {
249+
250+ while ((line = rd .readLine ()) != null ) {
251+ sb .append (line );
252+ }
253+
254+ response .setResponseBody (sb .toString ());
255+ response .setRequestId (conn .getHeaderField ("X-SparkPost-Request-Id" ));
256+
257+ logger .error ("Server Response:\n " + sb .toString () + "\n " );
258+
259+ } catch (IOException ex2 ) {
260+ // Ignore we are going to throw an exception anyway
261+ }
262+ if (logger .isDebugEnabled ()) {
263+ logger .error ("Server Response:" + response );
264+ }
250265 throw new SparkPostException ("Error reading server response: " + ex .toString () + ": " + sb .toString () + "(" + response .getResponseMessage () + ")" );
251266 }
252-
253267 return response ;
268+
254269 }
255270
256271 // This method actually performs an HTTP request.
0 commit comments