Skip to content

Commit 86485ab

Browse files
authored
Merge pull request #187 from fullstaq-labs/master
Throw GatewayTimeoutException upon receiving 504 Gateway Timeout responses
2 parents cd30e60 + af93e19 commit 86485ab

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

Diff for: Contentful.Core.Tests/ContentfulClientTests.cs

+12
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,18 @@ public async Task RateLimitWithRetryShouldStopCallingOnSuccess()
210210
Assert.Equal("SoSo Wall Clock", res.ProductName);
211211
}
212212

213+
[Fact]
214+
public async Task GatewayTimeoutExceptionShouldBeThrownCorrectly()
215+
{
216+
//Arrange
217+
var response = new HttpResponseMessage();
218+
response.Content = new StringContent("");
219+
response.StatusCode = (HttpStatusCode)504;
220+
_handler.Response = response;
221+
//Act
222+
await Assert.ThrowsAsync<GatewayTimeoutException>(async () => await _client.GetEntry<TestEntryModel>("12"));
223+
}
224+
213225
[Fact]
214226
public async Task SettingPreviewApiShouldUseCorrectApiKey()
215227
{

Diff for: Contentful.Core/ContentfulClientBase.cs

+23-5
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,16 @@ private static bool IsMacOS
8989
/// <returns></returns>
9090
protected async Task CreateExceptionForFailedRequest(HttpResponseMessage res)
9191
{
92-
var jsonError = JObject.Parse(await res.Content.ReadAsStringAsync().ConfigureAwait(false));
93-
var sys = jsonError.SelectToken("$.sys").ToObject<SystemProperties>();
94-
var errorDetails = jsonError.SelectToken("$.details")?.ToObject<ErrorDetails>();
95-
var message = jsonError.SelectToken("$.message")?.ToString();
92+
var responseContent = await res.Content.ReadAsStringAsync().ConfigureAwait(false);
93+
var jsonError = string.IsNullOrEmpty(responseContent) ? null : JObject.Parse(responseContent);
94+
var sys = jsonError?.SelectToken("$.sys").ToObject<SystemProperties>();
95+
var errorDetails = jsonError?.SelectToken("$.details")?.ToObject<ErrorDetails>();
96+
var message = jsonError?.SelectToken("$.message")?.ToString();
9697
var statusCode = (int)res.StatusCode;
9798

9899
if (string.IsNullOrEmpty(message))
99100
{
100-
message = GetGenericErrorMessageForStatusCode(statusCode, sys.Id);
101+
message = GetGenericErrorMessageForStatusCode(statusCode, sys?.Id);
101102
}
102103

103104
if(errorDetails != null)
@@ -120,6 +121,18 @@ protected async Task CreateExceptionForFailedRequest(HttpResponseMessage res)
120121
throw rateLimitException;
121122
}
122123

124+
if(statusCode == 504)
125+
{
126+
var gatewayTimeoutException = new GatewayTimeoutException()
127+
{
128+
RequestId = jsonError?.SelectToken("$.requestId")?.ToString(),
129+
ErrorDetails = errorDetails,
130+
SystemProperties = sys
131+
};
132+
133+
throw gatewayTimeoutException;
134+
}
135+
123136
var ex = new ContentfulException(statusCode, message)
124137
{
125138
RequestId = jsonError.SelectToken("$.requestId")?.ToString(),
@@ -186,6 +199,11 @@ private string GetGenericErrorMessageForStatusCode(int statusCode, string id)
186199
return "The requested space is hibernated.";
187200
}
188201

202+
if(statusCode == 504)
203+
{
204+
return "Gateway timeout.";
205+
}
206+
189207
return "An error occurred.";
190208
}
191209

Diff for: Contentful.Core/Errors/GatewayTimeoutException.cs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Contentful.Core.Errors
6+
{
7+
/// <summary>
8+
/// Represents errors that occurr when the Contentful API returns 504 Gateway Timeout responses.
9+
/// </summary>
10+
public class GatewayTimeoutException : ContentfulException
11+
{
12+
/// <summary>
13+
/// Initializes a new instance of <see cref="Contentful.Core.Errors.GatewayTimeoutException"/>.
14+
/// </summary>
15+
public GatewayTimeoutException() : base(429, "Gateway Timeout")
16+
{
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)