Skip to content

Commit 3dfcff8

Browse files
committed
Merge branch 'feature/603-bad-gateway' of https://github.com/jlukawska/Ocelot into jlukawska-feature/603-bad-gateway
2 parents 77d4bb1 + 8ead511 commit 3dfcff8

File tree

8 files changed

+70
-5
lines changed

8 files changed

+70
-5
lines changed

src/Ocelot/Errors/OcelotErrorCode.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public enum OcelotErrorCode
3939
CannotAddPlaceholderError = 34,
4040
CannotRemovePlaceholderError = 35,
4141
QuotaExceededError = 36,
42-
RequestCanceled = 37,
42+
RequestCanceled = 37,
43+
ConnectionToDownstreamServiceError = 38,
4344
}
4445
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Ocelot.Errors;
2+
using System;
3+
4+
namespace Ocelot.Requester
5+
{
6+
class ConnectionToDownstreamServiceError : Error
7+
{
8+
public ConnectionToDownstreamServiceError(Exception exception)
9+
: base($"Error connecting to downstream service, exception: {exception}", OcelotErrorCode.ConnectionToDownstreamServiceError)
10+
{
11+
}
12+
}
13+
}

src/Ocelot/Requester/HttpExeptionToErrorMapper.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ namespace Ocelot.Requester
44
using Microsoft.Extensions.DependencyInjection;
55
using System;
66
using System.Collections.Generic;
7+
using System.Net.Http;
8+
using System.Net.Sockets;
79

810
public class HttpExeptionToErrorMapper : IExceptionToErrorMapper
911
{
@@ -28,6 +30,11 @@ public Error Map(Exception exception)
2830
return new RequestCanceledError(exception.Message);
2931
}
3032

33+
if (type == typeof(HttpRequestException))
34+
{
35+
return new ConnectionToDownstreamServiceError(exception);
36+
}
37+
3138
return new UnableToCompleteRequestError(exception);
3239
}
3340
}

src/Ocelot/Responder/ErrorsToHttpStatusCodeMapper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public int Map(List<Error> errors)
3838
if (errors.Any(e => e.Code == OcelotErrorCode.UnableToFindDownstreamRouteError))
3939
{
4040
return 404;
41+
}
42+
43+
if (errors.Any(e => e.Code == OcelotErrorCode.ConnectionToDownstreamServiceError))
44+
{
45+
return 502;
4146
}
4247

4348
if (errors.Any(e => e.Code == OcelotErrorCode.UnableToCompleteRequestError))

test/Ocelot.AcceptanceTests/HttpTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public void should_return_response_200_when_using_http_two_point_zero()
141141
}
142142

143143
[Fact]
144-
public void should_return_response_500_when_using_http_one_to_talk_to_server_running_http_two()
144+
public void should_return_response_502_when_using_http_one_to_talk_to_server_running_http_two()
145145
{
146146
var port = RandomPortFinder.GetRandomPort();
147147

@@ -177,7 +177,7 @@ public void should_return_response_500_when_using_http_one_to_talk_to_server_run
177177
.And(x => _steps.GivenThereIsAConfiguration(configuration))
178178
.And(x => _steps.GivenOcelotIsRunning())
179179
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/", httpContent))
180-
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.InternalServerError))
180+
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.BadGateway))
181181
.BDDfy();
182182
}
183183

test/Ocelot.AcceptanceTests/ReturnsErrorTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,38 @@ public ReturnsErrorTests()
1616
{
1717
_serviceHandler = new ServiceHandler();
1818
_steps = new Steps();
19+
}
20+
21+
[Fact]
22+
public void should_return_bad_gateway_error_if_downstream_service_doesnt_respond()
23+
{
24+
var configuration = new FileConfiguration
25+
{
26+
ReRoutes = new List<FileReRoute>
27+
{
28+
new FileReRoute
29+
{
30+
DownstreamPathTemplate = "/",
31+
UpstreamPathTemplate = "/",
32+
UpstreamHttpMethod = new List<string> { "Get" },
33+
DownstreamHostAndPorts = new List<FileHostAndPort>
34+
{
35+
new FileHostAndPort
36+
{
37+
Host = "localhost",
38+
Port = 53877,
39+
},
40+
},
41+
DownstreamScheme = "http",
42+
},
43+
},
44+
};
45+
46+
this.Given(x => _steps.GivenThereIsAConfiguration(configuration))
47+
.And(x => _steps.GivenOcelotIsRunning())
48+
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
49+
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.BadGateway))
50+
.BDDfy();
1951
}
2052

2153
[Fact]

test/Ocelot.AcceptanceTests/SslTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void should_not_dangerous_accept_any_server_certificate_validator()
8989
.And(x => _steps.GivenThereIsAConfiguration(configuration))
9090
.And(x => _steps.GivenOcelotIsRunning())
9191
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
92-
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.InternalServerError))
92+
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.BadGateway))
9393
.BDDfy();
9494
}
9595

test/Ocelot.UnitTests/Responder/ErrorsToHttpStatusCodeMapperTests.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ public void should_return_service_unavailable(OcelotErrorCode errorCode)
5050
public void should_return_internal_server_error(OcelotErrorCode errorCode)
5151
{
5252
ShouldMapErrorToStatusCode(errorCode, HttpStatusCode.InternalServerError);
53+
}
54+
55+
[Theory]
56+
[InlineData(OcelotErrorCode.ConnectionToDownstreamServiceError)]
57+
public void should_return_bad_gateway_error(OcelotErrorCode errorCode)
58+
{
59+
ShouldMapErrorToStatusCode(errorCode, HttpStatusCode.BadGateway);
5360
}
5461

5562
[Theory]
@@ -125,7 +132,7 @@ public void check_we_have_considered_all_errors_in_these_tests()
125132
// If this test fails then it's because the number of error codes has changed.
126133
// You should make the appropriate changes to the test cases here to ensure
127134
// they cover all the error codes, and then modify this assertion.
128-
Enum.GetNames(typeof(OcelotErrorCode)).Length.ShouldBe(38, "Looks like the number of error codes has changed. Do you need to modify ErrorsToHttpStatusCodeMapper?");
135+
Enum.GetNames(typeof(OcelotErrorCode)).Length.ShouldBe(39, "Looks like the number of error codes has changed. Do you need to modify ErrorsToHttpStatusCodeMapper?");
129136
}
130137

131138
private void ShouldMapErrorToStatusCode(OcelotErrorCode errorCode, HttpStatusCode expectedHttpStatusCode)

0 commit comments

Comments
 (0)