Skip to content

Commit

Permalink
Add in-code doc for TryCatch, create seperate file for those extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
pizycki authored and bartsokol committed Apr 2, 2018
1 parent 61fb238 commit 95f311b
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 54 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,5 @@ paket-files/
# JetBrains Rider
.idea/
*.sln.iml

*.orig
19 changes: 1 addition & 18 deletions Monacs.Core/Result.Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static partial class Result
/// <typeparam name="T">Desired type parameter for <see cref="Result{T}"/> type.</typeparam>
/// <param name="error">Details of the error.</param>
public static Result<T> Error<T>(ErrorDetails error) => new Result<T>(error);

/* Converters */

/// <summary>
Expand Down Expand Up @@ -400,8 +400,6 @@ public static Result<IEnumerable<T>> Sequence<T>(this IEnumerable<Result<T>> ite
? Error<IEnumerable<T>>(items.First(i => i.IsError).Error)
: Ok(items.Select(i => i.Value));

/* TryCatch */

/// <summary>
/// Tries to execute <paramref name="func"/>.
/// If the execution completes without exception, returns Ok with the function result.
Expand All @@ -410,19 +408,6 @@ public static Result<IEnumerable<T>> Sequence<T>(this IEnumerable<Result<T>> ite
/// <typeparam name="T">Type of the value in the result.</typeparam>
/// <param name="func">Function to execute.</param>
/// <param name="errorHandler">Function that generates error details in case of exception.</param>
public static Result<T> TryCatch<T>(Func<T> func, Func<Exception, ErrorDetails> errorHandler)
{
try
{
var result = func();
return Ok(result);
}
catch (Exception ex)
{
return Error<T>(errorHandler(ex));
}
}

/// <summary>
/// Tries to execute <paramref name="func"/> with the value from the <paramref name="result"/> as an input.
/// If the execution completes without exception, returns Ok with the function result.
Expand All @@ -434,7 +419,5 @@ public static Result<T> TryCatch<T>(Func<T> func, Func<Exception, ErrorDetails>
/// <param name="result">Result to take the value from.</param>
/// <param name="func">Function to execute.</param>
/// <param name="errorHandler">Function that generates error details in case of exception.</param>
public static Result<TOut> TryCatch<TIn, TOut>(this Result<TIn> result, Func<TIn, TOut> func, Func<TIn, Exception, ErrorDetails> errorHandler) =>
result.Bind(value => Result.TryCatch(() => func(value), e => errorHandler(value, e)));
}
}
74 changes: 74 additions & 0 deletions Monacs.Core/Result.TryCatch.Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;

namespace Monacs.Core
{
public static partial class Result
{
/// <summary>
/// Invokes function in try/catch block and returns its result.
/// If any <see cref="Exception"/> is raised during execution, error handler is invoked and error details are returned.
/// </summary>
/// <typeparam name="TValue">Type of value returned by invoked function.</typeparam>
/// <param name="func">The function to be invoked in 'try' block.</param>
/// <param name="errorHandler">Handler invoked in 'catch' block on any raised exception.</param>
/// <returns><see cref="Result{TValue}"/> of invoked function in try block or <see cref="ErrorDetails"/> if any exception occurs.</returns>
public static Result<TValue> TryCatch<TValue>(Func<TValue> func, Func<Exception, ErrorDetails> errorHandler)
{
try
{
return Ok(func());
}
catch (Exception ex)
{
return Error<TValue>(errorHandler(ex));
}
}

/// <summary>
/// Invokes function in try/catch block and returns its result.
/// If any <see cref="Exception"/> is raised during execution, error handler is invoked and error details are returned.
/// </summary>
/// <typeparam name="TArg">Type of an argument accepted by invoked function.</typeparam>
/// <typeparam name="TValue">Type of value returned by invoked function.</typeparam>
/// <param name="result">Result of previous operation.</param>
/// <param name="func">The function to be invoked in 'try' block.</param>
/// <param name="errorHandler">Handler invoked in 'catch' block on any raised exception.</param>
/// <returns><see cref="Result{TValue}"/> of invoked function in try block or <see cref="ErrorDetails"/> if any exception occurs.</returns>
public static Result<TValue> TryCatch<TArg, TValue>(this Result<TArg> result, Func<TArg, TValue> func, Func<TArg, Exception, ErrorDetails> errorHandler) =>
result.Bind(value => Result.TryCatch(() => func(value), e => errorHandler(value, e)));

/// <summary>
/// Invokes function in try/catch block and returns its result.
/// </summary>
/// <typeparam name="TValue">Type of value returned by invoked function.</typeparam>
/// <typeparam name="TFst">Type of first value in function tuple argument.</typeparam>
/// <typeparam name="TSnd">Type of second value in function tuple argument.</typeparam>
/// <param name="result">Result of previous operation.</param>
/// <param name="tryFunc">The function to be invoked in 'try' block.</param>
/// <param name="errorHandler">Handler invoked in 'catch' block on any raised exception.</param>
/// <returns><see cref="Result{TValue}"/> of invoked function in try block or <see cref="ErrorDetails"/> if any exception occurs.</returns>
public static Result<TValue> TryCatch2<TValue, TFst, TSnd>(
this Result<(TFst fst, TSnd snd)> result,
Func<TFst, TSnd, TValue> tryFunc,
Func<TFst, TSnd, Exception, ErrorDetails> errorHandler) =>
result.Bind(value => TryCatch(func: () => tryFunc(value.fst, value.snd),
errorHandler: err => errorHandler(value.fst, value.snd, err)));

/// <summary>
/// Invokes function in try/catch block and returns its result.
/// </summary>
/// <typeparam name="TValue">Type of value returned by invoked function.</typeparam>
/// <typeparam name="TFst">Type of first value in function tuple argument.</typeparam>
/// <typeparam name="TSnd">Type of second value in function tuple argument.</typeparam>
/// <param name="result">Result of previous operation.</param>
/// <param name="tryFunc">The function to be invoked in 'try' block.</param>
/// <param name="errorHandler">Handler invoked in 'catch' block on any raised exception.</param>
/// <returns><see cref="Result{TValue}"/> of invoked function in try block or <see cref="ErrorDetails"/> if any exception occurs.</returns>
public static Result<TValue> TryCatch3<TValue, TFst, TSnd, TTrd>(
this Result<(TFst fst, TSnd snd, TTrd trd)> result,
Func<TFst, TSnd, TTrd, TValue> tryFunc,
Func<TFst, TSnd, TTrd, Exception, ErrorDetails> errorHandler) =>
result.Bind(value => TryCatch(func: () => tryFunc(value.fst, value.snd, value.trd),
errorHandler: err => errorHandler(value.fst, value.snd, value.trd, err)));
}
}
36 changes: 0 additions & 36 deletions Monacs.Core/Tuples/Result.Tuple.Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,41 +59,5 @@ public static TVal Match3<TFst, TSnd, TTrd, TVal>(this Result<(TFst fst, TSnd sn
return result;
}

/* Try/Catch */

/// <summary>
///
/// </summary>
/// <typeparam name="TValue"></typeparam>
/// <typeparam name="TFst"></typeparam>
/// <typeparam name="TSnd"></typeparam>
/// <param name="result"></param>
/// <param name="tryFunc"></param>
/// <param name="errorHandler"></param>
/// <returns></returns>
public static Result<TValue> TryCatch2<TValue, TFst, TSnd>(
this Result<(TFst fst, TSnd snd)> result,
Func<TFst, TSnd, TValue> tryFunc,
Func<TFst, TSnd, Exception, ErrorDetails> errorHandler) =>
result.Bind(value => TryCatch(func: () => tryFunc(value.fst, value.snd),
errorHandler: err => errorHandler(value.fst, value.snd, err)));

/// <summary>
///
/// </summary>
/// <typeparam name="TValue"></typeparam>
/// <typeparam name="TFst"></typeparam>
/// <typeparam name="TSnd"></typeparam>
/// <typeparam name="TTrd"></typeparam>
/// <param name="result"></param>
/// <param name="tryFunc"></param>
/// <param name="errorHandler"></param>
/// <returns></returns>
public static Result<TValue> TryCatch3<TValue, TFst, TSnd, TTrd>(
this Result<(TFst fst, TSnd snd, TTrd trd)> result,
Func<TFst, TSnd, TTrd, TValue> tryFunc,
Func<TFst, TSnd, TTrd, Exception, ErrorDetails> errorHandler) =>
result.Bind(value => TryCatch(func: () => tryFunc(value.fst, value.snd, value.trd),
errorHandler: err => errorHandler(value.fst, value.snd, value.trd, err)));
}
}

0 comments on commit 95f311b

Please sign in to comment.