//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// UserRTX.NET satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
using System.Threading.Tasks;

#if !WASM
namespace user_rtx { using u = user_rtx;
#else
namespace user_rtx.wasm { using u = user_rtx.wasm;
#endif

/// <summary>
/// General exception catching (guard)
/// </summary>
static partial class ops {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static Exception? Catch( Action action )
{
   try { action(); return null; }
   catch (Exception e) { return e; }
}

public static Exception? Catch<R>( out R? result, Func<R> action )
{
   try {
      result = action();
      return null;
   }
   catch ( Exception e ) {
      result = default;
      return e;
   }
}

//==================================================================================================

public static async Task<Exception?> Catch_A( Func<Task> actionA )
{
   try {
      await actionA().ConfigureAwait(false);
      return null;
   }
   catch ( Exception e ) {
      return e;
   }
}

public static async Task<(Exception?,R?)> Catch_A<R>( Func<Task<R>> functionA )
{
   try {
      return ( null,
         await functionA().ConfigureAwait(false) );
   }
   catch ( Exception e ) {
      return ( e, default );
   }
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // ops

} // user_rtx (.wasm)
