Skip to content

Commit 2399db2

Browse files
authored
Merge pull request #19 from mailjet/fix-for-500-and-429-errors
Fix for 500 and 429 errors
2 parents 7ed00da + f360a5d commit 2399db2

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

Mailjet.Client/MailjetClient.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Newtonsoft.Json;
22
using Newtonsoft.Json.Linq;
33
using System;
4+
using System.Net;
45
using System.Net.Http;
56
using System.Net.Http.Headers;
67
using System.Text;
@@ -26,6 +27,9 @@ public class MailjetClient : IMailjetClient
2627
private const string ApiVersionPathV3 = "v3";
2728
private const string ApiVersionPathV3_1 = "v3.1";
2829
private const string ApiVersionPathV4 = "v4";
30+
private const string ErrorInfo = "ErrorInfo";
31+
private const string TooManyRequestsMessage = "Too many requests";
32+
private const string InternalServerErrorGeneralMessage = "Internal Server Error";
2933

3034
private HttpClient _httpClient;
3135

@@ -119,7 +123,18 @@ private async Task<JObject> GetContent(HttpResponseMessage responseMessage)
119123

120124
if (!responseMessage.IsSuccessStatusCode)
121125
{
122-
content.Add("ErrorInfo", new JValue(responseMessage.ReasonPhrase));
126+
if (responseMessage.StatusCode == ((HttpStatusCode) 429))
127+
{
128+
content.Add(ErrorInfo, new JValue(TooManyRequestsMessage));
129+
}
130+
else if (responseMessage.StatusCode == HttpStatusCode.InternalServerError)
131+
{
132+
content.Add(ErrorInfo, new JValue(InternalServerErrorGeneralMessage));
133+
}
134+
else
135+
{
136+
content.Add(ErrorInfo, new JValue(responseMessage.ReasonPhrase));
137+
}
123138
}
124139
}
125140

Mailjet.Tests/MailjetClientTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Newtonsoft.Json.Linq;
55
using RichardSzalay.MockHttp;
66
using System;
7+
using System.Net;
78
using sms = Mailjet.Client.Resources.SMS;
89

910
namespace Mailjet.Tests
@@ -69,6 +70,50 @@ public void TestGetAsync()
6970
Assert.IsTrue(JToken.DeepEquals(expectedData, response.GetData()));
7071
}
7172

73+
[TestMethod]
74+
public void TestTooManyRequestsStatus()
75+
{
76+
var mockHttp = new MockHttpMessageHandler();
77+
mockHttp.When("https://api.mailjet.com/v3/*")
78+
.Respond(((HttpStatusCode) 429));
79+
80+
// Inject the handler into your application code
81+
IMailjetClient client = new MailjetClient(ApiKeyTest, ApiSecretTest, mockHttp);
82+
83+
MailjetRequest request = new MailjetRequest
84+
{
85+
Resource = Apikey.Resource,
86+
};
87+
88+
MailjetResponse response = client.GetAsync(request).Result;
89+
90+
Assert.AreEqual(429, response.StatusCode);
91+
Assert.AreEqual("Too many requests", response.GetErrorInfo());
92+
}
93+
94+
95+
[TestMethod]
96+
public void TestInternalServerErrorStatus()
97+
{
98+
var mockHttp = new MockHttpMessageHandler();
99+
mockHttp.When("https://api.mailjet.com/v3/*")
100+
.Respond(HttpStatusCode.InternalServerError);
101+
102+
103+
// Inject the handler into your application code
104+
IMailjetClient client = new MailjetClient(ApiKeyTest, ApiSecretTest, mockHttp);
105+
106+
MailjetRequest request = new MailjetRequest
107+
{
108+
Resource = Apikey.Resource,
109+
};
110+
111+
MailjetResponse response = client.GetAsync(request).Result;
112+
113+
Assert.AreEqual(500, response.StatusCode);
114+
Assert.AreEqual("Internal Server Error", response.GetErrorInfo());
115+
}
116+
72117
[TestMethod]
73118
public void TestSmsCountAsync()
74119
{

0 commit comments

Comments
 (0)