mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 08:15:27 +08:00 
			
		
		
		
	Merge branch 'jlukawska-feature/603-bad-gateway'
This commit is contained in:
		
							
								
								
									
										13
									
								
								docs/features/errorcodes.rst
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								docs/features/errorcodes.rst
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
			
		||||
Http Error Status Codes
 | 
			
		||||
=======================
 | 
			
		||||
 | 
			
		||||
Ocelot will return HTTP status error codes based on internal logic in certain siturations:
 | 
			
		||||
- 401 if the authentication middleware runs and the user is not authenticated.
 | 
			
		||||
- 403 if the authorisation middleware runs and the user is unauthenticated, claim value not authroised, scope not authorised, user doesnt have required claim or cannot find claim.
 | 
			
		||||
- 503 if the downstream request times out.
 | 
			
		||||
- 499 if the request is cancelled by the client.
 | 
			
		||||
- 404 if unable to find a downstream route.
 | 
			
		||||
- 502 if unable to connect to downstream service.
 | 
			
		||||
- 500 if unable to complete the HTTP request downstream and the exception is not OperationCanceledException or HttpRequestException.
 | 
			
		||||
- 404 if Ocelot is unable to map an internal error code to a HTTP status code.
 | 
			
		||||
 | 
			
		||||
@@ -42,6 +42,7 @@ Thanks for taking a look at the Ocelot documentation. Please use the left hand n
 | 
			
		||||
   features/loadbalancer
 | 
			
		||||
   features/delegatinghandlers
 | 
			
		||||
   features/raft
 | 
			
		||||
   features/errorcodes
 | 
			
		||||
 | 
			
		||||
.. toctree::
 | 
			
		||||
   :maxdepth: 2
 | 
			
		||||
 
 | 
			
		||||
@@ -39,6 +39,7 @@
 | 
			
		||||
        CannotAddPlaceholderError = 34,
 | 
			
		||||
        CannotRemovePlaceholderError = 35,
 | 
			
		||||
        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
 | 
			
		||||
{
 | 
			
		||||
    public class ConnectionToDownstreamServiceError : Error
 | 
			
		||||
    {
 | 
			
		||||
        public ConnectionToDownstreamServiceError(Exception exception)
 | 
			
		||||
            : base($"Error connecting to downstream service, exception: {exception}", OcelotErrorCode.ConnectionToDownstreamServiceError)
 | 
			
		||||
        {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -4,6 +4,7 @@ namespace Ocelot.Requester
 | 
			
		||||
    using Microsoft.Extensions.DependencyInjection;
 | 
			
		||||
    using System;
 | 
			
		||||
    using System.Collections.Generic;
 | 
			
		||||
    using System.Net.Http;
 | 
			
		||||
 | 
			
		||||
    public class HttpExeptionToErrorMapper : IExceptionToErrorMapper
 | 
			
		||||
    {
 | 
			
		||||
@@ -28,6 +29,11 @@ namespace Ocelot.Requester
 | 
			
		||||
                return new RequestCanceledError(exception.Message);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (type == typeof(HttpRequestException))
 | 
			
		||||
            {
 | 
			
		||||
                return new ConnectionToDownstreamServiceError(exception);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return new UnableToCompleteRequestError(exception);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -38,6 +38,11 @@ namespace Ocelot.Responder
 | 
			
		||||
            if (errors.Any(e => e.Code == OcelotErrorCode.UnableToFindDownstreamRouteError))
 | 
			
		||||
            {
 | 
			
		||||
                return 404;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (errors.Any(e => e.Code == OcelotErrorCode.ConnectionToDownstreamServiceError))
 | 
			
		||||
            {
 | 
			
		||||
                return 502;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (errors.Any(e => e.Code == OcelotErrorCode.UnableToCompleteRequestError))
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,9 @@
 | 
			
		||||
using Ocelot.Errors;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
namespace Ocelot.Responder
 | 
			
		||||
{
 | 
			
		||||
    using System.Net;
 | 
			
		||||
    using Ocelot.Errors;
 | 
			
		||||
    using System.Collections.Generic;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Responder
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Map a list OceoltErrors to a single appropriate HTTP status code
 | 
			
		||||
    /// </summary>
 | 
			
		||||
 
 | 
			
		||||
@@ -141,7 +141,7 @@ namespace Ocelot.AcceptanceTests
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_return_response_500_when_using_http_one_to_talk_to_server_running_http_two()
 | 
			
		||||
        public void should_return_response_502_when_using_http_one_to_talk_to_server_running_http_two()
 | 
			
		||||
        {
 | 
			
		||||
            var port = RandomPortFinder.GetRandomPort();
 | 
			
		||||
 | 
			
		||||
@@ -177,7 +177,7 @@ namespace Ocelot.AcceptanceTests
 | 
			
		||||
                .And(x => _steps.GivenThereIsAConfiguration(configuration))
 | 
			
		||||
                .And(x => _steps.GivenOcelotIsRunning())
 | 
			
		||||
                .When(x => _steps.WhenIGetUrlOnTheApiGateway("/", httpContent))
 | 
			
		||||
                .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.InternalServerError))
 | 
			
		||||
                .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.BadGateway))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -16,6 +16,38 @@
 | 
			
		||||
        {
 | 
			
		||||
            _serviceHandler = new ServiceHandler();
 | 
			
		||||
            _steps = new Steps();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_return_bad_gateway_error_if_downstream_service_doesnt_respond()
 | 
			
		||||
        {
 | 
			
		||||
            var configuration = new FileConfiguration
 | 
			
		||||
            {
 | 
			
		||||
                ReRoutes = new List<FileReRoute>
 | 
			
		||||
                    {
 | 
			
		||||
                        new FileReRoute
 | 
			
		||||
                        {
 | 
			
		||||
                            DownstreamPathTemplate = "/",
 | 
			
		||||
                            UpstreamPathTemplate = "/",
 | 
			
		||||
                            UpstreamHttpMethod = new List<string> { "Get" },
 | 
			
		||||
                            DownstreamHostAndPorts = new List<FileHostAndPort>
 | 
			
		||||
                            {
 | 
			
		||||
                                new FileHostAndPort
 | 
			
		||||
                                {
 | 
			
		||||
                                    Host = "localhost",
 | 
			
		||||
                                    Port = 53877,
 | 
			
		||||
                                },
 | 
			
		||||
                            },
 | 
			
		||||
                            DownstreamScheme = "http",
 | 
			
		||||
                        },
 | 
			
		||||
                    },
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => _steps.GivenThereIsAConfiguration(configuration))
 | 
			
		||||
                .And(x => _steps.GivenOcelotIsRunning())
 | 
			
		||||
                .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
 | 
			
		||||
                .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.BadGateway))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
 
 | 
			
		||||
@@ -89,7 +89,7 @@ namespace Ocelot.AcceptanceTests
 | 
			
		||||
                .And(x => _steps.GivenThereIsAConfiguration(configuration))
 | 
			
		||||
                .And(x => _steps.GivenOcelotIsRunning())
 | 
			
		||||
                .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
 | 
			
		||||
                .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.InternalServerError))
 | 
			
		||||
                .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.BadGateway))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -7,6 +7,7 @@
 | 
			
		||||
    using Shouldly;
 | 
			
		||||
    using System;
 | 
			
		||||
    using System.Collections.Generic;
 | 
			
		||||
    using System.Net.Http;
 | 
			
		||||
    using System.Threading.Tasks;
 | 
			
		||||
    using Xunit;
 | 
			
		||||
 | 
			
		||||
@@ -38,6 +39,14 @@
 | 
			
		||||
            error.ShouldBeOfType<RequestCanceledError>();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_return_ConnectionToDownstreamServiceError()
 | 
			
		||||
        {
 | 
			
		||||
            var error = _mapper.Map(new HttpRequestException());
 | 
			
		||||
 | 
			
		||||
            error.ShouldBeOfType<ConnectionToDownstreamServiceError>();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_return_request_canceled_for_subtype()
 | 
			
		||||
        {
 | 
			
		||||
 
 | 
			
		||||
@@ -50,6 +50,13 @@ namespace Ocelot.UnitTests.Responder
 | 
			
		||||
        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)
 | 
			
		||||
        {
 | 
			
		||||
            ShouldMapErrorToStatusCode(errorCode, HttpStatusCode.BadGateway);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Theory]
 | 
			
		||||
@@ -125,7 +132,7 @@ namespace Ocelot.UnitTests.Responder
 | 
			
		||||
            // 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
 | 
			
		||||
            // 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)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user