From 9bfc034807a23e8de522efbad0a416b683557d4c Mon Sep 17 00:00:00 2001 From: Next Turn <45985406+nxtn@users.noreply.github.com> Date: Mon, 9 Aug 2021 13:50:53 +0800 Subject: [PATCH] Fix download exceptions when ignoring errors (#854) --- src/WinSW.Core/Download.cs | 8 +++-- src/WinSW.Tests/DownloadTests.cs | 7 +++-- src/WinSW/WrapperService.cs | 51 +++----------------------------- 3 files changed, 14 insertions(+), 52 deletions(-) diff --git a/src/WinSW.Core/Download.cs b/src/WinSW.Core/Download.cs index 4610112..3b2112c 100644 --- a/src/WinSW.Core/Download.cs +++ b/src/WinSW.Core/Download.cs @@ -231,10 +231,14 @@ namespace WinSW if (supportsIfModifiedSince && ((HttpWebResponse?)e.Response)?.StatusCode == HttpStatusCode.NotModified) { Logger.Info($"Skipped downloading unmodified resource '{this.From}'"); + return; } - else + + string errorMessage = $"Failed to download {this.From} to {this.To}"; + Logger.Error(errorMessage, e); + if (this.FailOnError) { - throw; + throw new IOException(errorMessage, e); } } } diff --git a/src/WinSW.Tests/DownloadTests.cs b/src/WinSW.Tests/DownloadTests.cs index 3c62c66..4380b9d 100644 --- a/src/WinSW.Tests/DownloadTests.cs +++ b/src/WinSW.Tests/DownloadTests.cs @@ -215,10 +215,11 @@ namespace winswTests await this.TestClientServerAsync( async (source, dest) => { - var exception = await AsyncAssert.ThrowsAsync( - async () => await new Download(source, dest).PerformAsync()); + var exception = await AsyncAssert.ThrowsAsync( + async () => await new Download(source, dest, true).PerformAsync()); - Assert.That(exception.Status, Is.EqualTo(WebExceptionStatus.ProtocolError)); + var inner = (WebException)exception.InnerException; + Assert.That(inner.Status, Is.EqualTo(WebExceptionStatus.ProtocolError)); }, context => { diff --git a/src/WinSW/WrapperService.cs b/src/WinSW/WrapperService.cs index 608a99c..8c82d4a 100644 --- a/src/WinSW/WrapperService.cs +++ b/src/WinSW/WrapperService.cs @@ -21,9 +21,9 @@ namespace WinSW public class WrapperService : ServiceBase, IEventLogger { private readonly Process process = new(); - + private readonly IServiceConfig config; - + private Dictionary? envs; internal WinSWExtensionManager ExtensionManager { get; private set; } @@ -246,57 +246,14 @@ namespace WinSW tasks[i] = download.PerformAsync(); } - try - { - Task.WaitAll(tasks); - } - catch (AggregateException e) - { - var exceptions = new List(e.InnerExceptions.Count); - for (int i = 0; i < tasks.Length; i++) - { - if (tasks[i].IsFaulted) - { - var download = downloads[i]; - string errorMessage = $"Failed to download {download.From} to {download.To}"; - var exception = tasks[i].Exception!; - this.LogEvent($"{errorMessage}. {exception.Message}"); - Log.Error(errorMessage, exception); - - // TODO: move this code into the download logic - if (download.FailOnError) - { - exceptions.Add(new IOException(errorMessage, exception)); - } - } - } - - throw new AggregateException(exceptions); - } + Task.WaitAll(tasks); #else foreach (var download in this.config.Downloads) { string downloadMessage = $"Downloading: {download.From} to {download.To}. failOnError={download.FailOnError.ToString()}"; this.LogEvent(downloadMessage); Log.Info(downloadMessage); - try - { - download.Perform(); - } - catch (Exception e) - { - string errorMessage = $"Failed to download {download.From} to {download.To}"; - this.LogEvent($"{errorMessage}. {e.Message}"); - Log.Error(errorMessage, e); - - // TODO: move this code into the download logic - if (download.FailOnError) - { - throw new IOException(errorMessage, e); - } - - // Else just keep going - } + download.Perform(); } #endif