namespace XFEEDToolBox.Core.Navigation;
public sealed class FlightControlCleanupException(Exception primary,Exception cleanup):InvalidOperationException(primary.Message,primary)
{
public Exception CleanupFailure{get;}=cleanup;
public override string ToString()=>base.ToString()+"\n释放控制时的附加错误:\n"+CleanupFailure;
}
public static class FlightControlCleanup
{
/// <summary>Independent owned controls must all be released, even when one release cannot be confirmed.</summary>
public static async Task RunAllAsync(params Func<Task>[] releases)
{
List<Exception>? failures=null;
foreach(var release in releases)
try{await release().ConfigureAwait(false);}
catch(Exception error){(failures??=[]).Add(error);}
if(failures is {Count:1})System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(failures[0]).Throw();
if(failures is {Count:>1})throw new AggregateException("Not all owned flight controls confirmed release.",failures);
}
public static async Task RunAsync(Func<Task> operation,Func<Task> release)
{
Exception? primary=null;
try{await operation().ConfigureAwait(false);}
catch(Exception error){primary=error;throw;}
finally
{
try{await release().ConfigureAwait(false);}
catch(Exception cleanup)when(primary is not null)
{
if(primary is OperationCanceledException){primary.Data["ControlReleaseFailure"]=cleanup.ToString();System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(primary).Throw();}
throw new FlightControlCleanupException(primary,cleanup);
}
}
}
}
namespace XFEEDToolBox.Core.Navigation;
public sealed class FlightControlCleanupException(Exception primary,Exception cleanup):InvalidOperationException(primary.Message,primary)
{
public Exception CleanupFailure{get;}=cleanup;
public override string ToString()=>base.ToString()+"\n释放控制时的附加错误:\n"+CleanupFailure;
}
public static class FlightControlCleanup
{
/// <summary>Independent owned controls must all be released, even when one release cannot be confirmed.</summary>
public static async Task RunAllAsync(params Func<Task>[] releases)
{
List<Exception>? failures=null;
foreach(var release in releases)
try{await release().ConfigureAwait(false);}
catch(Exception error){(failures??=[]).Add(error);}
if(failures is {Count:1})System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(failures[0]).Throw();
if(failures is {Count:>1})throw new AggregateException("Not all owned flight controls confirmed release.",failures);
}
public static async Task RunAsync(Func<Task> operation,Func<Task> release)
{
Exception? primary=null;
try{await operation().ConfigureAwait(false);}
catch(Exception error){primary=error;throw;}
finally
{
try{await release().ConfigureAwait(false);}
catch(Exception cleanup)when(primary is not null)
{
if(primary is OperationCanceledException){primary.Data["ControlReleaseFailure"]=cleanup.ToString();System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(primary).Throw();}
throw new FlightControlCleanupException(primary,cleanup);
}
}
}
}