XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEEDToolBox

【WinUI】XFE精英危险工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
namespace XFEEDToolBox.Core.Navigation;

/// <summary>Classifies a failed journey after its expiring input leases have been released.</summary>
public interface IFlightJourneyFailureClassifier
{
    Task<Exception> ClassifyFailureAsync(Exception failure,CancellationToken token);
}

public static class FlightJourneyFailure
{
    /// <summary>A scene proof cannot hide a separate cancelled, protected or unknown cleanup cause.</summary>
    public static bool CanRecoverScene(Exception error)=>!HasProtectedCause(error)&&!FlightConnectionRecovery.HasCancellationCause(error)&&RecognizedCauses(error);
    private static bool RecognizedCauses(Exception error)
    {
        if(error is AggregateException aggregate)return aggregate.InnerExceptions.Count>0&&aggregate.InnerExceptions.All(RecognizedCauses);
        if(error is SavingSceneRecoveryRequiredException or FlightConnectionRecoveryRequiredException)return true;
        if(error.InnerException is {} inner&&(SavingSceneRecoveryRequiredException.Present(inner)||FlightConnectionRecoveryRequiredException.Find(inner) is not null))
            return RecognizedCauses(inner);
        return HasTransientCause(error,default);
    }
    public static bool HasTransientCause(Exception? failure,CancellationToken token)
    {
        if(token.IsCancellationRequested||failure is null)return false;
        if(HasProtectedCause(failure)||FlightConnectionRecoveryRequiredException.Find(failure) is not null)return false;
        // A cleanup failure can contain more than one cause. A real identity
        // or health failure must not be hidden by a concurrent transport fault.
        if(failure is AggregateException aggregate)return aggregate.InnerExceptions.Count>0
            &&aggregate.InnerExceptions.All(inner=>HasTransientCause(inner,token));
        return NavigationRecovery.CanRetry(failure,token)||HasTransientCause(failure.InnerException,token);
    }
    internal static bool HasProtectedCause(Exception error)=>error is InvalidDataException or FlightSafetyPauseException or DockingHandoffException or FlightDestinationChangedException
        ||error is AggregateException aggregate&&aggregate.InnerExceptions.Any(HasProtectedCause)
        ||error.InnerException is {} inner&&HasProtectedCause(inner);
    public static async Task<Exception> ClassifyAsync(Exception failure,Func<CancellationToken,Task<bool>> savingScene,CancellationToken token)
    {
        token.ThrowIfCancellationRequested();
        if(SavingSceneRecoveryRequiredException.Present(failure)||!HasTransientCause(failure,token))return failure;
        return await savingScene(token).ConfigureAwait(false)?new SavingSceneRecoveryRequiredException(failure):failure;
    }
}