Skip to content

Commit

Permalink
Merge pull request #142 from kolan72/dev
Browse files Browse the repository at this point in the history
Update main before release.
  • Loading branch information
kolan72 authored Dec 25, 2024
2 parents be5d25c + 0d1f66c commit 0406991
Show file tree
Hide file tree
Showing 32 changed files with 2,236 additions and 77 deletions.
15 changes: 15 additions & 0 deletions .idea/.idea.PoliNorError/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/CatchBlockHandlers/CatchBlockHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ protected CatchBlockHandler(CatchBlockFilter catchBlockFilter)

internal PolicyProcessor.ExceptionFilter ErrorFilter => CatchBlockFilter.ErrorFilter;

internal IBulkErrorProcessor BulkErrorProcessor { get; } = new BulkErrorProcessor();
internal void SetBulkErrorProcessor(IBulkErrorProcessor processor)
{
BulkErrorProcessor = processor;
}

internal IBulkErrorProcessor BulkErrorProcessor { get; private set; } = new BulkErrorProcessor();
}

/// <summary>
Expand Down
16 changes: 14 additions & 2 deletions src/CatchBlockHandlers/ErrorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,30 @@ protected ErrorContext(T t)
public abstract ProcessingErrorContext ToProcessingErrorContext();
}

internal sealed class EmptyErrorContext : ErrorContext<Unit>
internal class EmptyErrorContext : ErrorContext<Unit>
{
public static EmptyErrorContext Default { get; } = new EmptyErrorContext();

public static EmptyErrorContext DefaultFallback { get; } = new EmptyErrorContext() { PolicyKind = PolicyAlias.Fallback };

public static EmptyErrorContext DefaultSimple { get; } = new EmptyErrorContext() { PolicyKind = PolicyAlias.Simple };

private EmptyErrorContext() : base(Unit.Default){}
protected EmptyErrorContext() : base(Unit.Default){}

public PolicyAlias PolicyKind { get; private set; } = PolicyAlias.NotSet;

public override ProcessingErrorContext ToProcessingErrorContext() => new ProcessingErrorContext(PolicyKind);
}

internal class EmptyErrorContext<TParam> : EmptyErrorContext
{
public EmptyErrorContext(TParam param)
{
Param = param;
}

public TParam Param { get; private set; }

public override ProcessingErrorContext ToProcessingErrorContext() => new ProcessingErrorContext<TParam>(PolicyKind, Param);
}
}
18 changes: 9 additions & 9 deletions src/ErrorProcessors/BulkErrorProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public class BulkErrorProcessor : IBulkErrorProcessor
{
private readonly List<IErrorProcessor> _errorProcessors = new List<IErrorProcessor>();

private readonly PolicyAlias _policyAlias;
public BulkErrorProcessor() {}

public BulkErrorProcessor(): this(PolicyAlias.NotSet) {}

public BulkErrorProcessor(PolicyAlias policyAlias)
{
_policyAlias = policyAlias;
}
#pragma warning disable S1133 // Deprecated code should be removed
[Obsolete("This constructor is obsolete. Use parameterless constructor instead.")]
#pragma warning restore S1133 // Deprecated code should be removed
#pragma warning disable RCS1163 // Unused parameter.
public BulkErrorProcessor(PolicyAlias policyAlias){}
#pragma warning restore RCS1163 // Unused parameter.

public void AddProcessor(IErrorProcessor errorProcessor)
{
Expand All @@ -33,7 +33,7 @@ public BulkProcessResult Process(Exception handlingError, ProcessingErrorContext
return new BulkProcessResult(handlingError, errorProcessorExceptions);
}

var catchBlockProcessErrorInfo = errorContext.ToProcessingErrorInfo(_policyAlias);
var catchBlockProcessErrorInfo = errorContext.ToProcessingErrorInfo();
var curError = handlingError;
var isCanceledBetweenProcessOne = false;
foreach (var errorProcessor in _errorProcessors)
Expand Down Expand Up @@ -83,7 +83,7 @@ public async Task<BulkProcessResult> ProcessAsync(Exception handlingError, Proce
return new BulkProcessResult(handlingError, errorProcessorExceptions);
}

var catchBlockProcessErrorInfo = errorContext.ToProcessingErrorInfo(_policyAlias);
var catchBlockProcessErrorInfo = errorContext.ToProcessingErrorInfo();
var curError = handlingError;
var isCanceledBetweenProcessOne = false;
foreach (var errorProcessor in _errorProcessors)
Expand Down
133 changes: 133 additions & 0 deletions src/ErrorProcessors/DefaultErrorProcessor.T.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace PoliNorError
{
public class DefaultErrorProcessor<TParam> : IErrorProcessor
{
private readonly DefaultErrorProcessorT _errorProcessor;
public DefaultErrorProcessor(Action<Exception, ProcessingErrorInfo<TParam>> actionProcessor)
{
_errorProcessor = DefaultErrorProcessorT.Create(actionProcessor);
}

public DefaultErrorProcessor(Action<Exception, ProcessingErrorInfo<TParam>, CancellationToken> actionProcessor)
{
_errorProcessor = DefaultErrorProcessorT.Create(actionProcessor);
}

public DefaultErrorProcessor(Action<Exception, ProcessingErrorInfo<TParam>> actionProcessor, CancellationType cancellationType)
{
_errorProcessor = DefaultErrorProcessorT.Create(actionProcessor, cancellationType);
}

public DefaultErrorProcessor(Func<Exception, ProcessingErrorInfo<TParam>, Task> funcProcessor)
{
_errorProcessor = DefaultErrorProcessorT.Create(funcProcessor);
}

public DefaultErrorProcessor(Func<Exception, ProcessingErrorInfo<TParam>, CancellationToken, Task> funcProcessor)
{
_errorProcessor = DefaultErrorProcessorT.Create(funcProcessor);
}

public DefaultErrorProcessor(Func<Exception, ProcessingErrorInfo<TParam>, Task> funcProcessor, CancellationType cancellationType)
{
_errorProcessor = DefaultErrorProcessorT.Create(funcProcessor, cancellationType);
}

public Exception Process(Exception error, ProcessingErrorInfo catchBlockProcessErrorInfo = null, CancellationToken cancellationToken = default)
{
return _errorProcessor.Process(error, catchBlockProcessErrorInfo, cancellationToken);
}

public async Task<Exception> ProcessAsync(Exception error, ProcessingErrorInfo catchBlockProcessErrorInfo = null, bool configAwait = false, CancellationToken cancellationToken = default)
{
return await _errorProcessor.ProcessAsync(error, catchBlockProcessErrorInfo, configAwait, cancellationToken).ConfigureAwait(configAwait);
}
}

internal class DefaultErrorProcessorT : ErrorProcessorBase<ProcessingErrorInfo>
{
public static DefaultErrorProcessorT Create<TParam>(Action<Exception, ProcessingErrorInfo<TParam>> actionProcessor)
{
var action = ConvertToNonGenericAction(actionProcessor);
var res = new DefaultErrorProcessorT();
res.SetSyncRunner(action);
return res;
}

public static DefaultErrorProcessorT Create<TParam>(Action<Exception, ProcessingErrorInfo<TParam>, CancellationToken> actionProcessor)
{
void action(Exception ex, ProcessingErrorInfo pi, CancellationToken token)
{
if (pi is ProcessingErrorInfo<TParam> gpi)
actionProcessor(ex, gpi, token);
}
var res = new DefaultErrorProcessorT();
res.SetSyncRunner(action);
return res;
}

public static DefaultErrorProcessorT Create<TParam>(Action<Exception, ProcessingErrorInfo<TParam>> actionProcessor, CancellationType cancellationType)
{
var action = ConvertToNonGenericAction(actionProcessor);
var res = new DefaultErrorProcessorT();
res.SetSyncRunner(action, cancellationType);
return res;
}

public static DefaultErrorProcessorT Create<TParam>(Func<Exception, ProcessingErrorInfo<TParam>, Task> funcProcessor)
{
var func = ConvertToNonGenericFunc(funcProcessor);
var res = new DefaultErrorProcessorT();
res.SetAsyncRunner(func);
return res;
}

public static DefaultErrorProcessorT Create<TParam>(Func<Exception, ProcessingErrorInfo<TParam>, CancellationToken, Task> funcProcessor)
{
Task fn(Exception ex, ProcessingErrorInfo pi, CancellationToken token)
{
if (pi is ProcessingErrorInfo<TParam> gpi)
return funcProcessor(ex, gpi, token);
else
return Task.CompletedTask;
}
var res = new DefaultErrorProcessorT();
res.SetAsyncRunner(fn);
return res;
}

public static DefaultErrorProcessorT Create<TParam>(Func<Exception, ProcessingErrorInfo<TParam>, Task> funcProcessor, CancellationType cancellationType)
{
var func = ConvertToNonGenericFunc(funcProcessor);
var res = new DefaultErrorProcessorT();
res.SetAsyncRunner(func, cancellationType);
return res;
}

private static Action<Exception, ProcessingErrorInfo> ConvertToNonGenericAction<TParam>(Action<Exception, ProcessingErrorInfo<TParam>> actionProcessor)
{
return (Exception ex, ProcessingErrorInfo pi) =>
{
if (pi is ProcessingErrorInfo<TParam> gpi)
actionProcessor(ex, gpi);
};
}

private static Func<Exception, ProcessingErrorInfo, Task> ConvertToNonGenericFunc<TParam>(Func<Exception, ProcessingErrorInfo<TParam>, Task> funcProcessor)
{
return (Exception ex, ProcessingErrorInfo pi) =>
{
if (pi is ProcessingErrorInfo<TParam> gpi)
return funcProcessor(ex, gpi);
else
return Task.CompletedTask;
};
}

protected override Func<ProcessingErrorInfo, ProcessingErrorInfo> ParameterConverter => (_) => _;
}
}
2 changes: 1 addition & 1 deletion src/ErrorProcessors/ErrorProcessorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public async Task<Exception> ProcessAsync(Exception error, ProcessingErrorInfo c
if (NoRunners)
return error;

await(_asyncRunner ?? _syncRunner).RunAsync(error, ParameterConverter(catchBlockProcessErrorInfo), configAwait, cancellationToken);
await(_asyncRunner ?? _syncRunner).RunAsync(error, ParameterConverter(catchBlockProcessErrorInfo), configAwait, cancellationToken).ConfigureAwait(configAwait);
return error;
}

Expand Down
16 changes: 16 additions & 0 deletions src/ErrorProcessors/ProcessingErrorContext.T.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace PoliNorError
{
public class ProcessingErrorContext<TParam> : ProcessingErrorContext
{
public ProcessingErrorContext(PolicyAlias policyKind, TParam param) : base(policyKind)
{
Param = param;
}
public TParam Param { get; set; }

internal override ProcessingErrorInfo ToProcessingErrorInfo()
{
return new ProcessingErrorInfo<TParam>(this);
}
}
}
7 changes: 2 additions & 5 deletions src/ErrorProcessors/ProcessingErrorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ public class ProcessingErrorContext

internal PolicyAlias PolicyKind { get; set; }

internal virtual ProcessingErrorInfo ToProcessingErrorInfo(PolicyAlias policyAlias)
internal virtual ProcessingErrorInfo ToProcessingErrorInfo()
{
if (policyAlias != PolicyAlias.NotSet)
return new ProcessingErrorInfo(policyAlias, this);
else
return new ProcessingErrorInfo(this);
return new ProcessingErrorInfo(this);
}
}
}
16 changes: 16 additions & 0 deletions src/ErrorProcessors/ProcessingErrorInfo.T.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace PoliNorError
{
public class ProcessingErrorInfo<TParam> : ProcessingErrorInfo
{
internal ProcessingErrorInfo(ProcessingErrorContext<TParam> currentContext) : this(currentContext.PolicyKind, currentContext) { }

public ProcessingErrorInfo(PolicyAlias policyKind, ProcessingErrorContext<TParam> currentContext = null) : base(policyKind, currentContext)
{
if (currentContext != null)
{
Param = currentContext.Param;
}
}
public TParam Param { get; private set; }
}
}
4 changes: 2 additions & 2 deletions src/Fallback/DefaultFallbackProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace PoliNorError
public sealed class DefaultFallbackProcessor : PolicyProcessor, IFallbackProcessor
{
private readonly EmptyErrorContext _emptyErrorContext;
public DefaultFallbackProcessor(IBulkErrorProcessor bulkErrorProcessor = null) : base(PolicyAlias.Fallback, bulkErrorProcessor)
public DefaultFallbackProcessor(IBulkErrorProcessor bulkErrorProcessor = null) : base(bulkErrorProcessor)
{
_emptyErrorContext = _isPolicyAliasSet ? EmptyErrorContext.Default : EmptyErrorContext.DefaultFallback;
_emptyErrorContext = EmptyErrorContext.DefaultFallback;
}

public PolicyResult Fallback(Action action, Action<CancellationToken> fallback, CancellationToken token = default)
Expand Down
14 changes: 8 additions & 6 deletions src/Policy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ protected async Task HandlePolicyResultAsync<T>(PolicyResult<T> policyRetryResul

internal void SetWrap(IPolicyBase policyToWrap)
{
if (_policyWrapperFactory != null)
if (HasPolicyWrapperFactory)
{
throw new NotImplementedException("More than one wrapped policy is not supported.");
}
Expand All @@ -120,7 +120,7 @@ internal void SetWrap(IPolicyBase policyToWrap)

internal void SetWrap(IEnumerable<IPolicyBase> policies, ThrowOnWrappedCollectionFailed throwOnWrappedCollectionFailed)
{
if (_policyWrapperFactory != null)
if (HasPolicyWrapperFactory)
{
throw new ArgumentException("More than one wrapped PolicyCollection is not supported.");
}
Expand All @@ -133,7 +133,7 @@ internal void SetWrap(IEnumerable<IPolicyBase> policies, ThrowOnWrappedCollectio

internal (Action Act, PolicyWrapper Wrapper) WrapDelegateIfNeed(Action action, CancellationToken token)
{
if (_policyWrapperFactory == null)
if (!HasPolicyWrapperFactory)
{
return (action, null);
}
Expand All @@ -149,7 +149,7 @@ internal void SetWrap(IEnumerable<IPolicyBase> policies, ThrowOnWrappedCollectio

internal (Func<T> Fn, PolicyWrapper<T> Wrapper) WrapDelegateIfNeed<T>(Func<T> fn, CancellationToken token)
{
if (_policyWrapperFactory == null)
if (!HasPolicyWrapperFactory)
{
return (fn, null);
}
Expand All @@ -165,7 +165,7 @@ internal void SetWrap(IEnumerable<IPolicyBase> policies, ThrowOnWrappedCollectio

internal (Func<CancellationToken, Task> Fn, PolicyWrapper Wrapper) WrapDelegateIfNeed(Func<CancellationToken, Task> fn, CancellationToken token, bool configureAwait)
{
if (_policyWrapperFactory == null)
if (!HasPolicyWrapperFactory)
{
return (fn, null);
}
Expand All @@ -181,7 +181,7 @@ internal void SetWrap(IEnumerable<IPolicyBase> policies, ThrowOnWrappedCollectio

internal (Func<CancellationToken, Task<T>> Fn, PolicyWrapper<T> Wrapper) WrapDelegateIfNeed<T>(Func<CancellationToken, Task<T>> fn, CancellationToken token, bool configureAwait)
{
if (_policyWrapperFactory == null)
if (!HasPolicyWrapperFactory)
{
return (fn, null);
}
Expand All @@ -195,6 +195,8 @@ internal void SetWrap(IEnumerable<IPolicyBase> policies, ThrowOnWrappedCollectio
}
}

protected bool HasPolicyWrapperFactory => _policyWrapperFactory != null;

public void ResetWrap()
{
_policyWrapperFactory = null;
Expand Down
Loading

0 comments on commit 0406991

Please sign in to comment.