-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add in-code doc for TryCatch, create seperate file for those extensions
- Loading branch information
Showing
4 changed files
with
77 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -250,3 +250,5 @@ paket-files/ | |
# JetBrains Rider | ||
.idea/ | ||
*.sln.iml | ||
|
||
*.orig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters