mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 06:22:50 +08:00
#603 return 502 on HttpRequestException
This commit is contained in:
parent
f35a07a3da
commit
68e3ae8cdd
@ -40,5 +40,6 @@
|
|||||||
CannotRemovePlaceholderError = 35,
|
CannotRemovePlaceholderError = 35,
|
||||||
QuotaExceededError = 36,
|
QuotaExceededError = 36,
|
||||||
RequestCanceled = 37,
|
RequestCanceled = 37,
|
||||||
|
ConnectionToDownstreamServiceError = 38,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
13
src/Ocelot/Requester/ConnectionToDownstreamServiceError.cs
Normal file
13
src/Ocelot/Requester/ConnectionToDownstreamServiceError.cs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
using Ocelot.Errors;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Ocelot.Requester
|
||||||
|
{
|
||||||
|
class ConnectionToDownstreamServiceError : Error
|
||||||
|
{
|
||||||
|
public ConnectionToDownstreamServiceError(Exception exception)
|
||||||
|
: base($"Error connecting to downstream service, exception: {exception}", OcelotErrorCode.ConnectionToDownstreamServiceError)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -4,6 +4,8 @@ namespace Ocelot.Requester
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
|
||||||
public class HttpExeptionToErrorMapper : IExceptionToErrorMapper
|
public class HttpExeptionToErrorMapper : IExceptionToErrorMapper
|
||||||
{
|
{
|
||||||
@ -28,6 +30,11 @@ namespace Ocelot.Requester
|
|||||||
return new RequestCanceledError(exception.Message);
|
return new RequestCanceledError(exception.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (type == typeof(HttpRequestException))
|
||||||
|
{
|
||||||
|
return new ConnectionToDownstreamServiceError(exception);
|
||||||
|
}
|
||||||
|
|
||||||
return new UnableToCompleteRequestError(exception);
|
return new UnableToCompleteRequestError(exception);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,11 +40,16 @@ namespace Ocelot.Responder
|
|||||||
return 404;
|
return 404;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (errors.Any(e => e.Code == OcelotErrorCode.UnableToCompleteRequestError))
|
if (errors.Any(e => e.Code == OcelotErrorCode.ConnectionToDownstreamServiceError))
|
||||||
{
|
{
|
||||||
return 502;
|
return 502;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (errors.Any(e => e.Code == OcelotErrorCode.UnableToCompleteRequestError))
|
||||||
|
{
|
||||||
|
return 500;
|
||||||
|
}
|
||||||
|
|
||||||
return 404;
|
return 404;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -89,7 +89,7 @@ namespace Ocelot.AcceptanceTests
|
|||||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||||
.And(x => _steps.GivenOcelotIsRunning())
|
.And(x => _steps.GivenOcelotIsRunning())
|
||||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.BadGateway))
|
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.InternalServerError))
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,6 +47,13 @@ namespace Ocelot.UnitTests.Responder
|
|||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData(OcelotErrorCode.UnableToCompleteRequestError)]
|
[InlineData(OcelotErrorCode.UnableToCompleteRequestError)]
|
||||||
|
public void should_return_internal_server_error(OcelotErrorCode errorCode)
|
||||||
|
{
|
||||||
|
ShouldMapErrorToStatusCode(errorCode, HttpStatusCode.InternalServerError);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(OcelotErrorCode.ConnectionToDownstreamServiceError)]
|
||||||
public void should_return_bad_gateway_error(OcelotErrorCode errorCode)
|
public void should_return_bad_gateway_error(OcelotErrorCode errorCode)
|
||||||
{
|
{
|
||||||
ShouldMapErrorToStatusCode(errorCode, HttpStatusCode.BadGateway);
|
ShouldMapErrorToStatusCode(errorCode, HttpStatusCode.BadGateway);
|
||||||
@ -125,7 +132,7 @@ namespace Ocelot.UnitTests.Responder
|
|||||||
// If this test fails then it's because the number of error codes has changed.
|
// If this test fails then it's because the number of error codes has changed.
|
||||||
// You should make the appropriate changes to the test cases here to ensure
|
// You should make the appropriate changes to the test cases here to ensure
|
||||||
// they cover all the error codes, and then modify this assertion.
|
// they cover all the error codes, and then modify this assertion.
|
||||||
Enum.GetNames(typeof(OcelotErrorCode)).Length.ShouldBe(38, "Looks like the number of error codes has changed. Do you need to modify ErrorsToHttpStatusCodeMapper?");
|
Enum.GetNames(typeof(OcelotErrorCode)).Length.ShouldBe(39, "Looks like the number of error codes has changed. Do you need to modify ErrorsToHttpStatusCodeMapper?");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ShouldMapErrorToStatusCode(OcelotErrorCode errorCode, HttpStatusCode expectedHttpStatusCode)
|
private void ShouldMapErrorToStatusCode(OcelotErrorCode errorCode, HttpStatusCode expectedHttpStatusCode)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user