From e214eb51bc80423ff7b6bb8c5300ba106dca1441 Mon Sep 17 00:00:00 2001 From: Jiaqi Liu Date: Wed, 20 Dec 2023 21:00:10 -0800 Subject: [PATCH] Bug fix for not rethrowing immediately for non-retriable exceptions --- src/Notepads/Utilities/FileSystemUtility.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Notepads/Utilities/FileSystemUtility.cs b/src/Notepads/Utilities/FileSystemUtility.cs index 57b3c5dcb..a2e06364d 100644 --- a/src/Notepads/Utilities/FileSystemUtility.cs +++ b/src/Notepads/Utilities/FileSystemUtility.cs @@ -640,17 +640,23 @@ public static async Task WriteTextToFileAsync(StorageFile storageFile, string te } catch (Exception ex) { + // Rethrow the last attempt exception regardless of its type if (retryAttempts == maxRetryAttempts) { - throw; // Rethrow the last attempt exception + throw; } + // Delay and retry for retriable errors else if ((ex.HResult == ERROR_ACCESS_DENIED) || (ex.HResult == ERROR_SHARING_VIOLATION) || (ex.HResult == ERROR_UNABLE_TO_REMOVE_REPLACED) || (ex.HResult == ERROR_FAIL)) { errorCodes.Add("0x" + Convert.ToString(ex.HResult, 16)); - await Task.Delay(10); // Delay 10ms before retrying for all retriable errors + await Task.Delay(10); + } + else // Throw immediately for other errors + { + throw; } } }