From 95f311b43f470a5a0edbee0711781a61e7a3194c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20I=C5=BCycki?= Date: Sat, 24 Feb 2018 14:57:04 +0100 Subject: [PATCH] Add in-code doc for TryCatch, create seperate file for those extensions --- .gitignore | 2 + Monacs.Core/Result.Extensions.cs | 19 +---- Monacs.Core/Result.TryCatch.Extensions.cs | 74 +++++++++++++++++++ Monacs.Core/Tuples/Result.Tuple.Extensions.cs | 36 --------- 4 files changed, 77 insertions(+), 54 deletions(-) create mode 100644 Monacs.Core/Result.TryCatch.Extensions.cs diff --git a/.gitignore b/.gitignore index f1e3d20..67cad60 100644 --- a/.gitignore +++ b/.gitignore @@ -250,3 +250,5 @@ paket-files/ # JetBrains Rider .idea/ *.sln.iml + +*.orig diff --git a/Monacs.Core/Result.Extensions.cs b/Monacs.Core/Result.Extensions.cs index 5ca1f87..f9529de 100644 --- a/Monacs.Core/Result.Extensions.cs +++ b/Monacs.Core/Result.Extensions.cs @@ -24,7 +24,7 @@ public static partial class Result /// Desired type parameter for type. /// Details of the error. public static Result Error(ErrorDetails error) => new Result(error); - + /* Converters */ /// @@ -400,8 +400,6 @@ public static Result> Sequence(this IEnumerable> ite ? Error>(items.First(i => i.IsError).Error) : Ok(items.Select(i => i.Value)); - /* TryCatch */ - /// /// Tries to execute . /// If the execution completes without exception, returns Ok with the function result. @@ -410,19 +408,6 @@ public static Result> Sequence(this IEnumerable> ite /// Type of the value in the result. /// Function to execute. /// Function that generates error details in case of exception. - public static Result TryCatch(Func func, Func errorHandler) - { - try - { - var result = func(); - return Ok(result); - } - catch (Exception ex) - { - return Error(errorHandler(ex)); - } - } - /// /// Tries to execute with the value from the as an input. /// If the execution completes without exception, returns Ok with the function result. @@ -434,7 +419,5 @@ public static Result TryCatch(Func func, Func /// Result to take the value from. /// Function to execute. /// Function that generates error details in case of exception. - public static Result TryCatch(this Result result, Func func, Func errorHandler) => - result.Bind(value => Result.TryCatch(() => func(value), e => errorHandler(value, e))); } } diff --git a/Monacs.Core/Result.TryCatch.Extensions.cs b/Monacs.Core/Result.TryCatch.Extensions.cs new file mode 100644 index 0000000..9511899 --- /dev/null +++ b/Monacs.Core/Result.TryCatch.Extensions.cs @@ -0,0 +1,74 @@ +using System; + +namespace Monacs.Core +{ + public static partial class Result + { + /// + /// Invokes function in try/catch block and returns its result. + /// If any is raised during execution, error handler is invoked and error details are returned. + /// + /// Type of value returned by invoked function. + /// The function to be invoked in 'try' block. + /// Handler invoked in 'catch' block on any raised exception. + /// of invoked function in try block or if any exception occurs. + public static Result TryCatch(Func func, Func errorHandler) + { + try + { + return Ok(func()); + } + catch (Exception ex) + { + return Error(errorHandler(ex)); + } + } + + /// + /// Invokes function in try/catch block and returns its result. + /// If any is raised during execution, error handler is invoked and error details are returned. + /// + /// Type of an argument accepted by invoked function. + /// Type of value returned by invoked function. + /// Result of previous operation. + /// The function to be invoked in 'try' block. + /// Handler invoked in 'catch' block on any raised exception. + /// of invoked function in try block or if any exception occurs. + public static Result TryCatch(this Result result, Func func, Func errorHandler) => + result.Bind(value => Result.TryCatch(() => func(value), e => errorHandler(value, e))); + + /// + /// Invokes function in try/catch block and returns its result. + /// + /// Type of value returned by invoked function. + /// Type of first value in function tuple argument. + /// Type of second value in function tuple argument. + /// Result of previous operation. + /// The function to be invoked in 'try' block. + /// Handler invoked in 'catch' block on any raised exception. + /// of invoked function in try block or if any exception occurs. + public static Result TryCatch2( + this Result<(TFst fst, TSnd snd)> result, + Func tryFunc, + Func errorHandler) => + result.Bind(value => TryCatch(func: () => tryFunc(value.fst, value.snd), + errorHandler: err => errorHandler(value.fst, value.snd, err))); + + /// + /// Invokes function in try/catch block and returns its result. + /// + /// Type of value returned by invoked function. + /// Type of first value in function tuple argument. + /// Type of second value in function tuple argument. + /// Result of previous operation. + /// The function to be invoked in 'try' block. + /// Handler invoked in 'catch' block on any raised exception. + /// of invoked function in try block or if any exception occurs. + public static Result TryCatch3( + this Result<(TFst fst, TSnd snd, TTrd trd)> result, + Func tryFunc, + Func errorHandler) => + result.Bind(value => TryCatch(func: () => tryFunc(value.fst, value.snd, value.trd), + errorHandler: err => errorHandler(value.fst, value.snd, value.trd, err))); + } +} diff --git a/Monacs.Core/Tuples/Result.Tuple.Extensions.cs b/Monacs.Core/Tuples/Result.Tuple.Extensions.cs index 5299d6a..dfcbdf3 100644 --- a/Monacs.Core/Tuples/Result.Tuple.Extensions.cs +++ b/Monacs.Core/Tuples/Result.Tuple.Extensions.cs @@ -59,41 +59,5 @@ public static TVal Match3(this Result<(TFst fst, TSnd sn return result; } - /* Try/Catch */ - - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - public static Result TryCatch2( - this Result<(TFst fst, TSnd snd)> result, - Func tryFunc, - Func errorHandler) => - result.Bind(value => TryCatch(func: () => tryFunc(value.fst, value.snd), - errorHandler: err => errorHandler(value.fst, value.snd, err))); - - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - public static Result TryCatch3( - this Result<(TFst fst, TSnd snd, TTrd trd)> result, - Func tryFunc, - Func errorHandler) => - result.Bind(value => TryCatch(func: () => tryFunc(value.fst, value.snd, value.trd), - errorHandler: err => errorHandler(value.fst, value.snd, value.trd, err))); } } \ No newline at end of file