Fix download exceptions when ignoring errors

pull/854/head
Next Turn 2021-08-09 13:10:41 +08:00
parent 20846238ea
commit 4aa0b5b6f2
2 changed files with 10 additions and 49 deletions

View File

@ -231,10 +231,14 @@ namespace WinSW
if (supportsIfModifiedSince && ((HttpWebResponse?)e.Response)?.StatusCode == HttpStatusCode.NotModified) if (supportsIfModifiedSince && ((HttpWebResponse?)e.Response)?.StatusCode == HttpStatusCode.NotModified)
{ {
Logger.Info($"Skipped downloading unmodified resource '{this.From}'"); 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);
} }
} }
} }

View File

@ -246,58 +246,15 @@ namespace WinSW
tasks[i] = download.PerformAsync(); tasks[i] = download.PerformAsync();
} }
try
{
Task.WaitAll(tasks); Task.WaitAll(tasks);
}
catch (AggregateException e)
{
var exceptions = new List<Exception>(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);
}
#else #else
foreach (var download in this.config.Downloads) foreach (var download in this.config.Downloads)
{ {
string downloadMessage = $"Downloading: {download.From} to {download.To}. failOnError={download.FailOnError.ToString()}"; string downloadMessage = $"Downloading: {download.From} to {download.To}. failOnError={download.FailOnError.ToString()}";
this.LogEvent(downloadMessage); this.LogEvent(downloadMessage);
Log.Info(downloadMessage); Log.Info(downloadMessage);
try
{
download.Perform(); 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
}
}
#endif #endif
string? startArguments = this.config.StartArguments; string? startArguments = this.config.StartArguments;