From 2d68a0d048e0a391238412a4683d3bfe0c6eb94b Mon Sep 17 00:00:00 2001 From: Jean-Sebastien Carle <29762210+jscarle@users.noreply.github.com> Date: Thu, 29 Feb 2024 19:52:34 -0500 Subject: [PATCH] Reduced code duplication. --- .../ExceptionHandlingExtensions.Action.cs | 172 ++---------------- .../ExceptionHandlingExtensions.Func.cs | 170 ++--------------- .../ExceptionHandlingExtensions.cs | 29 +-- 3 files changed, 36 insertions(+), 335 deletions(-) diff --git a/src/LightResults.Extensions.ExceptionHandling/ExceptionHandlingExtensions.Action.cs b/src/LightResults.Extensions.ExceptionHandling/ExceptionHandlingExtensions.Action.cs index 51cb944..1d600db 100644 --- a/src/LightResults.Extensions.ExceptionHandling/ExceptionHandlingExtensions.Action.cs +++ b/src/LightResults.Extensions.ExceptionHandling/ExceptionHandlingExtensions.Action.cs @@ -9,15 +9,7 @@ public static partial class ExceptionHandlingExtensions /// A indicating success or failure. public static Result Try(this Action action) { - try - { - action(); - return Result.Ok(); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(action); } /// Attempts to execute an action with a single argument and returns a indicating success or failure. @@ -29,15 +21,7 @@ public static Result Try(this Action action) /// A indicating success or failure. public static Result Try(this Action action, T1 arg1) { - try - { - action(arg1); - return Result.Ok(); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(action, arg1); } /// Attempts to execute an action with two arguments and returns a indicating success or failure. @@ -51,15 +35,7 @@ public static Result Try(this Action action, T1 arg1) /// A indicating success or failure. public static Result Try(this Action action, T1 arg1, T2 arg2) { - try - { - action(arg1, arg2); - return Result.Ok(); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(action, arg1, arg2); } /// Attempts to execute an action with three arguments and returns a indicating success or failure. @@ -75,15 +51,7 @@ public static Result Try(this Action action, T1 arg1, T2 arg2) /// A indicating success or failure. public static Result Try(this Action action, T1 arg1, T2 arg2, T3 arg3) { - try - { - action(arg1, arg2, arg3); - return Result.Ok(); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(action, arg1, arg2, arg3); } /// Attempts to execute an action with four arguments and returns a indicating success or failure. @@ -101,15 +69,7 @@ public static Result Try(this Action action, T1 arg1, T2 /// A indicating success or failure. public static Result Try(this Action action, T1 arg1, T2 arg2, T3 arg3, T4 arg4) { - try - { - action(arg1, arg2, arg3, arg4); - return Result.Ok(); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(action, arg1, arg2, arg3, arg4); } /// Attempts to execute an action with five arguments and returns a indicating success or failure. @@ -129,15 +89,7 @@ public static Result Try(this Action action, T1 /// A indicating success or failure. public static Result Try(this Action action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) { - try - { - action(arg1, arg2, arg3, arg4, arg5); - return Result.Ok(); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(action, arg1, arg2, arg3, arg4, arg5); } /// Attempts to execute an action with six arguments and returns a indicating success or failure. @@ -159,15 +111,7 @@ public static Result Try(this Action act /// A indicating success or failure. public static Result Try(this Action action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6) { - try - { - action(arg1, arg2, arg3, arg4, arg5, arg6); - return Result.Ok(); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(action, arg1, arg2, arg3, arg4, arg5, arg6); } /// Attempts to execute an action with seven arguments and returns a indicating success or failure. @@ -191,15 +135,7 @@ public static Result Try(this ActionA indicating success or failure. public static Result Try(this Action action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7) { - try - { - action(arg1, arg2, arg3, arg4, arg5, arg6, arg7); - return Result.Ok(); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(action, arg1, arg2, arg3, arg4, arg5, arg6, arg7); } /// Attempts to execute an action with eight arguments and returns a indicating success or failure. @@ -225,15 +161,7 @@ public static Result Try(this ActionA indicating success or failure. public static Result Try(this Action action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8) { - try - { - action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); - return Result.Ok(); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(action, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); } /// Attempts to execute an action with nine arguments and returns a indicating success or failure. @@ -261,15 +189,7 @@ public static Result Try(this ActionA indicating success or failure. public static Result Try(this Action action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9) { - try - { - action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9); - return Result.Ok(); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(action, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9); } /// Attempts to execute an action with ten arguments and returns a indicating success or failure. @@ -299,15 +219,7 @@ public static Result Try(this ActionA indicating success or failure. public static Result Try(this Action action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10) { - try - { - action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10); - return Result.Ok(); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(action, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10); } /// Attempts to execute an action with eleven arguments and returns a indicating success or failure. @@ -340,15 +252,7 @@ public static Result Try(this Action(this Action action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11) { - try - { - action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11); - return Result.Ok(); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(action, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11); } /// Attempts to execute an action with twelve arguments and returns a indicating success or failure. @@ -383,15 +287,7 @@ public static Result Try(this Acti public static Result Try(this Action action, 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 - { - action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12); - return Result.Ok(); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(action, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12); } /// Attempts to execute an action with thirteen arguments and returns a indicating success or failure. @@ -428,15 +324,7 @@ public static Result Try(this public static Result Try(this Action action, 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 - { - action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13); - return Result.Ok(); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(action, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13); } /// Attempts to execute an action with fourteen arguments and returns a indicating success or failure. @@ -475,15 +363,7 @@ public static Result Try public static Result Try(this Action action, 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 - { - action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14); - return Result.Ok(); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(action, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14); } /// Attempts to execute an action with 15 arguments and returns a indicating success or failure. @@ -524,15 +404,7 @@ public static Result Try(this Action action, 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 - { - action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15); - return Result.Ok(); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(action, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15); } /// Attempts to execute an action with sixteen arguments and returns a indicating success or failure. @@ -575,14 +447,6 @@ public static Result Try(this Action action, 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 - { - action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16); - return Result.Ok(); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(action, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16); } -} +} \ No newline at end of file diff --git a/src/LightResults.Extensions.ExceptionHandling/ExceptionHandlingExtensions.Func.cs b/src/LightResults.Extensions.ExceptionHandling/ExceptionHandlingExtensions.Func.cs index 992c092..5b335df 100644 --- a/src/LightResults.Extensions.ExceptionHandling/ExceptionHandlingExtensions.Func.cs +++ b/src/LightResults.Extensions.ExceptionHandling/ExceptionHandlingExtensions.Func.cs @@ -10,15 +10,7 @@ public static partial class ExceptionHandlingExtensions /// A indicating success or failure. public static Result Try(this Func func) { - try - { - var result = func(); - return Result.Ok(result); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(func); } /// Attempts to execute an func with a single argument and returns a indicating success or failure. @@ -31,15 +23,7 @@ public static Result Try(this Func func) /// A indicating success or failure. public static Result Try(this Func func, T1 arg1) { - try - { - var result = func(arg1); - return Result.Ok(result); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(func, arg1); } /// Attempts to execute an func with two arguments and returns a indicating success or failure. @@ -54,15 +38,7 @@ public static Result Try(this Func func, T1 a /// A indicating success or failure. public static Result Try(this Func func, T1 arg1, T2 arg2) { - try - { - var result = func(arg1, arg2); - return Result.Ok(result); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(func, arg1, arg2); } /// Attempts to execute an func with three arguments and returns a indicating success or failure. @@ -79,15 +55,7 @@ public static Result Try(this Func fu /// A indicating success or failure. public static Result Try(this Func func, T1 arg1, T2 arg2, T3 arg3) { - try - { - var result = func(arg1, arg2, arg3); - return Result.Ok(result); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(func, arg1, arg2, arg3); } /// Attempts to execute an func with four arguments and returns a indicating success or failure. @@ -106,15 +74,7 @@ public static Result Try(this FuncA indicating success or failure. public static Result Try(this Func func, T1 arg1, T2 arg2, T3 arg3, T4 arg4) { - try - { - var result = func(arg1, arg2, arg3, arg4); - return Result.Ok(result); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(func, arg1, arg2, arg3, arg4); } /// Attempts to execute an func with five arguments and returns a indicating success or failure. @@ -135,15 +95,7 @@ public static Result Try(this FuncA indicating success or failure. public static Result Try(this Func func, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) { - try - { - var result = func(arg1, arg2, arg3, arg4, arg5); - return Result.Ok(result); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(func, arg1, arg2, arg3, arg4, arg5); } /// Attempts to execute an func with six arguments and returns a indicating success or failure. @@ -166,15 +118,7 @@ public static Result Try(this FuncA indicating success or failure. public static Result Try(this Func func, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6) { - try - { - var result = func(arg1, arg2, arg3, arg4, arg5, arg6); - return Result.Ok(result); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(func, arg1, arg2, arg3, arg4, arg5, arg6); } /// Attempts to execute an func with seven arguments and returns a indicating success or failure. @@ -199,15 +143,7 @@ public static Result Try(this FuncA indicating success or failure. public static Result Try(this Func func, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7) { - try - { - var result = func(arg1, arg2, arg3, arg4, arg5, arg6, arg7); - return Result.Ok(result); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7); } /// Attempts to execute an func with eight arguments and returns a indicating success or failure. @@ -234,15 +170,7 @@ public static Result Try(this Func /// A indicating success or failure. public static Result Try(this Func func, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8) { - try - { - var result = func(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); - return Result.Ok(result); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); } /// Attempts to execute an func with nine arguments and returns a indicating success or failure. @@ -271,15 +199,7 @@ public static Result Try(this /// A indicating success or failure. public static Result Try(this Func func, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9) { - try - { - var result = func(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9); - return Result.Ok(result); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9); } /// Attempts to execute an func with ten arguments and returns a indicating success or failure. @@ -311,15 +231,7 @@ public static Result Try(t public static Result Try(this 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 = func(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10); - return Result.Ok(result); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10); } /// Attempts to execute an func with eleven arguments and returns a indicating success or failure. @@ -353,15 +265,7 @@ public static Result Try Try(this 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 = func(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11); - return Result.Ok(result); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11); } /// Attempts to execute an func with twelve arguments and returns a indicating success or failure. @@ -397,15 +301,7 @@ public static Result Try Try(this 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 = func(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12); - return Result.Ok(result); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12); } /// Attempts to execute an func with thirteen arguments and returns a indicating success or failure. @@ -443,15 +339,7 @@ public static Result Try Try(this 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 = func(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13); - return Result.Ok(result); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13); } /// Attempts to execute an func with fourteen arguments and returns a indicating success or failure. @@ -491,15 +379,7 @@ public static Result Try Try(this 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 = func(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14); - return Result.Ok(result); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14); } /// Attempts to execute an func with 15 arguments and returns a indicating success or failure. @@ -541,15 +421,7 @@ public static Result Try Try(this 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 = func(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15); - return Result.Ok(result); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15); } /// Attempts to execute an func with sixteen arguments and returns a indicating success or failure. @@ -593,14 +465,6 @@ public static Result Try Try(this 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 = func(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16); - return Result.Ok(result); - } - catch (Exception ex) - { - return HandleException(ex); - } + return ExceptionHandler.Try(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16); } } diff --git a/src/LightResults.Extensions.ExceptionHandling/ExceptionHandlingExtensions.cs b/src/LightResults.Extensions.ExceptionHandling/ExceptionHandlingExtensions.cs index 340804b..986725c 100644 --- a/src/LightResults.Extensions.ExceptionHandling/ExceptionHandlingExtensions.cs +++ b/src/LightResults.Extensions.ExceptionHandling/ExceptionHandlingExtensions.cs @@ -1,31 +1,4 @@ namespace LightResults.Extensions.ExceptionHandling; /// Provides extension methods for handling exceptions using the type. -public static partial class ExceptionHandlingExtensions -{ - private const string ExceptionKey = "Exception"; - - private static Result HandleException(Exception ex) - { - var error = GetExceptionError(ex); - return Result.Fail(error); - } - - private static Result HandleException(Exception ex) - { - var error = GetExceptionError(ex); - return Result.Fail(error); - } - - private static Error GetExceptionError(Exception ex) - { - var message = GetExceptionMessage(ex); - var error = new Error(message, (ExceptionKey, ex)); - return error; - } - - private static string GetExceptionMessage(Exception ex) - { - return $"{ex.GetType().Name}: {ex.Message}"; - } -} +public static partial class ExceptionHandlingExtensions;