Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reporting of wrong download as failed #4236

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Core/Net/NetAsyncDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ public void DownloadAndWait(IList<DownloadTarget> targets)
}

// Check to see if we've had any errors. If so, then release the kraken!
var exceptions = downloads.Select((dl, i) => dl.error is Exception exc
? new KeyValuePair<int, Exception>(i, exc)
: (KeyValuePair<int, Exception>?)null)
.OfType<KeyValuePair<int, Exception>>()
var exceptions = downloads.Select(dl => dl.error != null
? new KeyValuePair<DownloadTarget, Exception>(dl.target, dl.error)
: (KeyValuePair<DownloadTarget, Exception>?)null)
.OfType<KeyValuePair<DownloadTarget, Exception>>()
.ToList();

if (exceptions.Select(kvp => kvp.Value)
Expand All @@ -153,7 +153,7 @@ public void DownloadAndWait(IList<DownloadTarget> targets)
&& wex.Response is HttpWebResponse hresp
// Handle HTTP 403 used for throttling
&& hresp.StatusCode == HttpStatusCode.Forbidden
&& downloads[kvp.Key].CurrentUri is Uri url
&& kvp.Key.urls.LastOrDefault() is Uri url
&& url.IsAbsoluteUri
&& Net.ThrottledHosts.TryGetValue(url.Host, out Uri? infoUrl)
&& infoUrl is not null
Expand Down
23 changes: 15 additions & 8 deletions Core/Net/NetAsyncModulesDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,31 @@ public void DownloadModules(IEnumerable<CkanModule> modules)
// Add all the requested modules
this.modules.AddRange(moduleGroups.SelectMany(grp => grp));

var preferredHosts = ServiceLocator.Container.Resolve<IConfiguration>().PreferredHosts;
var targets = moduleGroups
// Skip any group that already has a URL in progress
.Where(grp => grp.All(mod => mod.download?.All(dlUri => !activeURLs.Contains(dlUri)) ?? false))
// Each group gets one target containing all the URLs
.Select(grp => TargetFromModuleGroup(grp, preferredHosts))
.ToArray();
try
{
cancelTokenSrc = new CancellationTokenSource();
var preferredHosts = ServiceLocator.Container.Resolve<IConfiguration>().PreferredHosts;
// Start the downloads!
downloader.DownloadAndWait(moduleGroups
// Skip any group that already has a URL in progress
.Where(grp => grp.All(mod => mod.download?.All(dlUri => !activeURLs.Contains(dlUri)) ?? false))
// Each group gets one target containing all the URLs
.Select(grp => TargetFromModuleGroup(grp, preferredHosts))
.ToArray());
downloader.DownloadAndWait(targets);
this.modules.Clear();
AllComplete?.Invoke();
}
catch (DownloadErrorsKraken kraken)
{
// Associate the errors with the affected modules
var exc = new ModuleDownloadErrorsKraken(this.modules.ToList(), kraken);
// Find a module for each target
var targetModules = targets.Select(t => this.modules
.First(m => m.download?.Intersect(t.urls)
.Any()
?? false))
.ToList();
var exc = new ModuleDownloadErrorsKraken(targetModules, kraken);
// Clear this.modules because we're done with these
this.modules.Clear();
throw exc;
Expand Down
11 changes: 3 additions & 8 deletions Core/Repositories/RepositoryDataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public UpdateResult Update(Repository[] repos,
catch (UnsupportedKraken kraken)
{
// Show parsing errors in the Downloads Failed popup
throw new DownloadErrorsKraken(Array.IndexOf(toUpdate, repo), kraken);
throw new DownloadErrorsKraken(target, kraken);
}
progress.NextFile();
}
Expand All @@ -220,15 +220,10 @@ public UpdateResult Update(Repository[] repos,
// Fire an event so affected registry objects can clear their caches
Updated?.Invoke(toUpdate);
}
catch (DownloadErrorsKraken exc)
catch (DownloadErrorsKraken)
{
loadETags();
throw new DownloadErrorsKraken(
// Renumber the exceptions based on the original repo list
exc.Exceptions.Select(kvp => new KeyValuePair<int, Exception>(
Array.IndexOf(repos, toUpdate[kvp.Key]),
kvp.Value))
.ToList());
throw;
}
catch (Exception exc)
{
Expand Down
34 changes: 19 additions & 15 deletions Core/Types/Kraken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,24 +324,23 @@ public FileExistsKraken(string filename, string? reason = null, Exception? inner
/// </summary>
public class DownloadErrorsKraken : Kraken
{
public readonly List<KeyValuePair<int, Exception>> Exceptions
= new List<KeyValuePair<int, Exception>>();

public DownloadErrorsKraken(List<KeyValuePair<int, Exception>> errors)
public DownloadErrorsKraken(List<KeyValuePair<NetAsyncDownloader.DownloadTarget, Exception>> errors)
: base(string.Join(Environment.NewLine,
new string[] { Properties.Resources.KrakenDownloadErrorsHeader, "" }
.Concat(errors.Select(e => e.Value.Message))))
{
Exceptions = new List<KeyValuePair<int, Exception>>(errors);
Exceptions = new List<KeyValuePair<NetAsyncDownloader.DownloadTarget, Exception>>(errors);
}

public DownloadErrorsKraken(int index, Exception exc)
public DownloadErrorsKraken(NetAsyncDownloader.DownloadTarget target, Exception exc)
{
Exceptions = new List<KeyValuePair<int, Exception>>
Exceptions = new List<KeyValuePair<NetAsyncDownloader.DownloadTarget, Exception>>
{
new KeyValuePair<int, Exception>(index, exc),
new KeyValuePair<NetAsyncDownloader.DownloadTarget, Exception>(target, exc),
};
}

public readonly List<KeyValuePair<NetAsyncDownloader.DownloadTarget, Exception>> Exceptions;
}

/// <summary>
Expand All @@ -350,9 +349,6 @@ public DownloadErrorsKraken(int index, Exception exc)
/// </summary>
public class ModuleDownloadErrorsKraken : Kraken
{
public readonly List<KeyValuePair<CkanModule, Exception>> Exceptions
= new List<KeyValuePair<CkanModule, Exception>>();

/// <summary>
/// Initialize the exception.
/// </summary>
Expand All @@ -361,11 +357,16 @@ public readonly List<KeyValuePair<CkanModule, Exception>> Exceptions
public ModuleDownloadErrorsKraken(IList<CkanModule> modules, DownloadErrorsKraken kraken)
: base()
{
foreach (var kvp in kraken.Exceptions)
foreach ((NetAsyncDownloader.DownloadTarget target, Exception exc) in kraken.Exceptions)
{
Exceptions.Add(new KeyValuePair<CkanModule, Exception>(
modules[kvp.Key], kvp.Value.GetBaseException() ?? kvp.Value
));
foreach (var module in modules.Where(m => m.download?.Intersect(target.urls)
.Any()
?? false))
{
Exceptions.Add(new KeyValuePair<CkanModule, Exception>(
module,
exc.GetBaseException() ?? exc));
}
}
}

Expand Down Expand Up @@ -394,6 +395,9 @@ public override string ToString()
return builder.ToString();
}

public readonly List<KeyValuePair<CkanModule, Exception>> Exceptions
= new List<KeyValuePair<CkanModule, Exception>>();

private StringBuilder? builder = null;
}

Expand Down
4 changes: 3 additions & 1 deletion GUI/Main/MainRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ private void UpdateRepo(object? sender, DoWorkEventArgs? e)
Properties.Resources.RepoDownloadsFailedColHdr,
Properties.Resources.RepoDownloadsFailedAbortBtn,
k.Exceptions.Select(kvp => new KeyValuePair<object[], Exception>(
new object[] { repos[kvp.Key] }, kvp.Value)),
repos.Where(r => kvp.Key.urls.Contains(r.uri))
.ToArray(),
kvp.Value)),
// Rows are only linked to themselves
(r1, r2) => r1 == r2);
dfd.ShowDialog(this);
Expand Down
3 changes: 2 additions & 1 deletion Tests/Core/Net/NetAsyncDownloaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ public void DownloadAndWait_WithSomeInvalidUrls_ThrowsDownloadErrorsKraken(
foreach (var kvp in exception?.Exceptions!)
{
var baseExc = kvp.Value.GetBaseException() as FileNotFoundException;
Assert.AreEqual(fromPaths[kvp.Key], baseExc?.FileName);
Assert.AreEqual(fromPaths[Array.IndexOf(targets, kvp.Key)],
baseExc?.FileName);
}
foreach (var t in validTargets)
{
Expand Down