Skip to content

Commit

Permalink
Fix download exceptions when ignoring errors (winsw#853)
Browse files Browse the repository at this point in the history
  • Loading branch information
nxtn committed Aug 9, 2021
1 parent b774ee1 commit 71f08e5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 31 deletions.
8 changes: 6 additions & 2 deletions src/WinSW.Core/Download.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,14 @@ public async Task PerformAsync()
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);
}
}
}
Expand Down
27 changes: 1 addition & 26 deletions src/WinSW.Core/WrapperService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,32 +272,7 @@ private void DoStart()
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!;
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);

var sharedDirectories = this.config.SharedDirectories;
if (sharedDirectories.Count > 0)
Expand Down
7 changes: 4 additions & 3 deletions src/WinSW.Tests/DownloadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,11 @@ public async Task TestHttp_NotFound_ThrowsAsync()
await this.TestClientServerAsync(
async (source, dest) =>
{
var exception = await Assert.ThrowsAsync<WebException>(
async () => await new Download(source, dest).PerformAsync());
var exception = await Assert.ThrowsAsync<IOException>(
async () => await new Download(source, dest, true).PerformAsync());
Assert.Equal(WebExceptionStatus.ProtocolError, exception.Status);
var inner = Assert.IsType<WebException>(exception.InnerException);
Assert.Equal(WebExceptionStatus.ProtocolError, inner.Status);
},
context =>
{
Expand Down

0 comments on commit 71f08e5

Please sign in to comment.