From 3016f70349f5acfff40c099ca24d9553e458a3c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Kaan=20K=C3=B6se?= Date: Thu, 29 Aug 2024 22:43:27 +0200 Subject: [PATCH] Handling gmail errors. --- Wino.Core/Synchronizers/GmailSynchronizer.cs | 21 ++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/Wino.Core/Synchronizers/GmailSynchronizer.cs b/Wino.Core/Synchronizers/GmailSynchronizer.cs index 3b0ed163..4a15ecf6 100644 --- a/Wino.Core/Synchronizers/GmailSynchronizer.cs +++ b/Wino.Core/Synchronizers/GmailSynchronizer.cs @@ -793,6 +793,8 @@ namespace Wino.Core.Synchronizers var bundleRequestCount = bundle.Count(); + var bundleTasks = new List(); + for (int k = 0; k < bundleRequestCount; k++) { var requestBundle = bundle.ElementAt(k); @@ -802,37 +804,40 @@ namespace Wino.Core.Synchronizers request.ApplyUIChanges(); - // TODO: Queue is synchronous. Create a task bucket to await all processing. - nativeBatchRequest.Queue(nativeRequest, async (content, error, index, message) - => await ProcessSingleNativeRequestResponseAsync(requestBundle, error, message, cancellationToken).ConfigureAwait(false)); + nativeBatchRequest.Queue(nativeRequest, (content, error, index, message) + => bundleTasks.Add(ProcessSingleNativeRequestResponseAsync(requestBundle, error, message, cancellationToken))); } await nativeBatchRequest.ExecuteAsync(cancellationToken).ConfigureAwait(false); + + await Task.WhenAll(bundleTasks); } } - private void ProcessGmailRequestError(RequestError error) + private void ProcessGmailRequestError(RequestError error, IRequestBundle bundle) { if (error == null) return; // OutOfMemoryException is a known bug in Gmail SDK. if (error.Code == 0) { + bundle?.Request.RevertUIChanges(); throw new OutOfMemoryException(error.Message); } // Entity not found. if (error.Code == 404) { + bundle?.Request.RevertUIChanges(); throw new SynchronizerEntityNotFoundException(error.Message); } if (!string.IsNullOrEmpty(error.Message)) { + bundle?.Request.RevertUIChanges(); error.Errors?.ForEach(error => _logger.Error("Unknown Gmail SDK error for {Name}\n{Error}", Account.Name, error)); - // TODO: Debug - // throw new SynchronizerException(error.Message); + throw new SynchronizerException(error.Message); } } @@ -851,7 +856,7 @@ namespace Wino.Core.Synchronizers { try { - ProcessGmailRequestError(error); + ProcessGmailRequestError(error, null); } catch (OutOfMemoryException) { @@ -900,7 +905,7 @@ namespace Wino.Core.Synchronizers HttpResponseMessage httpResponseMessage, CancellationToken cancellationToken = default) { - ProcessGmailRequestError(error); + ProcessGmailRequestError(error, bundle); if (bundle is HttpRequestBundle messageBundle) {