Skip to content

Commit

Permalink
Catch json parse exceptions and throw as a mandrill error.
Browse files Browse the repository at this point in the history
  • Loading branch information
billoneil committed Nov 20, 2015
1 parent c3da85e commit 5f6b766
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ public static final class MandrillError {
private String status, name, message;
private Integer code;

public MandrillError(String status, String name, String message, Integer code) {
this.status = status;
this.name = name;
this.message = message;
this.code = code;
}

/**
* @return The error status returned by the Mandrill API.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,28 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import com.microtripit.mandrillapp.lutung.logging.Logger;
import com.microtripit.mandrillapp.lutung.logging.LoggerFactory;
import com.microtripit.mandrillapp.lutung.model.MandrillApiError.MandrillError;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;
import org.apache.http.params.HttpConnectionParams;

import com.google.gson.JsonSyntaxException;
import com.microtripit.mandrillapp.lutung.logging.Logger;
import com.microtripit.mandrillapp.lutung.logging.LoggerFactory;
import com.microtripit.mandrillapp.lutung.model.MandrillApiError.MandrillError;

/**
* @author rschreijer
* @since Feb 21, 2013
Expand Down Expand Up @@ -88,8 +92,17 @@ public static final <T> T execute(final RequestModel<T> requestModel,
} else {
// ==> compile mandrill error!
final String e = IOUtils.toString(responseInputStream);
final MandrillError error = LutungGsonUtils.getGson()
MandrillError error = null;
try {
error = LutungGsonUtils.getGson()
.fromJson(e, MandrillError.class);
} catch (Throwable ex) {
error = new MandrillError("Invalid Error Format",
"Invalid Error Format",
e,
status.getStatusCode());
}

throw new MandrillApiError(
"Unexpected http status in response: "
+status.getStatusCode()+ " ("
Expand Down

0 comments on commit 5f6b766

Please sign in to comment.