Skip to content

Instantly share code, notes, and snippets.

@solvingj
Last active May 19, 2017 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save solvingj/2e8005c7375984b3ac2a8b2f03430029 to your computer and use it in GitHub Desktop.
Save solvingj/2e8005c7375984b3ac2a8b2f03430029 to your computer and use it in GitHub Desktop.
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