mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 09:15:27 +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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -56,18 +56,11 @@ namespace Ocelot.Responder.Middleware
 | 
			
		||||
            _logger.LogTrace($"completed {MiddlwareName}");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void SetErrorResponse(HttpContext context, List<Error> errors)
 | 
			
		||||
        {
 | 
			
		||||
        private void SetErrorResponse(HttpContext context, List<Error> errors)
 | 
			
		||||
        {
 | 
			
		||||
            var statusCode = _codeMapper.Map(errors);
 | 
			
		||||
 | 
			
		||||
            if (!statusCode.IsError)
 | 
			
		||||
            {
 | 
			
		||||
                _responder.SetErrorResponseOnContext(context, statusCode.Data);
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                _responder.SetErrorResponseOnContext(context, 500);
 | 
			
		||||
            }
 | 
			
		||||
            _responder.SetErrorResponseOnContext(context, statusCode);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user