Skip to content

Commit

Permalink
add FinallyThrow
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusz96 committed Jul 31, 2024
1 parent 464558a commit 0bf3db7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

namespace PipelineNet.ChainsOfResponsibility
{
public static class AsyncResponsibilityChainExtensions
{
/// <summary>
/// Throws an <see cref="InvalidOperationException"/> at the end of the chain as a fallback.
/// A chain can only have one finally function. Calling this method more
/// a second time will just replace the existing finally <see cref="Func{TParameter, TResult}"/>.
/// </summary>
/// <param name="chain">The instace of <see cref="IAsyncResponsibilityChain{TParameter, TReturn}"/>.</param>
/// <returns>The current instance of <see cref="IAsyncResponsibilityChain{TParameter, TReturn}"/>.</returns>
public static IAsyncResponsibilityChain<TParameter, TReturn> FinallyThrow<TParameter, TReturn>(
this IAsyncResponsibilityChain<TParameter, TReturn> chain)
{
if (chain == null) throw new ArgumentNullException("chain");

return chain.Finally(_ =>
throw new InvalidOperationException("End of the asynchronous chain of responsibility reached. No middleware matches returned a value."));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

namespace PipelineNet.ChainsOfResponsibility
{
public static class ResponsibilityChainExtensions
{
/// <summary>
/// Throws an <see cref="InvalidOperationException"/> at the end of the chain as a fallback.
/// A chain can only have one finally function. Calling this method more
/// a second time will just replace the existing finally <see cref="Func{TParameter, TResult}"/>.
/// </summary>
/// <param name="chain">The instace of <see cref="IResponsibilityChain{TParameter, TReturn}"/>.</param>
/// <returns>The current instance of <see cref="IResponsibilityChain{TParameter, TReturn}"/>.</returns>
public static IResponsibilityChain<TParameter, TReturn> FinallyThrow<TParameter, TReturn>(
this IResponsibilityChain<TParameter, TReturn> chain)
{
if (chain == null) throw new ArgumentNullException("chain");

return chain.Finally(_ =>
throw new InvalidOperationException("End of the chain of responsibility reached. No middleware matches returned a value."));
}
}
}

0 comments on commit 0bf3db7

Please sign in to comment.