diff --git a/src/LightResults.Extensions.ExceptionHandling/ExceptionHandler.Func.cs b/src/LightResults.Extensions.ExceptionHandling/ExceptionHandler.Func.cs index b60300a..68ef4cc 100644 --- a/src/LightResults.Extensions.ExceptionHandling/ExceptionHandler.Func.cs +++ b/src/LightResults.Extensions.ExceptionHandling/ExceptionHandler.Func.cs @@ -602,4 +602,623 @@ public static Result Try(ex); } } + + /// Attempts to execute an func and returns a indicating success or failure. + /// If an exception occurs, it is captured and converted to an error. + /// + /// The func to attempt. + /// A indicating success or failure. + public static async Task TryAsync(Func func) + { + try + { + await func().ConfigureAwait(false); + return Result.Ok(); + } + catch (Exception ex) + { + return HandleException(ex); + } + } + + /// Attempts to execute an func and returns a indicating success or failure. + /// If an exception occurs, it is captured and converted to an error. + /// + /// The func to attempt. + /// The type of the result. + /// A indicating success or failure. + public static async Task> TryAsync(Func> func) + { + try + { + var result = await func().ConfigureAwait(false); + return Result.Ok(result); + } + catch (Exception ex) + { + return HandleException(ex); + } + } + + /// Attempts to execute an func with a single argument and returns a indicating success or failure. + /// If an exception occurs, it is captured and converted to an error. + /// + /// The type of the first argument. + /// The type of the result. + /// The func to attempt. + /// The first argument. + /// A indicating success or failure. + public static async Task> TryAsync(Func> func, T1 arg1) + { + try + { + var result = await func(arg1).ConfigureAwait(false); + return Result.Ok(result); + } + catch (Exception ex) + { + return HandleException(ex); + } + } + + /// Attempts to execute an func with two arguments and returns a indicating success or failure. + /// If an exception occurs, it is captured and converted to an error. + /// + /// The type of the first argument. + /// The type of the second argument. + /// The type of the result. + /// The func to attempt. + /// The first argument. + /// The second argument. + /// A indicating success or failure. + public static async Task> TryAsync(Func> func, T1 arg1, T2 arg2) + { + try + { + var result = await func(arg1, arg2).ConfigureAwait(false); + return Result.Ok(result); + } + catch (Exception ex) + { + return HandleException(ex); + } + } + + /// Attempts to execute an func with three arguments and returns a indicating success or failure. + /// If an exception occurs, it is captured and converted to an error. + /// + /// The type of the first argument. + /// The type of the second argument. + /// The type of the third argument. + /// The type of the result. + /// The func to attempt. + /// The first argument. + /// The second argument. + /// The third argument. + /// A indicating success or failure. + public static async Task> TryAsync(Func> func, T1 arg1, T2 arg2, T3 arg3) + { + try + { + var result = await func(arg1, arg2, arg3).ConfigureAwait(false); + return Result.Ok(result); + } + catch (Exception ex) + { + return HandleException(ex); + } + } + + /// Attempts to execute an func with four arguments and returns a indicating success or failure. + /// If an exception occurs, it is captured and converted to an error. + /// + /// The type of the first argument. + /// The type of the second argument. + /// The type of the third argument. + /// The type of the fourth argument. + /// The type of the result. + /// The func to attempt. + /// The first argument. + /// The second argument. + /// The third argument. + /// The fourth argument. + /// A indicating success or failure. + public static async Task> TryAsync(Func> func, T1 arg1, T2 arg2, T3 arg3, T4 arg4) + { + try + { + var result = await func(arg1, arg2, arg3, arg4).ConfigureAwait(false); + return Result.Ok(result); + } + catch (Exception ex) + { + return HandleException(ex); + } + } + + /// Attempts to execute an func with five arguments and returns a indicating success or failure. + /// If an exception occurs, it is captured and converted to an error. + /// + /// The type of the first argument. + /// The type of the second argument. + /// The type of the third argument. + /// The type of the fourth argument. + /// The type of the fifth argument. + /// The type of the result. + /// The func to attempt. + /// The first argument. + /// The second argument. + /// The third argument. + /// The fourth argument. + /// The fifth argument. + /// A indicating success or failure. + public static async Task> TryAsync(Func> func, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) + { + try + { + var result = await func(arg1, arg2, arg3, arg4, arg5).ConfigureAwait(false); + return Result.Ok(result); + } + catch (Exception ex) + { + return HandleException(ex); + } + } + + /// Attempts to execute an func with six arguments and returns a indicating success or failure. + /// If an exception occurs, it is captured and converted to an error. + /// + /// The type of the first argument. + /// The type of the second argument. + /// The type of the third argument. + /// The type of the fourth argument. + /// The type of the fifth argument. + /// The type of the sixth argument. + /// The type of the result. + /// The func to attempt. + /// The first argument. + /// The second argument. + /// The third argument. + /// The fourth argument. + /// The fifth argument. + /// The sixth argument. + /// A indicating success or failure. + public static async Task> TryAsync(Func> func, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6) + { + try + { + var result = await func(arg1, arg2, arg3, arg4, arg5, arg6).ConfigureAwait(false); + return Result.Ok(result); + } + catch (Exception ex) + { + return HandleException(ex); + } + } + + /// Attempts to execute an func with seven arguments and returns a indicating success or failure. + /// If an exception occurs, it is captured and converted to an error. + /// + /// The type of the first argument. + /// The type of the second argument. + /// The type of the third argument. + /// The type of the fourth argument. + /// The type of the fifth argument. + /// The type of the sixth argument. + /// The type of the seventh argument. + /// The type of the result. + /// The func to attempt. + /// The first argument. + /// The second argument. + /// The third argument. + /// The fourth argument. + /// The fifth argument. + /// The sixth argument. + /// The seventh argument. + /// A indicating success or failure. + public static async Task> TryAsync(Func> func, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7) + { + try + { + var result = await func(arg1, arg2, arg3, arg4, arg5, arg6, arg7).ConfigureAwait(false); + return Result.Ok(result); + } + catch (Exception ex) + { + return HandleException(ex); + } + } + + /// Attempts to execute an func with eight arguments and returns a indicating success or failure. + /// If an exception occurs, it is captured and converted to an error. + /// + /// The type of the first argument. + /// The type of the second argument. + /// The type of the third argument. + /// The type of the fourth argument. + /// The type of the fifth argument. + /// The type of the sixth argument. + /// The type of the seventh argument. + /// The type of the eighth argument. + /// The type of the result. + /// The func to attempt. + /// The first argument. + /// The second argument. + /// The third argument. + /// The fourth argument. + /// The fifth argument. + /// The sixth argument. + /// The seventh argument. + /// The eighth argument. + /// A indicating success or failure. + public static async Task> TryAsync(Func> func, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8) + { + try + { + var result = await func(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8).ConfigureAwait(false); + return Result.Ok(result); + } + catch (Exception ex) + { + return HandleException(ex); + } + } + + /// Attempts to execute an func with nine arguments and returns a indicating success or failure. + /// If an exception occurs, it is captured and converted to an error. + /// + /// The type of the first argument. + /// The type of the second argument. + /// The type of the third argument. + /// The type of the fourth argument. + /// The type of the fifth argument. + /// The type of the sixth argument. + /// The type of the seventh argument. + /// The type of the eighth argument. + /// The type of the ninth argument. + /// The type of the result. + /// The func to attempt. + /// The first argument. + /// The second argument. + /// The third argument. + /// The fourth argument. + /// The fifth argument. + /// The sixth argument. + /// The seventh argument. + /// The eighth argument. + /// The ninth argument. + /// A indicating success or failure. + public static async Task> TryAsync(Func> func, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9) + { + try + { + var result = await func(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9).ConfigureAwait(false); + return Result.Ok(result); + } + catch (Exception ex) + { + return HandleException(ex); + } + } + + /// Attempts to execute an func with ten arguments and returns a indicating success or failure. + /// If an exception occurs, it is captured and converted to an error. + /// + /// The type of the first argument. + /// The type of the second argument. + /// The type of the third argument. + /// The type of the fourth argument. + /// The type of the fifth argument. + /// The type of the sixth argument. + /// The type of the seventh argument. + /// The type of the eighth argument. + /// The type of the ninth argument. + /// The type of the tenth argument. + /// The type of the result. + /// The func to attempt. + /// The first argument. + /// The second argument. + /// The third argument. + /// The fourth argument. + /// The fifth argument. + /// The sixth argument. + /// The seventh argument. + /// The eighth argument. + /// The ninth argument. + /// The tenth argument. + /// A indicating success or failure. + public static async Task> TryAsync(Func> func, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10) + { + try + { + var result = await func(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10).ConfigureAwait(false); + return Result.Ok(result); + } + catch (Exception ex) + { + return HandleException(ex); + } + } + + /// Attempts to execute an func with eleven arguments and returns a indicating success or failure. + /// If an exception occurs, it is captured and converted to an error. + /// + /// The type of the first argument. + /// The type of the second argument. + /// The type of the third argument. + /// The type of the fourth argument. + /// The type of the fifth argument. + /// The type of the sixth argument. + /// The type of the seventh argument. + /// The type of the eighth argument. + /// The type of the ninth argument. + /// The type of the tenth argument. + /// The type of the eleventh argument. + /// The type of the result. + /// The func to attempt. + /// The first argument. + /// The second argument. + /// The third argument. + /// The fourth argument. + /// The fifth argument. + /// The sixth argument. + /// The seventh argument. + /// The eighth argument. + /// The ninth argument. + /// The tenth argument. + /// The eleventh argument. + /// A indicating success or failure. + public static async Task> TryAsync(Func> func, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, + T10 arg10, T11 arg11) + { + try + { + var result = await func(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11).ConfigureAwait(false); + return Result.Ok(result); + } + catch (Exception ex) + { + return HandleException(ex); + } + } + + /// Attempts to execute an func with twelve arguments and returns a indicating success or failure. + /// If an exception occurs, it is captured and converted to an error. + /// + /// The type of the first argument. + /// The type of the second argument. + /// The type of the third argument. + /// The type of the fourth argument. + /// The type of the fifth argument. + /// The type of the sixth argument. + /// The type of the seventh argument. + /// The type of the eighth argument. + /// The type of the ninth argument. + /// The type of the tenth argument. + /// The type of the eleventh argument. + /// The type of the twelfth argument. + /// The type of the result. + /// The func to attempt. + /// The first argument. + /// The second argument. + /// The third argument. + /// The fourth argument. + /// The fifth argument. + /// The sixth argument. + /// The seventh argument. + /// The eighth argument. + /// The ninth argument. + /// The tenth argument. + /// The eleventh argument. + /// The twelfth argument. + /// A indicating success or failure. + public static async Task> TryAsync(Func> func, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, + T9 arg9, T10 arg10, T11 arg11, T12 arg12) + { + try + { + var result = await func(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12).ConfigureAwait(false); + return Result.Ok(result); + } + catch (Exception ex) + { + return HandleException(ex); + } + } + + /// Attempts to execute an func with thirteen arguments and returns a indicating success or failure. + /// If an exception occurs, it is captured and converted to an error. + /// + /// The type of the first argument. + /// The type of the second argument. + /// The type of the third argument. + /// The type of the fourth argument. + /// The type of the fifth argument. + /// The type of the sixth argument. + /// The type of the seventh argument. + /// The type of the eighth argument. + /// The type of the ninth argument. + /// The type of the tenth argument. + /// The type of the eleventh argument. + /// The type of the twelfth argument. + /// The type of the thirteenth argument. + /// The type of the result. + /// The func to attempt. + /// The first argument. + /// The second argument. + /// The third argument. + /// The fourth argument. + /// The fifth argument. + /// The sixth argument. + /// The seventh argument. + /// The eighth argument. + /// The ninth argument. + /// The tenth argument. + /// The eleventh argument. + /// The twelfth argument. + /// The thirteenth argument. + /// A indicating success or failure. + public static async Task> TryAsync(Func> func, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, + T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13) + { + try + { + var result = await func(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13).ConfigureAwait(false); + return Result.Ok(result); + } + catch (Exception ex) + { + return HandleException(ex); + } + } + + /// Attempts to execute an func with fourteen arguments and returns a indicating success or failure. + /// If an exception occurs, it is captured and converted to an error. + /// + /// The type of the first argument. + /// The type of the second argument. + /// The type of the third argument. + /// The type of the fourth argument. + /// The type of the fifth argument. + /// The type of the sixth argument. + /// The type of the seventh argument. + /// The type of the eighth argument. + /// The type of the ninth argument. + /// The type of the tenth argument. + /// The type of the eleventh argument. + /// The type of the twelfth argument. + /// The type of the thirteenth argument. + /// The type of the fourteenth argument. + /// The type of the result. + /// The func to attempt. + /// The first argument. + /// The second argument. + /// The third argument. + /// The fourth argument. + /// The fifth argument. + /// The sixth argument. + /// The seventh argument. + /// The eighth argument. + /// The ninth argument. + /// The tenth argument. + /// The eleventh argument. + /// The twelfth argument. + /// The thirteenth argument. + /// The fourteenth argument. + /// A indicating success or failure. + public static async Task> TryAsync(Func> func, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, + T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14) + { + try + { + var result = await func(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14).ConfigureAwait(false); + return Result.Ok(result); + } + catch (Exception ex) + { + return HandleException(ex); + } + } + + /// Attempts to execute an func with 15 arguments and returns a indicating success or failure. + /// If an exception occurs, it is captured and converted to an error. + /// + /// The type of the first argument. + /// The type of the second argument. + /// The type of the third argument. + /// The type of the fourth argument. + /// The type of the fifth argument. + /// The type of the sixth argument. + /// The type of the seventh argument. + /// The type of the eighth argument. + /// The type of the ninth argument. + /// The type of the tenth argument. + /// The type of the eleventh argument. + /// The type of the twelfth argument. + /// The type of the thirteenth argument. + /// The type of the fourteenth argument. + /// The type of the fifteenth argument. + /// The type of the result. + /// The func to attempt. + /// The first argument. + /// The second argument. + /// The third argument. + /// The fourth argument. + /// The fifth argument. + /// The sixth argument. + /// The seventh argument. + /// The eighth argument. + /// The ninth argument. + /// The tenth argument. + /// The eleventh argument. + /// The twelfth argument. + /// The thirteenth argument. + /// The fourteenth argument. + /// The fifteenth argument. + /// A indicating success or failure. + public static async Task> TryAsync(Func> func, T1 arg1, T2 arg2, T3 arg3, T4 arg4, + T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15) + { + try + { + var result = await func(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15).ConfigureAwait(false); + return Result.Ok(result); + } + catch (Exception ex) + { + return HandleException(ex); + } + } + + /// Attempts to execute an func with sixteen arguments and returns a indicating success or failure. + /// If an exception occurs, it is captured and converted to an error. + /// + /// The type of the first argument. + /// The type of the second argument. + /// The type of the third argument. + /// The type of the fourth argument. + /// The type of the fifth argument. + /// The type of the sixth argument. + /// The type of the seventh argument. + /// The type of the eighth argument. + /// The type of the ninth argument. + /// The type of the tenth argument. + /// The type of the eleventh argument. + /// The type of the twelfth argument. + /// The type of the thirteenth argument. + /// The type of the fourteenth argument. + /// The type of the fifteenth argument. + /// The type of the sixteenth argument. + /// The type of the result. + /// The func to attempt. + /// The first argument. + /// The second argument. + /// The third argument. + /// The fourth argument. + /// The fifth argument. + /// The sixth argument. + /// The seventh argument. + /// The eighth argument. + /// The ninth argument. + /// The tenth argument. + /// The eleventh argument. + /// The twelfth argument. + /// The thirteenth argument. + /// The fourteenth argument. + /// The fifteenth argument. + /// The sixteenth argument. + /// A indicating success or failure. + public static async Task> TryAsync(Func> func, T1 arg1, T2 arg2, T3 arg3, + T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16) + { + try + { + var result = await func(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16).ConfigureAwait(false); + return Result.Ok(result); + } + catch (Exception ex) + { + return HandleException(ex); + } + } }