From 62e28b9554a53bf9570f0e203a4083f6f65e6516 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Sharma?= Date: Thu, 23 Jan 2020 08:44:54 -0800 Subject: [PATCH] Gracefully handle mirror failures for offline scenarios (#448) Addresses https://github.com/loic-sharma/BaGet/issues/432 --- src/BaGet.Core/Mirror/MirrorService.cs | 30 ++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/BaGet.Core/Mirror/MirrorService.cs b/src/BaGet.Core/Mirror/MirrorService.cs index 82ba631d8..1b8d3e1e8 100644 --- a/src/BaGet.Core/Mirror/MirrorService.cs +++ b/src/BaGet.Core/Mirror/MirrorService.cs @@ -36,8 +36,12 @@ public async Task> FindPackageVersionsOrNullAsync( 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; } @@ -51,8 +55,12 @@ public async Task> FindPackageVersionsOrNullAsync( public async Task> 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; } @@ -168,6 +176,20 @@ private IEnumerable FindDependenciesFromDependencyGroup(Depen }); } + private async Task RunOrNull(string id, string data, Func> 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();