Skip to content

Commit

Permalink
Merge pull request chocolatey#3262 from vexx32/3258-exception-logging
Browse files Browse the repository at this point in the history
(chocolatey#3258) Expand logging for nuget resources errors
  • Loading branch information
corbob authored Jul 12, 2023
2 parents a3305eb + cbfa4d9 commit 9c470f1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 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;
}
}
}
}
5 changes: 3 additions & 2 deletions src/chocolatey/chocolatey.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\Microsoft.CodeAnalysis.BannedApiAnalyzers.3.3.3\build\Microsoft.CodeAnalysis.BannedApiAnalyzers.props" Condition="Exists('..\packages\Microsoft.CodeAnalysis.BannedApiAnalyzers.3.3.3\build\Microsoft.CodeAnalysis.BannedApiAnalyzers.props')" />
<PropertyGroup>
Expand Down 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 9c470f1

Please sign in to comment.