Last active
May 19, 2017 17:06
-
-
Save solvingj/2e8005c7375984b3ac2a8b2f03430029 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Tryable | |
{ | |
public static Exceptional<T> Try<T>(Func<T> func) | |
{ | |
try | |
{ | |
return Exceptional.Ok(func()); | |
} | |
catch (Exception e) | |
{ | |
return Exceptional.Fail<T>(e); | |
} | |
} | |
public static async Task<Exceptional<T>> Try<T>(Task<Func<T>> funcTask) | |
{ | |
Func<T> func = await funcTask; | |
T t = func(); | |
try | |
{ | |
return Exceptional.Ok(t); | |
} | |
catch (Exception e) | |
{ | |
return Exceptional.Fail<T>(e); | |
} | |
} | |
public static async Task<Exceptional<T>> Try<T>(Func<Task<T>> func) | |
{ | |
try | |
{ | |
T t = await func(); | |
return Exceptional.Ok(t); | |
} | |
catch (Exception e) | |
{ | |
return Exceptional.Fail<T>(e); | |
} | |
} | |
public static async Task<Exceptional<ValueTuple>> Try(Func<Task> func) | |
{ | |
try | |
{ | |
await func(); | |
return Exceptional.Ok(new ValueTuple()); | |
} | |
catch (Exception e) | |
{ | |
return Exceptional.Fail<ValueTuple>(e); | |
} | |
} | |
public static Exceptional<ValueTuple> Try(Action action) | |
{ | |
return Try(action.ToFunc()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment