Skip to content

Commit 68e3ae8

Browse files
committed
#603 return 502 on HttpRequestException
1 parent f35a07a commit 68e3ae8

File tree

6 files changed

+37
-4
lines changed

6 files changed

+37
-4
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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,16 @@ 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))
4449
{
45-
return 502;
50+
return 500;
4651
}
4752

4853
return 404;

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.BadGateway))
92+
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.InternalServerError))
9393
.BDDfy();
9494
}
9595

test/Ocelot.UnitTests/Responder/ErrorsToHttpStatusCodeMapperTests.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ public void should_return_service_unavailable(OcelotErrorCode errorCode)
4747

4848
[Theory]
4949
[InlineData(OcelotErrorCode.UnableToCompleteRequestError)]
50+
public void should_return_internal_server_error(OcelotErrorCode errorCode)
51+
{
52+
ShouldMapErrorToStatusCode(errorCode, HttpStatusCode.InternalServerError);
53+
}
54+
55+
[Theory]
56+
[InlineData(OcelotErrorCode.ConnectionToDownstreamServiceError)]
5057
public void should_return_bad_gateway_error(OcelotErrorCode errorCode)
5158
{
5259
ShouldMapErrorToStatusCode(errorCode, HttpStatusCode.BadGateway);
@@ -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)