2using System.Collections.Generic;
4using System.Threading.Tasks;
5using EverythingSearchClient;
12public record
class Attempt(Exception[]? Error =
null)
14 public static implicit
operator Attempt (Exception[] e) =>
new(Error: e);
15 public static implicit
operator Attempt (Exception e) =>
new(Error: [e]);
16 public static implicit
operator Exception? (
Attempt from) => from.Error?[0];
17 public static implicit
operator bool (
Attempt from) => from.Error ==
null;
23public record
class ValueOrError<T>(T? Value =
default, Exception[]? Error =
null)
26 public static implicit
operator ValueOrError<T> (Exception[]? e) =>
new(Error: e);
27 public static implicit
operator ValueOrError<T> (Exception e) =>
new(Error: [e]);
28 public static implicit
operator T? (
ValueOrError<T> from) => from.Value;
29 public static implicit
operator Exception? (
ValueOrError<T> from) => from.Error?[0];
30 public static implicit
operator bool (
ValueOrError<T> from) => from.Error ==
null;
43 public static ValueOrError<T> TryGet<T>(Func<T> getter, Action<Exception>? onFailure =
null, Exception[]? previousErrors =
null)
45 try {
return getter(); }
49 var prevErrors = previousErrors ?? [];
50 return prevErrors.Prepend(e).ToArray();
65 if (!
self)
return self.Error;
66 return TryGet(() => transform(
self.Get()));
83 if (
self)
return self;
84 return TryGet(getter, onFailure,
self.Error);
102 if (
self || !condition)
return self;
103 return TryGet(getter, onFailure,
self.Error);
116 if (
self)
return self!;
117 if (
self.Error!.Length == 1)
throw self.Error[0];
118 throw message ==
null
119 ?
new AggregateException(
self.Error!)
120 :
new AggregateException(message,
self.Error!);
131 public static Attempt Try(Action action, Action<Exception>? onFailure =
null, Exception[]? previousErrors =
null)
140 onFailure?.Invoke(e);
141 var prevErrors = previousErrors ?? [];
142 return prevErrors.Prepend(e).ToArray();
156 if (
self)
return self;
157 return Try(action, onFailure,
self.Error);
172 public static Attempt Else(
this Attempt self,
bool condition, Action action, Action<Exception>? onFailure =
null)
174 if (
self || !condition)
return self;
175 return Try(action, onFailure,
self.Error);
187 if (
self.Error!.Length == 1)
throw self.Error[0];
188 throw message ==
null
189 ?
new AggregateException(
self.Error!)
190 :
new AggregateException(message,
self.Error!);
static ValueOrError< T > TryGet< T >(Func< T > getter, Action< Exception >? onFailure=null, Exception[]? previousErrors=null)
Try to gwt a value from an input function which may throw an exception. If an exception is thrown the...
static Attempt Try(Action action, Action< Exception >? onFailure=null, Exception[]? previousErrors=null)
Attempt to try something which may throw an exception. If an exception is thrown then wrap it inside ...
static void Assume(this Attempt self, string? message=null)
Guarantee that one of the chain of attempts proceeding this function has succeeded otherwise throw th...
static ValueOrError< T > Else< T >(this ValueOrError< T > self, Func< T > getter, Action< Exception >? onFailure=null)
If input ValueOrError is an error then attempt to execute the input getter function (which may also f...
static ValueOrError< TResult > Transform< TResult, TInput >(this ValueOrError< TInput > self, Func< TInput, TResult > transform)
Work on the value inside a ValueOrError but only if input ValueOrError is valid. Return aggregated er...
static T Get< T >(this ValueOrError< T > self, string? message=null)
Guarantee the result of an input ValueOrError otherwise throw the aggregated exceptions inside the er...
static Attempt Else(this Attempt self, bool condition, Action action, Action< Exception >? onFailure=null)
If input attempt is an error then attempt to execute another input action (which may also fail) only ...
static Attempt Else(this Attempt self, Action action, Action< Exception >? onFailure=null)
If input attempt is an error then attempt to execute another input action (which may also fail)
record class Attempt(Exception[]? Error=null)
A record that can represent an attempt at an arbitrary action.
record class ValueOrError< T >(T? Value=default, Exception[]? Error=null)
A union that can hold either a correct value or an array of errors.