forked from ThreeMammals/Ocelot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/inject error mapper (ThreeMammals#562)
* added delegate to select last handler * ThreeMammals#529 implemented a way we can inject the last delegating handler * wip - moving code * ThreeMammals#529 removed loads of qos code and moved it into Ocelot.Provider.Polly * ThreeMammals#529 can now inject http client expcetions to ocelot errors mappers and updated docs
- Loading branch information
1 parent
98ba027
commit 6d8b18c
Showing
13 changed files
with
177 additions
and
317 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
using Ocelot.Errors; | ||
|
||
namespace Ocelot.Requester | ||
{ | ||
public interface IExceptionToErrorMapper | ||
{ | ||
Error Map(Exception exception); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
src/Ocelot/Requester/UnableToFindDelegatingHandlerProviderError.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
test/Ocelot.UnitTests/Requester/HttpExeptionToErrorMapperTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
namespace Ocelot.UnitTests.Requester | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Ocelot.Errors; | ||
using Ocelot.Requester; | ||
using Responder; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
public class HttpExeptionToErrorMapperTests | ||
{ | ||
private HttpExeptionToErrorMapper _mapper; | ||
private readonly ServiceCollection _services; | ||
|
||
public HttpExeptionToErrorMapperTests() | ||
{ | ||
_services = new ServiceCollection(); | ||
var provider = _services.BuildServiceProvider(); | ||
_mapper = new HttpExeptionToErrorMapper(provider); | ||
} | ||
|
||
[Fact] | ||
public void should_return_default_error_because_mappers_are_null() | ||
{ | ||
var error = _mapper.Map(new Exception()); | ||
|
||
error.ShouldBeOfType<UnableToCompleteRequestError>(); | ||
} | ||
|
||
[Fact] | ||
public void should_return_error_from_mapper() | ||
{ | ||
var errorMapping = new Dictionary<Type, Func<Exception, Error>> | ||
{ | ||
{typeof(TaskCanceledException), e => new AnyError()}, | ||
}; | ||
|
||
_services.AddSingleton(errorMapping); | ||
|
||
var provider = _services.BuildServiceProvider(); | ||
|
||
_mapper = new HttpExeptionToErrorMapper(provider); | ||
|
||
var error = _mapper.Map(new TaskCanceledException()); | ||
|
||
error.ShouldBeOfType<AnyError>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
namespace Ocelot.UnitTests.Requester | ||
{ | ||
using System.Net.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Moq; | ||
using Ocelot.Configuration; | ||
using Ocelot.Configuration.Builder; | ||
using Ocelot.Logging; | ||
using Ocelot.Requester; | ||
using Ocelot.Requester.QoS; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
public class QoSFactoryTests | ||
{ | ||
private QoSFactory _factory; | ||
private ServiceCollection _services; | ||
private readonly Mock<IOcelotLoggerFactory> _loggerFactory; | ||
|
||
public QoSFactoryTests() | ||
{ | ||
_services = new ServiceCollection(); | ||
_loggerFactory = new Mock<IOcelotLoggerFactory>(); | ||
var provider = _services.BuildServiceProvider(); | ||
_factory = new QoSFactory(provider, _loggerFactory.Object); | ||
} | ||
|
||
[Fact] | ||
public void should_return_error() | ||
{ | ||
var downstreamReRoute = new DownstreamReRouteBuilder().Build(); | ||
var handler = _factory.Get(downstreamReRoute); | ||
handler.IsError.ShouldBeTrue(); | ||
handler.Errors[0].ShouldBeOfType<UnableToFindQoSProviderError>(); | ||
} | ||
|
||
[Fact] | ||
public void should_return_handler() | ||
{ | ||
_services = new ServiceCollection(); | ||
DelegatingHandler QosDelegatingHandlerDelegate(DownstreamReRoute a, IOcelotLoggerFactory b) => new FakeDelegatingHandler(); | ||
_services.AddSingleton<QosDelegatingHandlerDelegate>(QosDelegatingHandlerDelegate); | ||
var provider = _services.BuildServiceProvider(); | ||
_factory = new QoSFactory(provider, _loggerFactory.Object); | ||
var downstreamReRoute = new DownstreamReRouteBuilder().Build(); | ||
var handler = _factory.Get(downstreamReRoute); | ||
handler.IsError.ShouldBeFalse(); | ||
handler.Data.ShouldBeOfType<FakeDelegatingHandler>(); | ||
} | ||
|
||
class FakeDelegatingHandler : DelegatingHandler | ||
{ | ||
} | ||
} | ||
} |
93 changes: 0 additions & 93 deletions
93
test/Ocelot.UnitTests/Requester/QoSProviderFactoryTests.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.