public OutcomeFinalizer <T, ReturnType> Catch <ExceptionType>(Func <ExceptionType, ReturnType> handler) where ExceptionType : Exception { if (!_isHandled && !_outcome.IsSuccessful && _outcome.FailureOrThrow().AsException() is ExceptionType exception) { _isHandled = true; _returnValue = handler(exception); } return(this); }
public static Outcome <TResult> Reduce <T, TResult>( this Outcome <IEnumerable <T> > @this, Func <TResult, T, Outcome <TResult> > generator) { if ([email protected]) { return(Outcome <TResult> .Reject(@this.FailureOrThrow())); } var parameters = @this.ResultOrDefault(); TResult previous = default; foreach (var param in parameters) { var current = generator(previous, param); if (current.IsSuccessful) { previous = current.ResultOrDefault(); } else { return(Outcome <TResult> .Reject( $"{nameof(generator)} function failed for the parameter value {param}")); } } return(previous); }
public static Outcome <IEnumerable <ResultType> > ThenForEach <T, ResultType>(this Outcome <IEnumerable <T> > @this, Func <ResultType> fn) { if ([email protected]) { return(Outcome <IEnumerable <ResultType> > .Reject(@this.FailureOrThrow())); } return(Outcome.Of(() => @this.ResultOrDefault(Enumerable.Empty <T>()).Select(_ => fn()))); }
public static Outcome <T> Catch <T>(this Outcome <T> @this, Func <Failure, T> fn) { if (IsIgnorable(@this)) { return(@this); } return(Outcome.Of(() => fn(@this.FailureOrThrow()))); }
public static Outcome <T> Catch <T>(this Outcome <T> @this, Filter filter, Func <Failure, T> fn) { if (!IsCatchable(@this, filter)) { return(@this); } return(Outcome.Of(() => fn(@this.FailureOrThrow()))); }
public static Outcome <T> TapFailure <T>(this Outcome <T> @this, Action <Failure> action) { if (IsIgnorable(@this)) { return(@this); } return(Try(() => { action(@this.FailureOrThrow()); return ToKnownFailed(@this); })); }
public static Outcome <T> TapFailure <T>(this Outcome <T> @this, Func <Failure, bool> filter, Action <Failure> action) { if (!IsCatchable(@this, filter)) { return(@this); } return(Try(() => { action(@this.FailureOrThrow()); return ToKnownFailed(@this); })); }
public static Outcome <T> Catch <T>(this Outcome <T> @this, Func <Failure, Outcome <T> > fn) { if (IsIgnorable(@this)) { return(@this); } try { return(fn(@this.FailureOrThrow())); } catch (Exception ex) { return(Fail(ex)); } }
public static Outcome <T> Filter <T>(this Outcome <T> @this, T matchValue, IComparer <T> comparer) { if (IsUnprocessable(@this)) { return(@this); } T result = @this.IsSuccessful ? @this.ResultOrThrow() : (@this.FailureOrThrow() as ExpectationFailure <T>).ResultAtSource; if (comparer.Compare(result, matchValue) != 0) { return(new ExpectationFailure <T>(result)); } return(@this); }
public static Outcome <T> Catch <T>(this Outcome <T> @this, Filter filter, Func <Failure, Outcome <T>, Outcome <T> > fn) { if (!IsCatchable(@this, filter)) { return(@this); } try { return(fn(@this.FailureOrThrow(), ToKnownFailed(@this))); } catch (Exception ex) { return(Fail(ex)); } }
/// <summary> /// Applies the given function on all elements of the input enumerable and /// returns an enumerable of the results. /// </summary> /// <typeparam name="TResult">The return type of the generator function.</typeparam> /// <typeparam name="TParam">The element type of the input enumerable.</typeparam> /// <param name="parameters">The input enumerable.</param> /// <param name="generator">The function to be applied.</param> /// <returns>The enumerable of results.</returns> public static Outcome <IEnumerable <TResult> > MapAll <T, TResult>( this Outcome <IEnumerable <T> > @this, Func <T, TResult> fn) { if ([email protected]) { return(Outcome <IEnumerable <TResult> > .Reject(@this.FailureOrThrow())); } return(Outcome.Of( () => @this .ResultOrDefault(Enumerable.Empty <T>()) .Select(r => fn(r)) )); }
public static Outcome <T> Filter <T>(this Outcome <T> @this, Func <T, bool> predicate) { if (IsUnprocessable(@this)) { return(@this); } T result = @this.IsSuccessful ? @this.ResultOrThrow() : (@this.FailureOrThrow() as ExpectationFailure <T>).ResultAtSource; return(Try(() => { if (!predicate(result)) { return new ExpectationFailure <T>(result); } return @this; })); }
/// <summary> /// Applies the given async function on all elements of the input enumerable and /// returns an enumerable of the results. /// </summary> /// <typeparam name="TResult">The return type of the generator function.</typeparam> /// <typeparam name="TParam">The element type of the input enumerable.</typeparam> /// <param name="parameters">The input enumerable.</param> /// <param name="generator">The async function to be applied.</param> /// <returns>The enumerable of results.</returns> public static async Task <Outcome <IEnumerable <TResult> > > MapAll <T, TResult>( this Outcome <IEnumerable <T> > @this, Func <Task <TResult> > fn) { if (@this.IsSuccessful) { var source = @this.ResultOrDefault(Enumerable.Empty <T>()); int count = source is ICollection c ? c.Count : 5; var dest = new List <TResult>(count); return(await Utility.Try <IEnumerable <TResult> >(async() => { foreach (var item in source) { dest.Add(await fn()); } return dest; })); } return(@this.FailureOrThrow()); }
public static Outcome <Nop> DiscardResult <T>(this Outcome <T> @this) => @this.IsSuccessful ? Outcome.Any() : Outcome.Reject(@this.FailureOrThrow());