using System; using System.Threading; using System.Threading.Tasks; using UnityEngine; namespace Crowdin.Plugin.Helpers { public static class TaskExtensions { /// Starts a fire-and-forget task from a synchronous entry point (button click, editor callback) /// and logs any fault to the Console. Unity does not surface unobserved Task exceptions, so /// discarding a Task with `_ = ...` silently swallows everything the method itself doesn't catch. public static void Forget(this Task task) { if (task is null) { return; } if (task.IsCompleted) { LogFault(task); return; } task.ContinueWith( LogFault, CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default); } private static void LogFault(Task task) { if (task.Exception is null) { return; } foreach (Exception exception in task.Exception.Flatten().InnerExceptions) { Debug.LogException(exception); } } } }