Skip to content

Conversation

epsitec
Copy link
Contributor

@epsitec epsitec commented Sep 13, 2024

Related to dotnet/runtime#107743

Summary

Fixes an incorrect description of what method GetBaseException() does.

@epsitec epsitec requested a review from a team as a code owner September 13, 2024 06:12
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Sep 13, 2024
@ghost ghost added the area-System.Runtime label Sep 13, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-runtime

This comment was marked as outdated.

Copy link

Learn Build status updates of commit 4f513d6:

✅ Validation status: passed

File Status Preview URL Details
xml/System/AggregateException.xml ✅Succeeded View

For more details, please refer to the build report.

For any questions, please:

Comment on lines +685 to +686
<summary>Returns the <see cref="T:System.Exception"/> that is the root cause of this exception. This exception is either the root exception or the first <see cref="T:System.AggregateException"/> that contains either multiple inner exceptions or no inner exceptions at all.</summary>
<returns>The <see cref="T:System.Exception" /> that is the root cause of this exception.</returns>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<summary>Returns the <see cref="T:System.Exception"/> that is the root cause of this exception. This exception is either the root exception or the first <see cref="T:System.AggregateException"/> that contains either multiple inner exceptions or no inner exceptions at all.</summary>
<returns>The <see cref="T:System.Exception" /> that is the root cause of this exception.</returns>
<summary>Returns the <see cref="T:System.AggregateException"/> that is the root cause of this exception. This exception is either the current exception or the first <see cref="T:System.AggregateException"/> that contains either multiple inner exceptions or no inner exceptions at all.</summary>
<returns>The <see cref="T:System.AggregateException" /> that is the root cause of this exception.</returns>

https://learn.microsoft.com/en-us/dotnet/api/system.exception.getbaseexception and other docs use "current exception" for this.

Also, I think the original docs were better in highlighting that the returned instance is always AggregateException.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The original docs were wrong, hence my fixes. The code will return the plain Exception which was thrown up the chain (what's now called the current exception), and only if there are multiple exceptions wrapped in an AggregateException will it return an aggregate exception. That's the whole point of this pull request.

Copy link
Member

Choose a reason for hiding this comment

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

Ah ok, I have misread how the implementation behaves.

I agree with you that the return type in the docs is wrong.

I am not sure whether it makes sense to try to describe the exact algorithm used by this method in the docs. The current implementation of Exception.GetBaseException and AggregateException.GetBaseException methods are in conflict with the contract for these methods described in the docs.

https://learn.microsoft.com/en-us/dotnet/api/system.exception.getbaseexception?view=net-8.0 says "For all exceptions in a chain of exceptions, the GetBaseException method must return the same object (the base exception).". For example, this invariant does not seem to hold for

TargetInvocationException 
   -> AggregateException
          -> ArgumentException
          -> ArgumentException

GetBaseException() called on TargetInvocationException returns first ArgumentException for , but GetBaseException() called on AggregateException returns the same AggregateException.

We may want to fix the implementation and/or docs to make this more sensible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, practically, are you @jkotas addressing this in the documentation?

@gewarren gewarren merged commit c069a8c into dotnet:main Oct 24, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Runtime community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants