-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from martinsmith1968/feature/extra-extensions-…
…from-apps Feature/extra extensions from apps
- Loading branch information
Showing
6 changed files
with
523 additions
and
11 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
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,113 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace DNX.Helpers.Internal | ||
{ | ||
/// <summary> | ||
/// RunSafely - execute code with defaulted exception handling | ||
/// </summary> | ||
public class RunSafely | ||
{ | ||
/// <summary> | ||
/// Executes the specified action. | ||
/// </summary> | ||
/// <param name="action">The action.</param> | ||
/// <param name="exceptionHandler">The exception handler.</param> | ||
public static void Execute(Action action, Action<Exception> exceptionHandler = null) | ||
{ | ||
try | ||
{ | ||
action.Invoke(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Execute(() => exceptionHandler?.Invoke(ex)); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Executes the specified function. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="func">The function.</param> | ||
/// <param name="exceptionHandler">The exception handler.</param> | ||
/// <returns>T.</returns> | ||
public static T Execute<T>(Func<T> func, Action<Exception> exceptionHandler = null) | ||
{ | ||
return Execute(func, default, exceptionHandler); | ||
} | ||
|
||
/// <summary> | ||
/// Executes the specified function. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="func">The function.</param> | ||
/// <param name="defaultValue">The default value.</param> | ||
/// <param name="exceptionHandler">The exception handler.</param> | ||
/// <returns>T.</returns> | ||
public static T Execute<T>(Func<T> func, T defaultValue, Action<Exception> exceptionHandler = null) | ||
{ | ||
try | ||
{ | ||
return func.Invoke(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Execute(() => exceptionHandler?.Invoke(ex)); | ||
|
||
return defaultValue; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// execute as an asynchronous operation. | ||
/// </summary> | ||
/// <param name="task">The task.</param> | ||
/// <param name="exceptionHandler">The exception handler.</param> | ||
public static async Task ExecuteAsync(Task task, Action<Exception> exceptionHandler = null) | ||
{ | ||
try | ||
{ | ||
await task.ConfigureAwait(false); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Execute(() => exceptionHandler?.Invoke(ex)); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// execute as an asynchronous operation. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="task">The task.</param> | ||
/// <param name="exceptionHandler">The exception handler.</param> | ||
/// <returns>Task<T>.</returns> | ||
public static async Task<T> ExecuteAsync<T>(Task<T> task, Action<Exception> exceptionHandler = null) | ||
{ | ||
return await ExecuteAsync(task, default, exceptionHandler); | ||
} | ||
|
||
/// <summary> | ||
/// execute as an asynchronous operation. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="task">The task.</param> | ||
/// <param name="defaultValue">The default value.</param> | ||
/// <param name="exceptionHandler">The exception handler.</param> | ||
/// <returns>Task<T>.</returns> | ||
public static async Task<T> ExecuteAsync<T>(Task<T> task, T defaultValue, Action<Exception> exceptionHandler = null) | ||
{ | ||
try | ||
{ | ||
return await task.ConfigureAwait(false); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Execute(() => exceptionHandler?.Invoke(ex)); | ||
|
||
return defaultValue; | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.