using System; using System.Collections; using Unity.Services.Authentication.Utilities; using Unity.Services.Core; namespace Unity.Services.Authentication { class AuthenticationAsyncOperation : IAsyncOperation { ILogger m_Logger; AsyncOperation m_AsyncOperation; AuthenticationException m_AuthenticationException; public AuthenticationAsyncOperation(ILogger logger) { m_Logger = logger; m_AsyncOperation = new AsyncOperation(); m_AsyncOperation.SetInProgress(); } /// /// Complete the operation as a failure. /// public void Fail(string errorCode, string message = null, Exception innerException = null) { Fail(new AuthenticationException(errorCode, message, innerException)); } /// /// Complete the operation as a failure with the exception. /// /// /// Exception with type other than are wrapped as /// an with error code AuthenticationError.UnknownError. /// public void Fail(Exception innerException) { if (innerException is AuthenticationException) { m_AuthenticationException = (AuthenticationException)innerException; } else { m_AuthenticationException = new AuthenticationException(AuthenticationError.UnknownError, null, innerException); } LogAuthenticationException(m_AuthenticationException); BeforeFail?.Invoke(this); m_AsyncOperation.Fail(m_AuthenticationException); } /// /// Complete this operation as a success. /// public void Succeed() { m_AsyncOperation.Succeed(); } /// /// The event to invoke in case of failure right before marking the operation done. /// This is a good place to put some cleanup code before sending out the completed callback. /// public event Action BeforeFail; /// public bool IsDone { get => m_AsyncOperation.IsDone; } /// public AsyncOperationStatus Status { get => m_AsyncOperation.Status; } /// public event Action Completed { add => m_AsyncOperation.Completed += value; remove => m_AsyncOperation.Completed -= value; } /// /// The exception that occured during the operation if it failed. /// The value can be set before the operation is done. /// public AuthenticationException Exception { get => m_AuthenticationException; } /// Exception IAsyncOperation.Exception { get => m_AuthenticationException; } /// bool IEnumerator.MoveNext() => !IsDone; /// /// /// Left empty because we don't support operation reset. /// void IEnumerator.Reset() {} /// object IEnumerator.Current => null; void LogAuthenticationException(AuthenticationException exception) { if (m_Logger == null) { return; } var logMessage = exception.Message; if (exception.InnerException != null) { logMessage += $" cause: ${exception.InnerException}"; } m_Logger.Error(logMessage); } } }