Skip to content

Commit 663bf0a

Browse files
committed
Fixes issue #28 where JSON errors were not getting pulled out of HTTP string
1 parent 5f737c1 commit 663bf0a

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

apps/sparkpost-samples-app/src/main/java/com/sparkpost/samples/SendEmailSample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private void sendEmail(String from, String[] recipients) throws SparkPostExcepti
6464

6565
// Populate Email Body
6666
TemplateContentAttributes contentAttributes = new TemplateContentAttributes();
67-
contentAttributes.setFrom(new AddressAttributes(from));
67+
//contentAttributes.setFrom(new AddressAttributes(from));
6868
contentAttributes.setSubject("Your subject content here. {{yourContent}}");
6969
contentAttributes.setText("Your Text content here. {{yourContent}}");
7070
contentAttributes.setHtml("<p>Your <b>HTML</b> content here. {{yourContent}}</p>");

libs/sparkpost-lib/src/main/java/com/sparkpost/transport/RestConnection.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)