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

Resolve CA2201: No Reserved Exceptions #47873

Merged
merged 22 commits into from
May 3, 2023

Conversation

dahlbyk
Copy link
Contributor

@dahlbyk dahlbyk commented Apr 25, 2023

Resolve CA2201: No Reserved Exceptions

Description

Addresses initial list from @danmoseley all the CA2201 errors in published projects.

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Apr 25, 2023
@ghost
Copy link

ghost commented Apr 25, 2023

Thanks for your PR, @dahlbyk. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@BrennanConroy
Copy link
Member

Wondering if there's an easy way to turn CA2201 on just for production projects?

You add it here https://github.com/dotnet/aspnetcore/blob/main/.editorconfig#L80
And then also here with lower severity for tests/samples/etc. https://github.com/dotnet/aspnetcore/blob/main/.editorconfig#L319

@dahlbyk dahlbyk force-pushed the 47708-no-System-Exception branch from 4bdd3bf to 095dacc Compare April 29, 2023 19:09
@dahlbyk dahlbyk requested a review from a team as a code owner April 29, 2023 19:09
@dahlbyk
Copy link
Contributor Author

dahlbyk commented Apr 29, 2023

Change the new exception name not to conflict. AuthenticationFailureException?

Renamed and tried to fix failing tests.

dahlbyk added 2 commits April 29, 2023 23:44
All but src/Components/benchmarkapps covered by perf
@dahlbyk dahlbyk force-pushed the 47708-no-System-Exception branch from 095dacc to 4222688 Compare April 30, 2023 05:06
@dahlbyk dahlbyk requested review from a team and javiercn as code owners April 30, 2023 05:06
@dahlbyk
Copy link
Contributor Author

dahlbyk commented Apr 30, 2023

Ended up just finishing the job and marking CA2201 as an error for production projects. Glad to split into multiple PRs if that's preferred - each commit is meant to be atomic.

Co-authored-by: Chris Ross <Tratcher@Outlook.com>
@@ -33,7 +33,7 @@ public ConditionCollection(LogicalGrouping grouping, bool trackAllCaptures)
{
return _conditions[index];
}
throw new IndexOutOfRangeException($"Cannot access condition at index {index}. Only {_conditions.Count} conditions were captured.");
throw new ArgumentOutOfRangeException(nameof(index), $"Cannot access condition at index {index}. Only {_conditions.Count} conditions were captured.");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is another spot where the custom message is good enough that paramName: null might be preferred.

Suggested change
throw new ArgumentOutOfRangeException(nameof(index), $"Cannot access condition at index {index}. Only {_conditions.Count} conditions were captured.");
throw new ArgumentOutOfRangeException(null, $"Cannot access condition at index {index}. Only {_conditions.Count} conditions were captured.");

Copy link
Member

Choose a reason for hiding this comment

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

I agree that the existing custom message was good here, but how does passing in a null parameter name help? The custom message still gets used with nameof(index), right?

Copy link
Contributor Author

@dahlbyk dahlbyk May 3, 2023

Choose a reason for hiding this comment

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

how does passing in a null parameter name help?

Non-null/empty paramName is appended to the Message, which adds nothing to this message.

I went ahead and switched to null.

Copy link
Member

@halter73 halter73 May 3, 2023

Choose a reason for hiding this comment

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

I missed that you changed this before approving. Can you change it back? I think the slight redundancy in the error message is worth keeping ArgumentOutOfRangeException.ParamName non-null. Can we change our ArgumentOutOfRange exceptions to pass in the actualValue where it makes sense too?

Edit: Never mind. We can do this as a follow up if anyone actually cares.

src/SignalR/common/Shared/ClientResultsManager.cs Outdated Show resolved Hide resolved
src/SignalR/server/Core/src/StreamTracker.cs Outdated Show resolved Hide resolved
@dahlbyk dahlbyk changed the title Fewer System.Exception Resolve CA2201: No Reserved Exceptions Apr 30, 2023
Copy link
Member

@halter73 halter73 left a comment

Choose a reason for hiding this comment

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

Looking good! The API proposal for AuthenticationFailureException was also approved.

src/SignalR/common/Shared/ClientResultsManager.cs Outdated Show resolved Hide resolved
src/SignalR/server/Core/src/StreamTracker.cs Outdated Show resolved Hide resolved
.editorconfig Outdated
@@ -240,6 +240,9 @@ dotnet_diagnostic.CA2016.severity = warning
# CA2200: Rethrow to preserve stack details
dotnet_diagnostic.CA2200.severity = warning

# CA2201: Do not raise reserved exception types
dotnet_diagnostic.CA2201.severity = error
Copy link
Member

Choose a reason for hiding this comment

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

Do we really need to enable this for our test projects? Isn't the addition on line 394 enough? Conversely, if we really do want to enable this for test, do we need line 394?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we really need to enable this for our test projects? Isn't the addition on line 394 enough? Conversely, if we really do want to enable this for test, do we need line 394?

This line promotes to warning for all projects, then line 394 demotes back to suggestion for test/perf projects.

@@ -33,7 +33,7 @@ public ConditionCollection(LogicalGrouping grouping, bool trackAllCaptures)
{
return _conditions[index];
}
throw new IndexOutOfRangeException($"Cannot access condition at index {index}. Only {_conditions.Count} conditions were captured.");
throw new ArgumentOutOfRangeException(nameof(index), $"Cannot access condition at index {index}. Only {_conditions.Count} conditions were captured.");
Copy link
Member

Choose a reason for hiding this comment

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

I agree that the existing custom message was good here, but how does passing in a null parameter name help? The custom message still gets used with nameof(index), right?

.editorconfig Outdated Show resolved Hide resolved
src/SignalR/common/Shared/ClientResultsManager.cs Outdated Show resolved Hide resolved
Copy link
Contributor Author

@dahlbyk dahlbyk left a comment

Choose a reason for hiding this comment

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

I believe all feedback has been addressed by the last few commits. Should I rebase onto main?

.editorconfig Outdated
@@ -240,6 +240,9 @@ dotnet_diagnostic.CA2016.severity = warning
# CA2200: Rethrow to preserve stack details
dotnet_diagnostic.CA2200.severity = warning

# CA2201: Do not raise reserved exception types
dotnet_diagnostic.CA2201.severity = error
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we really need to enable this for our test projects? Isn't the addition on line 394 enough? Conversely, if we really do want to enable this for test, do we need line 394?

This line promotes to warning for all projects, then line 394 demotes back to suggestion for test/perf projects.

Copy link
Member

@halter73 halter73 left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@halter73 halter73 merged commit 0c8a569 into dotnet:main May 3, 2023
@ghost ghost added this to the 8.0-preview5 milestone May 3, 2023
@dahlbyk dahlbyk deleted the 47708-no-System-Exception branch May 3, 2023 17:40
@dahlbyk
Copy link
Contributor Author

dahlbyk commented May 3, 2023

I noticed GitHub didn't add a reference from that issue to this PR or #47708. Might be nice if a collaborator could add a note there about AuthenticationFailureException.

@ghost
Copy link

ghost commented May 3, 2023

Hi @dahlbyk. It looks like you just commented on a closed PR. The team will most probably miss it. If you'd like to bring something important up to their attention, consider filing a new issue and add enough details to build context.

@Tratcher Tratcher linked an issue May 3, 2023 that may be closed by this pull request
1 task
@amcasey amcasey added area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlesware area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Jun 6, 2023
@asos-jasperdeluna asos-jasperdeluna mentioned this pull request Nov 4, 2024
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlesware area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions 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.

Avoid CA2201 (Generic Exception) Violations Custom RemoteAuthenticationHandler Exceptions
7 participants