-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Introduces error filters * Converts existing error policies to error filters
- Loading branch information
Showing
31 changed files
with
1,134 additions
and
595 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace Nybus.Filters | ||
{ | ||
public interface IErrorFilter | ||
{ | ||
Task HandleErrorAsync<TCommand>(ICommandContext<TCommand> context, Exception exception, CommandErrorDelegate<TCommand> next) where TCommand : class, ICommand; | ||
|
||
Task HandleErrorAsync<TEvent>(IEventContext<TEvent> context, Exception exception, EventErrorDelegate<TEvent> next) where TEvent : class, IEvent; | ||
} | ||
|
||
public delegate Task CommandErrorDelegate<TCommand>(ICommandContext<TCommand> context, Exception exception) where TCommand : class, ICommand; | ||
|
||
public delegate Task EventErrorDelegate<TEvent>(IEventContext<TEvent> context, Exception exception) where TEvent : class, IEvent; | ||
|
||
public interface IErrorFilterProvider | ||
{ | ||
string ProviderName { get; } | ||
|
||
IErrorFilter CreateErrorFilter(IConfigurationSection settings); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Nybus.Utils | ||
{ | ||
public static class EnumerableExtensions | ||
{ | ||
public static IEnumerable<T> NotNull<T>(this IEnumerable<T> items) where T : class => items.Where(i => i != null); | ||
|
||
public static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T> items) => items ?? Array.Empty<T>(); | ||
|
||
public static IEnumerable<T> IfNull<T>(this IEnumerable<T> items, IEnumerable<T> alternative) => items ?? alternative; | ||
} | ||
} |
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,70 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Nybus.Filters | ||
{ | ||
public class DiscardErrorFilterProvider : IErrorFilterProvider | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
|
||
public DiscardErrorFilterProvider(IServiceProvider serviceProvider) | ||
{ | ||
_serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); | ||
} | ||
|
||
public string ProviderName { get; } = "discard"; | ||
|
||
public IErrorFilter CreateErrorFilter(IConfigurationSection settings) | ||
{ | ||
var engine = _serviceProvider.GetRequiredService<IBusEngine>(); | ||
var logger = _serviceProvider.GetRequiredService<ILogger<DiscardErrorFilter>>(); | ||
|
||
return new DiscardErrorFilter(engine, logger); | ||
} | ||
} | ||
|
||
public class DiscardErrorFilter : IErrorFilter | ||
{ | ||
private readonly IBusEngine _engine; | ||
private readonly ILogger<DiscardErrorFilter> _logger; | ||
|
||
public DiscardErrorFilter(IBusEngine engine, ILogger<DiscardErrorFilter> logger) | ||
{ | ||
_engine = engine ?? throw new ArgumentNullException(nameof(engine)); | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
public async Task HandleErrorAsync<TCommand>(ICommandContext<TCommand> context, Exception exception, CommandErrorDelegate<TCommand> next) | ||
where TCommand : class, ICommand | ||
{ | ||
try | ||
{ | ||
await _engine.NotifyFailAsync(context.Message).ConfigureAwait(false); | ||
} | ||
catch (Exception discardException) | ||
{ | ||
_logger.LogError(discardException, ex => $"Unable to discard message: {ex.Message}"); | ||
await next(context, exception).ConfigureAwait(false); | ||
} | ||
} | ||
|
||
public async Task HandleErrorAsync<TEvent>(IEventContext<TEvent> context, Exception exception, EventErrorDelegate<TEvent> next) | ||
where TEvent : class, IEvent | ||
{ | ||
try | ||
{ | ||
await _engine.NotifyFailAsync(context.Message).ConfigureAwait(false); | ||
} | ||
catch (Exception discardException) | ||
{ | ||
_logger.LogError(discardException, ex => $"Unable to discard message: {ex.Message}"); | ||
await next(context, exception).ConfigureAwait(false); | ||
} | ||
} | ||
} | ||
} |
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,27 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Nybus.Filters | ||
{ | ||
public class FallbackErrorFilter : IErrorFilter | ||
{ | ||
private readonly IBusEngine _engine; | ||
|
||
public FallbackErrorFilter(IBusEngine engine) | ||
{ | ||
_engine = engine ?? throw new ArgumentNullException(nameof(engine)); | ||
} | ||
|
||
public Task HandleErrorAsync<TCommand>(ICommandContext<TCommand> context, Exception exception, CommandErrorDelegate<TCommand> next) | ||
where TCommand : class, ICommand | ||
{ | ||
return _engine.NotifyFailAsync(context.Message); | ||
} | ||
|
||
public Task HandleErrorAsync<TEvent>(IEventContext<TEvent> context, Exception exception, EventErrorDelegate<TEvent> next) | ||
where TEvent : class, IEvent | ||
{ | ||
return _engine.NotifyFailAsync(context.Message); | ||
} | ||
} | ||
} |
Oops, something went wrong.