Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make exception summarizer work with derived exception types #4370

Merged
merged 1 commit into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,11 @@

namespace Microsoft.Extensions.Diagnostics.ExceptionSummarization;

/// <summary>
/// Looks through all the registered summary providers, returns a summary if possible.
/// </summary>
internal sealed class ExceptionSummarizer : IExceptionSummarizer
{
private const string DefaultDescription = "Unknown";
private readonly FrozenDictionary<Type, IExceptionSummaryProvider> _exceptionTypesToProviders;

/// <summary>
/// Initializes a new instance of the <see cref="ExceptionSummarizer"/> class.
/// </summary>
/// <param name="providers">All registered exception providers.</param>
public ExceptionSummarizer(IEnumerable<IExceptionSummaryProvider> providers)
{
var exceptionTypesToProvidersBuilder = new Dictionary<Type, IExceptionSummaryProvider>();
Expand All @@ -35,21 +28,22 @@ public ExceptionSummarizer(IEnumerable<IExceptionSummaryProvider> providers)
_exceptionTypesToProviders = exceptionTypesToProvidersBuilder.ToFrozenDictionary();
}

/// <summary>
/// It iterates through all registered summarizers, returns a summary if possible.
/// Default is <see cref="Exception"/> message if its length is less than 32, otherwise exception type name.
/// </summary>
/// <param name="exception">The exception.</param>
/// <returns>The summary of the given <see cref="Exception"/>.</returns>
Comment on lines -38 to -43
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why delete the existing xml-docs? They can still be useful.

public ExceptionSummary Summarize(Exception exception)
{
_ = Throw.IfNull(exception);
var exceptionType = exception.GetType();
var exceptionTypeName = exception.GetType().Name;

if (_exceptionTypesToProviders.TryGetValue(exceptionType, out var exceptionSummaryProvider))
// find a match for the exception type or a base type thereof
var type = exceptionType;
while (type != null)
{
return BuildSummary(exception, exceptionSummaryProvider, exceptionTypeName);
if (_exceptionTypesToProviders.TryGetValue(type, out var exceptionSummaryProvider))
{
return BuildSummary(exception, exceptionSummaryProvider, exceptionTypeName);
}

type = type.BaseType;
}

// Let's see if we get lucky with the inner exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,24 @@ public void Summarize_WithDefaultHResultAndInnerException_ReturnSummary()
Assert.Equal(exceptionHResultSummary, summary);
Assert.Equal(exceptionHResultSummary.ToString(), summary.ToString());
}

private class DerivedException : TestException
{
public DerivedException(uint hResult, string message, Exception innerException)
: base(hResult, message, innerException)
{
}
}

[Fact]
public void Summarize_WithDerivedExceptionType_ReturnSummary()
{
var exception = new DerivedException(0, "Test", new TestException(0));
var exceptionHResultSummary = new ExceptionSummary("DerivedException", "TestException", "Unknown");

var summary = _exceptionSummarizer.Summarize(exception);

Assert.Equal(exceptionHResultSummary, summary);
Assert.Equal(exceptionHResultSummary.ToString(), summary.ToString());
}
}