public static async Task <A> IfFail <A>(this TryAsync <A> self, Func <Exception, A> Fail) { if (isnull(self)) { throw new ArgumentNullException(nameof(self)); } if (isnull(Fail)) { throw new ArgumentNullException(nameof(Fail)); } try { var res = await self.Try(); if (res.IsFaulted) { return(Fail(res.Exception)); } else { return(res.Value); } } catch (Exception e) { TryConfig.ErrorLogger(e); return(Fail(e)); } }
/// <summary> /// Invoke a delegate if the Try returns a value successfully /// </summary> /// <param name="Succ">Delegate to invoke if successful</param> public static async Task <Unit> IfSucc <A>(this TryAsync <A> self, Action <A> Succ) { if (isnull(self)) { throw new ArgumentNullException(nameof(self)); } if (isnull(Succ)) { throw new ArgumentNullException(nameof(Succ)); } try { var res = await self.Try(); if (!res.IsFaulted) { Succ(res.Value); } return(unit); } catch (Exception e) { TryConfig.ErrorLogger(e); return(unit); } }
public static A IfFailThrow <A>(this TryOption <A> self) { try { var res = self(); if (res.IsBottom) { throw new BottomException(); } if (res.IsFaulted) { throw new InnerException(res.Exception); } if (res.Value.IsNone) { throw new ValueIsNoneException(); } return(res.Value.Value); } catch (Exception e) { TryConfig.ErrorLogger(e); throw; } }
public static Try <C> SelectMany <A, B, C>( this Try <A> ma, Func <A, Try <B> > bind, Func <A, B, C> project) => Memo(() => { try { var ra = ma(); if (ra.IsSuccess) { var rb = bind(ra.Value)(); if (rb.IsSuccess) { return(project(ra.Value, rb.Value)); } else { return(new Result <C>(rb.Exception)); } } else { return(new Result <C>(ra.Exception)); } } catch (Exception e) { TryConfig.ErrorLogger(e); return(new Result <C>(e)); } });
public static TryOption <U> Select <T, U>(this TryOption <T> self, Func <T, U> select) { return(new TryOption <U>(() => { TryOptionResult <T> resT; resT = self.Try(); if (resT.IsFaulted) { return new TryOptionResult <U>(resT.Exception); } if (resT.Value.IsNone) { return new TryOptionResult <U>(None); } Option <U> resU; try { resU = select(resT.Value.Value); } catch (Exception e) { TryConfig.ErrorLogger(e); return new TryOptionResult <U>(e); } return new TryOptionResult <U>(resU); })); }
public static Try <V> SelectMany <T, U, V>( this Try <T> self, Func <T, Try <U> > bind, Func <T, U, V> project ) { return(new Try <V>( () => { var resT = self.Try(); if (resT.IsFaulted) { return new TryResult <V>(resT.Exception); } var resU = bind(resT.Value).Try(); if (resU.IsFaulted) { return new TryResult <V>(resT.Exception); } try { return new TryResult <V>(project(resT.Value, resU.Value)); } catch (Exception e) { TryConfig.ErrorLogger(e); return new TryResult <V>(e); } } )); }
public static TryResult <T> Try <T>(this Try <T> self) { try { return(self()); } catch (Exception e) { TryConfig.ErrorLogger(e); return(new TryResult <T>(e)); } }
public static A IfFailThrow <A>(this TryOption <A> self) { try { return(self().Value.Value); } catch (Exception e) { TryConfig.ErrorLogger(e); throw; } }
public static OptionalResult <T> Try <T>(this TryOption <T> self) { try { if (self == null) { throw new ArgumentNullException("this is null"); } return(self()); } catch (Exception e) { TryConfig.ErrorLogger(e); return(new OptionalResult <T>(e)); } }
public static TryOptionResult <T> Try <T>(this TryOption <T> self) { try { if (self == null) { return(None); } return(self()); } catch (Exception e) { TryConfig.ErrorLogger(e); return(new TryOptionResult <T>(e)); } }
public static T IfFailThrow <T>(this Try <T> self) { try { var res = self(); if (res.IsFaulted) { throw res.Exception; } return(res.Value); } catch (Exception e) { TryConfig.ErrorLogger(e); throw; } }
public static async Task <A> IfFailThrow <A>(this TryAsync <A> self) { try { var res = await self.Try(); if (res.IsFaulted) { throw new InnerException(res.Exception); } return(res.Value); } catch (Exception e) { TryConfig.ErrorLogger(e); throw; } }
public static Try <B> Bind <A, B>(this Try <A> ma, Func <A, Try <B> > f) => Memo(() => { try { var ra = ma(); if (ra.IsSuccess) { return(f(ra.Value)()); } else { return(new Result <B>(ra.Exception)); } } catch (Exception e) { TryConfig.ErrorLogger(e); return(new Result <B>(e)); } });
public static Try <U> Select <T, U>(this Try <T> self, Func <T, U> select) { return(new Try <U>(() => { TryResult <T> resT; try { resT = self(); if (resT.IsFaulted) { return new TryResult <U>(resT.Exception); } } catch (Exception e) { TryConfig.ErrorLogger(e); return new TryResult <U>(e); } U resU; try { resU = select(resT.Value); if (resT.Value is ILinqDisposable) { (resT.Value as ILinqDisposable).Dispose(); } } catch (Exception e) { if (resT.Value is ILinqDisposable) { (resT.Value as ILinqDisposable).Dispose(); } TryConfig.ErrorLogger(e); return new TryResult <U>(e); } return new TryResult <U>(resU); })); }
public static Option <T> IfFailThrow <T>(this TryOption <T> self) { try { if (self == null) { return(None); } var res = self(); if (res.IsFaulted) { throw res.Exception; } return(res.Value); } catch (Exception e) { TryConfig.ErrorLogger(e); throw; } }
public static TryOption <V> SelectMany <T, U, V>( this TryOption <T> self, Func <T, TryOption <U> > bind, Func <T, U, V> project ) { return(new TryOption <V>( () => { var resT = self.Try(); if (resT.IsFaulted) { return new TryOptionResult <V>(resT.Exception); } if (resT.Value.IsNone) { return new TryOptionResult <V>(None); } var resU = bind(resT.Value.Value).Try(); if (resU.IsFaulted) { if (resT.Value is ILinqDisposable) { (resT.Value as ILinqDisposable).Dispose(); } return new TryOptionResult <V>(resU.Exception); } if (resU.Value.IsNone) { if (resT.Value is ILinqDisposable) { (resT.Value as ILinqDisposable).Dispose(); } return new TryOptionResult <V>(None); } try { var res = new TryOptionResult <V>(project(resT.Value.Value, resU.Value.Value)); if (resU.Value is ILinqDisposable) { (resU.Value as ILinqDisposable).Dispose(); } if (resT.Value is ILinqDisposable) { (resT.Value as ILinqDisposable).Dispose(); } return res; } catch (Exception e) { if (resU.Value is ILinqDisposable) { (resU.Value as ILinqDisposable).Dispose(); } if (resT.Value is ILinqDisposable) { (resT.Value as ILinqDisposable).Dispose(); } TryConfig.ErrorLogger(e); return new TryOptionResult <V>(e); } } )); }