Skip to content

Commit

Permalink
Gracefully handle mirror failures for offline scenarios (#448)
Browse files Browse the repository at this point in the history
Addresses #432
  • Loading branch information
loic-sharma committed Jan 23, 2020
1 parent 9d58ca5 commit 62e28b9
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/BaGet.Core/Mirror/MirrorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ public class MirrorService : IMirrorService
string id,
CancellationToken cancellationToken)
{
var upstreamVersions = await _upstreamClient.ListPackageVersionsAsync(id, includeUnlisted: true, cancellationToken);
if (!upstreamVersions.Any())
var upstreamVersions = await RunOrNull(
id,
"versions",
() => _upstreamClient.ListPackageVersionsAsync(id, includeUnlisted: true, cancellationToken));

if (upstreamVersions == null || !upstreamVersions.Any())
{
return null;
}
Expand All @@ -51,8 +55,12 @@ public class MirrorService : IMirrorService

public async Task<IReadOnlyList<Package>> FindPackagesOrNullAsync(string id, CancellationToken cancellationToken)
{
var items = await _upstreamClient.GetPackageMetadataAsync(id, cancellationToken);
if (!items.Any())
var items = await RunOrNull(
id,
"metadata",
() => _upstreamClient.GetPackageMetadataAsync(id, cancellationToken));

if (items == null || !items.Any())
{
return null;
}
Expand Down Expand Up @@ -168,6 +176,20 @@ private IEnumerable<PackageDependency> FindDependenciesFromDependencyGroup(Depen
});
}

private async Task<T> RunOrNull<T>(string id, string data, Func<Task<T>> x)
where T : class
{
try
{
return await x();
}
catch (Exception e)
{
_logger.LogError(e, "Unable to mirror package {Package}'s upstream {Data}", id, data);
return null;
}
}

private async Task IndexFromSourceAsync(string id, NuGetVersion version, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
Expand Down

0 comments on commit 62e28b9

Please sign in to comment.