namespace XFEEDToolBox.Core.Navigation; /// Classifies a failed journey after its expiring input leases have been released. public interface IFlightJourneyFailureClassifier { Task ClassifyFailureAsync(Exception failure,CancellationToken token); } public static class FlightJourneyFailure { /// A scene proof cannot hide a separate cancelled, protected or unknown cleanup cause. 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 ClassifyAsync(Exception failure,Func> savingScene,CancellationToken token) { token.ThrowIfCancellationRequested(); if(SavingSceneRecoveryRequiredException.Present(failure)||!HasTransientCause(failure,token))return failure; return await savingScene(token).ConfigureAwait(false)?new SavingSceneRecoveryRequiredException(failure):failure; } }