using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

using CommonHelpers;

namespace MyFlat {

//**************************************************************************************************
partial class XControllerBase {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/// <exception cref="InvalidOperationException" />
/// <exception cref="HUnsuccessException" />
[HAsyncStatic]
protected Task<dynamic> DoAuto_A<T>( string methodNameOrSignature,
                                     Func<string,T> errorResponseCreator,
                                     Func<Task<T>> bodyA )
where T: class =>
   DoAuto_A<T>( errorResponseCreator, bodyA, methodNameOrSignature );

/// <exception cref="InvalidOperationException" />
/// <exception cref="HUnsuccessException" />
[HAsyncStatic]
protected async Task<dynamic> DoAuto_A<T>( Func<string,T> errorResponseCreator, Func<Task<T>> bodyA,
                                           [CallerMemberName][HRequired]
                                           string? methodNameOrSignature=null )
where T: class
{
   BeginDo( HArgChecking.NN( methodNameOrSignature ) );
   try {
      return await bodyA().ConfigureAwait(false);
   }
   catch (Exception e) {
      ReportException( e );
      return errorResponseCreator( MSG.ServerError( e ) ) ??
         throw ErrorResponseCreatorReturnedNull_Error;
   }
   finally {
      EndDo();
   }
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // XControllerBase
//**************************************************************************************************

} // MyFlat
