Skip to content

Commit c99a8b3

Browse files
authored
Merge pull request #95 from mailjet/handling-error-429-and-500
Handling for error 429 and 500
2 parents 8d5f07f + 7a673e3 commit c99a8b3

File tree

4 files changed

+117
-21
lines changed

4 files changed

+117
-21
lines changed

src/main/java/com/mailjet/client/MailjetClient.java

+20-21
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,24 @@
1616

1717
package com.mailjet.client;
1818

19+
import static com.mailjet.client.MailjetResponseUtil.validateMailjetResponse;
20+
21+
import java.io.IOException;
22+
import java.io.UnsupportedEncodingException;
23+
import java.net.HttpURLConnection;
24+
import java.net.MalformedURLException;
25+
26+
import org.json.JSONObject;
27+
1928
import com.mailjet.client.errors.MailjetException;
2029
import com.mailjet.client.errors.MailjetSocketTimeoutException;
2130
import com.turbomanage.httpclient.BasicHttpClient;
2231
import com.turbomanage.httpclient.BasicRequestHandler;
2332
import com.turbomanage.httpclient.ConsoleRequestLogger;
2433
import com.turbomanage.httpclient.HttpResponse;
2534
import com.turbomanage.httpclient.ParameterMap;
26-
import com.turbomanage.httpclient.RequestLogger;
2735
import com.turbomanage.httpclient.RequestHandler;
28-
import java.io.IOException;
29-
import java.io.UnsupportedEncodingException;
30-
import java.net.HttpURLConnection;
31-
import java.net.MalformedURLException;
32-
import org.json.JSONObject;
33-
import com.mailjet.client.ClientOptions;
36+
import com.turbomanage.httpclient.RequestLogger;
3437

3538
/**
3639
*
@@ -237,9 +240,7 @@ public MailjetResponse get(MailjetRequest request) throws MailjetException, Mail
237240
p.putAll(request._filters);
238241
HttpResponse response = _client.get(url, p);
239242

240-
if (response == null) {
241-
throw new MailjetSocketTimeoutException("Socket Timeout");
242-
}
243+
validateMailjetResponse(response);
243244

244245
String json = (response.getBodyAsString() != null && !(response.getBodyAsString().equals("")) ?
245246
response.getBodyAsString() : new JSONObject().put("status", response.getStatus()).toString());
@@ -278,9 +279,7 @@ public MailjetResponse post(MailjetRequest request) throws MailjetException, Mai
278279

279280
response = _client.post(url, request.getContentType(), request.getBody().getBytes("UTF8"));
280281

281-
if (response == null) {
282-
throw new MailjetSocketTimeoutException("Socket Timeout");
283-
}
282+
validateMailjetResponse(response);
284283

285284
json = (response.getBodyAsString() != null && !(response.getBodyAsString().equals("")) ?
286285
response.getBodyAsString() : new JSONObject().put("status", response.getStatus()).toString());
@@ -307,11 +306,12 @@ public MailjetResponse put(MailjetRequest request) throws MailjetException, Mail
307306

308307
response = _client.put(url, request.getContentType(), request.getBody().getBytes("UTF8"));
309308

310-
if (response == null) {
311-
throw new MailjetSocketTimeoutException("Socket Timeout");
312-
}
309+
validateMailjetResponse(response);
310+
311+
String json = (response.getBodyAsString() != null && !response.getBodyAsString().trim().equals("") ?
312+
response.getBodyAsString() : new JSONObject().put("status", response.getStatus()).toString());
313313

314-
return new MailjetResponse(response.getStatus(), new JSONObject(response.getBodyAsString()));
314+
return new MailjetResponse(response.getStatus(), new JSONObject(json));
315315
} catch (MalformedURLException ex) {
316316
throw new MailjetException("Internal Exception: Malformed Url");
317317
} catch (UnsupportedEncodingException ex) {
@@ -336,12 +336,11 @@ public MailjetResponse delete(MailjetRequest request) throws MailjetException, M
336336
p.putAll(request._filters);
337337
response = _client.delete(url, p);
338338

339-
if (response == null) {
340-
throw new MailjetSocketTimeoutException("Socket Timeout");
341-
}
342-
339+
validateMailjetResponse(response);
340+
343341
json = (response.getBodyAsString() != null && !response.getBodyAsString().trim().equals("") ?
344342
response.getBodyAsString() : new JSONObject().put("status", response.getStatus()).toString());
343+
345344
return new MailjetResponse(response.getStatus(), new JSONObject(json));
346345
} catch (MalformedURLException ex) {
347346
throw new MailjetException("Internal Exception: Malformed Url");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.mailjet.client;
2+
3+
import org.json.JSONObject;
4+
5+
import com.mailjet.client.errors.MailjetRateLimitException;
6+
import com.mailjet.client.errors.MailjetServerException;
7+
import com.mailjet.client.errors.MailjetSocketTimeoutException;
8+
import com.turbomanage.httpclient.HttpResponse;
9+
10+
/**
11+
*
12+
* @author y.vanov
13+
*
14+
*/
15+
public final class MailjetResponseUtil {
16+
17+
private static final int TOO_MANY_REQUEST_STATUS = 429;
18+
private static final int INTERNAL_SERVER_ERROR_STATUS = 500;
19+
20+
private static final String STATUS_PROPERTY = "status";
21+
private static final String SOCKET_TIMEOUT_EXCEPTION = "Socket Timeout";
22+
private static final String TOO_MANY_REQUESTS_EXCEPTION = "Too Many Requests";
23+
private static final String INTERNAL_SERVER_ERROR_GENERAL_EXCEPTION = "Internal Server Error";
24+
25+
private MailjetResponseUtil() {
26+
}
27+
28+
public static void validateMailjetResponse(HttpResponse response)
29+
throws MailjetSocketTimeoutException, MailjetRateLimitException, MailjetServerException {
30+
if (response == null) {
31+
throw new MailjetSocketTimeoutException(SOCKET_TIMEOUT_EXCEPTION);
32+
} else if (response.getStatus() == TOO_MANY_REQUEST_STATUS) {
33+
throw new MailjetRateLimitException(TOO_MANY_REQUESTS_EXCEPTION);
34+
} else if (response.getStatus() >= INTERNAL_SERVER_ERROR_STATUS) {
35+
if (!isValidJSON(response.getBodyAsString())) {
36+
throw new MailjetServerException(INTERNAL_SERVER_ERROR_GENERAL_EXCEPTION);
37+
}
38+
}
39+
}
40+
41+
public static String parseResponseBodyToValidString(HttpResponse response) {
42+
return (response.getBodyAsString() != null && !response.getBodyAsString().trim().equals("")
43+
? response.getBodyAsString()
44+
: new JSONObject().put(STATUS_PROPERTY, response.getStatus()).toString());
45+
}
46+
47+
public static boolean isValidJSON(String json) {
48+
return json != null && json.trim().startsWith("{");
49+
}
50+
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.mailjet.client.errors;
7+
8+
/**
9+
* MailjetRateLimitException handles http status code 429 (Too Many Requests)
10+
*
11+
* @author y.vanov
12+
*
13+
*/
14+
public class MailjetRateLimitException extends MailjetException {
15+
16+
/**
17+
*
18+
*/
19+
private static final long serialVersionUID = 7854200196479735430L;
20+
21+
public MailjetRateLimitException(String error) {
22+
super(error);
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.mailjet.client.errors;
2+
3+
/**
4+
* MailjetServerException handles http status code >= 500 (Internal Server Error)
5+
*
6+
* @author y.vanov
7+
*
8+
*/
9+
public class MailjetServerException extends MailjetException {
10+
11+
/**
12+
*
13+
*/
14+
private static final long serialVersionUID = -6534953525088397824L;
15+
16+
public MailjetServerException(String error) {
17+
super(error);
18+
}
19+
20+
21+
}

0 commit comments

Comments
 (0)