using System.Reactive.Linq; namespace InnovEnergy.VenusLogger; internal static class ObservableExtensions { public static IObservable TrySelect(this IObservable source, Func map) where R : notnull { return source .Select(e => Try(map, e)) .OfType(); } public static IEnumerable TrySelect(this IEnumerable source, Func map) where R : notnull { return source .Select(e => Try(map, e)) .OfType(); } public static IObservable TrySelect(this IObservable source, Func map, R onError) where R : notnull { return source .Select(e => Try(map, e)) .Select(e => e is R r ? r : onError); } public static IEnumerable TrySelect(this IEnumerable source, Func map, R onError) where R : notnull { return source .Select(e => Try(map, e)) .Select(e=>e is R r ? r : onError); } private static Object Try(Func map, T e) where R : notnull { try { return map(e); } catch (Exception ex) { return ex; } } }