Skip to content

Commit

Permalink
(chocolatey#3258) Expand logging for nuget resources errors
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vexx32 authored and corbob committed Jul 12, 2023
1 parent 38905a1 commit cbfa4d9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
21 changes: 21 additions & 0 deletions src/chocolatey/ExceptionExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<Exception> Enumerate(this Exception error)
{
while (error != null)
{
yield return error;

error = error.InnerException;
}
}
}
}
3 changes: 2 additions & 1 deletion src/chocolatey/chocolatey.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@
<Link>Properties\SolutionVersion.cs</Link>
</Compile>
<Compile Include="AssemblyExtensions.cs" />
<Compile Include="ExceptionExtensions.cs" />
<Compile Include="infrastructure.app\attributes\MultiServiceAttribute.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyCacheCommand.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyListCommand.cs" />
Expand Down Expand Up @@ -538,4 +539,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,32 @@ private T ResolveResource<T>()
{
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;
}
}
Expand Down

0 comments on commit cbfa4d9

Please sign in to comment.