From cbfa4d95da595aa9aae5c3513b00c950bfa5edb3 Mon Sep 17 00:00:00 2001 From: Rain Sallow Date: Mon, 10 Jul 2023 17:09:54 -0400 Subject: [PATCH] (#3258) Expand logging for nuget resources errors Previously all that was logged was the surface exception, which in quite a few cases we've seen not yielding any useful information. Instead, log the entire exception chain, surfacing just the outermost and innermost exceptions, which we generally expect to contain the most relevant information for users. --- src/chocolatey/ExceptionExtensions.cs | 21 +++++++++++++++ src/chocolatey/chocolatey.csproj | 3 ++- .../nuget/NuGetEndpointResources.cs | 27 ++++++++++++++++++- 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 src/chocolatey/ExceptionExtensions.cs diff --git a/src/chocolatey/ExceptionExtensions.cs b/src/chocolatey/ExceptionExtensions.cs new file mode 100644 index 0000000000..eda7e450a9 --- /dev/null +++ b/src/chocolatey/ExceptionExtensions.cs @@ -0,0 +1,21 @@ +namespace chocolatey +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + + public static class ExceptionExtensions + { + public static IEnumerable Enumerate(this Exception error) + { + while (error != null) + { + yield return error; + + error = error.InnerException; + } + } + } +} diff --git a/src/chocolatey/chocolatey.csproj b/src/chocolatey/chocolatey.csproj index d52eaa8a74..7c2f36db0b 100644 --- a/src/chocolatey/chocolatey.csproj +++ b/src/chocolatey/chocolatey.csproj @@ -200,6 +200,7 @@ Properties\SolutionVersion.cs + @@ -538,4 +539,4 @@ --> - \ No newline at end of file + diff --git a/src/chocolatey/infrastructure.app/nuget/NuGetEndpointResources.cs b/src/chocolatey/infrastructure.app/nuget/NuGetEndpointResources.cs index e93e9b0be8..9189b69251 100644 --- a/src/chocolatey/infrastructure.app/nuget/NuGetEndpointResources.cs +++ b/src/chocolatey/infrastructure.app/nuget/NuGetEndpointResources.cs @@ -153,7 +153,32 @@ private T ResolveResource() { if (!_resolvingFailed) { - this.Log().Warn(ex.InnerException.Message); + // Unwrap the AggregateException as its surface message is useless + Exception error = ex.InnerException; + this.Log().Warn(error.Message); + + // Enumerate the inner exceptions, log all but the last one in the list to debug + string message = null; + foreach (var err in error.InnerException.Enumerate()) + { + if (message != null) + { + this.Log().Debug(message); + } + + message = err.Message; + error = err; + } + + // If the last error in the list isn't the only one, write its message as a warning. + // Typically the deepest/last error in the InnerExceptions will be a relevant and + // actionable error. + if (error != ex.InnerException && message != null) + { + this.Log().Warn(message); + } + + this.Log().Warn("For more information on this issue and guidance in resolving the problem, see https://ch0.co/t/svcidx"); _resolvingFailed = true; } }