mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 22:30:50 +08:00 
			
		
		
		
	Merged in develop
This commit is contained in:
		@@ -1,17 +1,16 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using Ocelot.Errors;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Responder
 | 
			
		||||
{
 | 
			
		||||
    public class ErrorsToHttpStatusCodeMapper : IErrorsToHttpStatusCodeMapper
 | 
			
		||||
    {
 | 
			
		||||
        public Response<int> Map(List<Error> errors)
 | 
			
		||||
        public int Map(List<Error> errors)
 | 
			
		||||
        {
 | 
			
		||||
            if (errors.Any(e => e.Code == OcelotErrorCode.UnauthenticatedError))
 | 
			
		||||
            {
 | 
			
		||||
                return new OkResponse<int>(401);
 | 
			
		||||
                return 401;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (errors.Any(e => e.Code == OcelotErrorCode.UnauthorizedError 
 | 
			
		||||
@@ -19,20 +18,20 @@ namespace Ocelot.Responder
 | 
			
		||||
                || e.Code == OcelotErrorCode.UserDoesNotHaveClaimError
 | 
			
		||||
                || e.Code == OcelotErrorCode.CannotFindClaimError))
 | 
			
		||||
            {
 | 
			
		||||
                return new OkResponse<int>(403);
 | 
			
		||||
                return 403;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (errors.Any(e => e.Code == OcelotErrorCode.RequestTimedOutError))
 | 
			
		||||
            {
 | 
			
		||||
                return new OkResponse<int>(503);
 | 
			
		||||
                return 503;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (errors.Any(e => e.Code == OcelotErrorCode.UnableToFindDownstreamRouteError))
 | 
			
		||||
            {
 | 
			
		||||
                return new OkResponse<int>(404);
 | 
			
		||||
                return 404;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return new OkResponse<int>(404);
 | 
			
		||||
            return 404;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,11 +1,13 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using Ocelot.Errors;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Responder
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Map a list OceoltErrors to a single appropriate HTTP status code
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public interface IErrorsToHttpStatusCodeMapper
 | 
			
		||||
    {
 | 
			
		||||
        Response<int> Map(List<Error> errors);
 | 
			
		||||
        int Map(List<Error> errors);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -60,14 +60,7 @@ namespace Ocelot.Responder.Middleware
 | 
			
		||||
        {
 | 
			
		||||
            var statusCode = _codeMapper.Map(errors);
 | 
			
		||||
 | 
			
		||||
            if (!statusCode.IsError)
 | 
			
		||||
            {
 | 
			
		||||
                _responder.SetErrorResponseOnContext(context, statusCode.Data);
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                _responder.SetErrorResponseOnContext(context, 500);
 | 
			
		||||
            }
 | 
			
		||||
            _responder.SetErrorResponseOnContext(context, statusCode);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -4,7 +4,6 @@ using Ocelot.Errors;
 | 
			
		||||
using Ocelot.Middleware;
 | 
			
		||||
using Ocelot.Requester;
 | 
			
		||||
using Ocelot.Responder;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
using Shouldly;
 | 
			
		||||
using TestStack.BDDfy;
 | 
			
		||||
using Xunit;
 | 
			
		||||
@@ -14,7 +13,7 @@ namespace Ocelot.UnitTests.Responder
 | 
			
		||||
    public class ErrorsToHttpStatusCodeMapperTests
 | 
			
		||||
    {
 | 
			
		||||
        private readonly IErrorsToHttpStatusCodeMapper _codeMapper;
 | 
			
		||||
        private Response<int> _result;
 | 
			
		||||
        private int _result;
 | 
			
		||||
        private List<Error> _errors;
 | 
			
		||||
 | 
			
		||||
        public ErrorsToHttpStatusCodeMapperTests()
 | 
			
		||||
@@ -77,7 +76,7 @@ namespace Ocelot.UnitTests.Responder
 | 
			
		||||
 | 
			
		||||
        private void ThenTheResponseIsStatusCodeIs(int expectedCode)
 | 
			
		||||
        {
 | 
			
		||||
            _result.Data.ShouldBe(expectedCode);
 | 
			
		||||
            _result.ShouldBe(expectedCode);
 | 
			
		||||
        }    
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user