Feature/inject error mapper (#562)

* added delegate to select last handler

* #529 implemented a way we can inject the last delegating handler

* wip - moving code

* #529 removed loads of qos code and moved it into Ocelot.Provider.Polly

* #529 can now inject http client expcetions to ocelot errors mappers and updated docs
This commit is contained in:
Tom Pallister
2018-08-19 12:57:43 +01:00
committed by GitHub
parent 98ba0271be
commit 6d8b18c01d
13 changed files with 177 additions and 317 deletions

View File

@ -7,8 +7,6 @@
int durationofBreak,
int timeoutValue,
string key,
//todo - this is never set in Ocelot so always Pessimistic...I guess it doesn't
//matter to much.
string timeoutStrategy = "Pessimistic")
{
ExceptionsAllowedBeforeBreaking = exceptionsAllowedBeforeBreaking;

View File

@ -130,6 +130,7 @@ namespace Ocelot.DependencyInjection
Services.TryAddSingleton<IDownstreamRequestCreator, DownstreamRequestCreator>();
Services.TryAddSingleton<IFrameworkDescription, FrameworkDescription>();
Services.TryAddSingleton<IQoSFactory, QoSFactory>();
Services.TryAddSingleton<IExceptionToErrorMapper, HttpExeptionToErrorMapper>();
}
public IOcelotBuilder AddSingletonDefinedAggregator<T>()

View File

@ -12,14 +12,17 @@ namespace Ocelot.Requester
private readonly IHttpClientCache _cacheHandlers;
private readonly IOcelotLogger _logger;
private readonly IDelegatingHandlerHandlerFactory _factory;
private readonly IExceptionToErrorMapper _mapper;
public HttpClientHttpRequester(IOcelotLoggerFactory loggerFactory,
IHttpClientCache cacheHandlers,
IDelegatingHandlerHandlerFactory house)
IDelegatingHandlerHandlerFactory factory,
IExceptionToErrorMapper mapper)
{
_logger = loggerFactory.CreateLogger<HttpClientHttpRequester>();
_cacheHandlers = cacheHandlers;
_factory = house;
_factory = factory;
_mapper = mapper;
}
public async Task<Response<HttpResponseMessage>> GetResponse(DownstreamContext context)
@ -35,7 +38,8 @@ namespace Ocelot.Requester
}
catch (Exception exception)
{
return new ErrorResponse<HttpResponseMessage>(new UnableToCompleteRequestError(exception));
var error = _mapper.Map(exception);
return new ErrorResponse<HttpResponseMessage>(error);
}
finally
{

View File

@ -0,0 +1,29 @@
namespace Ocelot.Requester
{
using System;
using System.Collections.Generic;
using Errors;
using Microsoft.Extensions.DependencyInjection;
public class HttpExeptionToErrorMapper : IExceptionToErrorMapper
{
private readonly Dictionary<Type, Func<Exception, Error>> _mappers;
public HttpExeptionToErrorMapper(IServiceProvider serviceProvider)
{
_mappers = serviceProvider.GetService<Dictionary<Type, Func<Exception, Error>>>();
}
public Error Map(Exception exception)
{
var type = exception.GetType();
if (_mappers != null && _mappers.ContainsKey(type))
{
return _mappers[type](exception);
}
return new UnableToCompleteRequestError(exception);
}
}
}

View File

@ -0,0 +1,10 @@
using System;
using Ocelot.Errors;
namespace Ocelot.Requester
{
public interface IExceptionToErrorMapper
{
Error Map(Exception exception);
}
}

View File

@ -1,10 +0,0 @@
using System.Net.Http;
namespace Ocelot.Requester
{
public class ReRouteDelegatingHandler<T>
where T : DelegatingHandler
{
public T DelegatingHandler { get; private set; }
}
}

View File

@ -1,12 +0,0 @@
using Ocelot.Errors;
namespace Ocelot.Requester
{
public class UnableToFindDelegatingHandlerProviderError : Error
{
public UnableToFindDelegatingHandlerProviderError(string message)
: base(message, OcelotErrorCode.UnableToFindDelegatingHandlerProviderError)
{
}
}
}