Fix download exceptions when ignoring errors (#854)

pull/874/head
Next Turn 2021-08-09 13:50:53 +08:00 committed by GitHub
parent 20846238ea
commit 9bfc034807
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 52 deletions

View File

@ -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);
}
}
}

View File

@ -215,10 +215,11 @@ namespace winswTests
await this.TestClientServerAsync(
async (source, dest) =>
{
var exception = await AsyncAssert.ThrowsAsync<WebException>(
async () => await new Download(source, dest).PerformAsync());
var exception = await AsyncAssert.ThrowsAsync<IOException>(
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 =>
{

View File

@ -246,57 +246,14 @@ namespace WinSW
tasks[i] = download.PerformAsync();
}
try
{
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);
}
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