using System.Reactive.Linq; namespace InnovEnergy.Lib.Utils; public static class ObservableExceptionHandling { public static IObservable> Try(this IObservable src, Func> map) { return src.SelectMany(t => ExceptionHandling.Try(() => map(t))); } public static IObservable> Try(this IObservable src, Func map) { return src.Select(t => ExceptionHandling.Try(() => map(t))); } public static IObservable> ThenTry(this IObservable> src, Func map) { return src.Select(t => ExceptionHandling.ThenTry(t, map)); } // public static IObservable Catch(this IObservable> src, Func onError) // { // return src.Select(t => ExceptionHandling.Catch(t, onError)); // } // TODO: onError should be a func returning T // public static IObservable Catch(this IObservable> src, Action onError) // { // return src // .OnErrorDo(onError) // .IgnoreErrors(); // } public static IObservable IgnoreErrors(this IObservable> src) { return src .Where(t => !(t.Wrapped is Exception)) .Select(t => (T) t.Wrapped); } public static IObservable> OnErrorDo(this IObservable> src, Action onError) { return src.Do(t=> { if (t.Wrapped is Exception e) onError(e); }); } public static IObservable> Match(this IObservable> src, Action onValue, Action onError) { return src.Do(t=> { if (t.Wrapped is Exception e) onError(e); else onValue((T) t.Wrapped); }); } // public static IObservable> Try(this IObservable observable, Func map) // { // return observable // .Select(e => CanFail.Try(() => map(e))); // } // // public static IObservable> Try(this IObservable> observable, Func map) // { // return observable // .Select(t => t.Select(map)); // } // // // public static IObservable> TryAsync(this IObservable observable, Func> map) // { // return observable // .SelectMany(e => CanFail.TryAsync(() => map(e))); // } // // // public static IObservable> TryAsync(this IObservable> observable, // Func> map) // { // return observable // .SelectMany(t => CanFail.SelectMany(t, map)); // } // // // public static IObservable> OnErrorDo(this IObservable> observable, Action onError) // { // return observable // .Select(t => // { // if (t is Failure f) // onError(f.Error); // // return t; // }); // } // public static IObservable TryCatch(this IObservable observable, // Func map, // Func exceptionHandler) // where E : Exception // { // R TryMap(T i) // { // try // { // return map(i); // } // catch (E e) // { // return exceptionHandler(e); // } // } // // return observable.Select(TryMap); // } // public static IObservable> Try(this IObservable observable, // Func map, // Func> exceptionHandler) // where E : Exception // { // CanFail TryMap(T i) // { // try // { // return CanFail.Success(map(i)); // } // catch (E e) // { // return exceptionHandler(e); // } // } // // return observable.Select(TryMap); // } // public static IObservable TryAsync(this IObservable observable, // Func> map, // Action onError) // { // async Task> TryMap(T i) // { // try // { // return Notification.CreateOnNext(await map(i)); // } // catch (Exception e) // { // onError(e); // return Notification.CreateOnError(e); // } // } // // return observable // .SelectMany(TryMap) // .Where(n => n.Kind != NotificationKind.OnError) // .Dematerialize(); // } // public static IObservable Try(this IObservable observable, Func map) // { // Notification TryMap(T i) // { // try // { // return Notification.CreateOnNext(map(i)); // } // catch (Exception e) // { // return Notification.CreateOnError(e); // } // } // // return observable // .Select(TryMap) // .Where(n => n.Kind != NotificationKind.OnError) // .Dematerialize(); // } // // public static IObservable Try(this IObservable observable, // Func map, // Action onError) // { // Notification TryMap(T i) // { // try // { // return Notification.CreateOnNext(map(i)); // } // catch (Exception e) // { // onError(e); // return Notification.CreateOnError(e); // } // } // // return observable // .Select(TryMap) // .Where(n => n.Kind != NotificationKind.OnError) // .Dematerialize(); // } // // public static IObservable Try(this IObservable observable, // Func map, // Func ignoreException) // { // Notification TryMap(T i) // { // try // { // return Notification.CreateOnNext(map(i)); // } // catch (Exception e) // { // if (ignoreException(e)) // return Notification.CreateOnError(e); // // throw; // // // throw ex resets the stack trace (so your errors would appear to originate from HandleException) // // throw doesn't - the original offender would be preserved. // } // } // // return observable // .Select(TryMap) // .Where(n => n.Kind != NotificationKind.OnError) // .Dematerialize(); // } }