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

Use ArgumentException.ThrowIfNullOrWhitespace #48414

Closed

Conversation

wcontayon
Copy link
Contributor

Use ArgumentException.ThrowIfNullOrWhitespace

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Description

Use ArgumentException.ThrowIfNullOrWhitespace method from dotnet/runtime#86007
Fixes #48185 (partially, still check if we can replace some code with ArgumentException.ThrowIfNullOrWhitespace)

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-identity Includes: Identity and providers label May 24, 2023
@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label May 24, 2023
@ghost
Copy link

ghost commented May 24, 2023

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

@wcontayon wcontayon marked this pull request as ready for review May 27, 2023 01:57
{
throw new ArgumentException("A valid non-empty content root must be provided.", nameof(contentRootPath));
}
ArgumentException.ThrowIfNullOrEmpty(contentRootPath);
Copy link
Member

Choose a reason for hiding this comment

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

Definitely less explanatory

{
throw new ArgumentException("Cannot be null or empty", nameof(key));
}
ArgumentException.ThrowIfNullOrEmpty(purpose);
Copy link
Member

Choose a reason for hiding this comment

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

Observable change - not saying good or bad, but: definitely a change; I'm fine with it personally

Copy link
Member

Choose a reason for hiding this comment

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

I'm missing what observable here?

Copy link
Member

@mgravell mgravell May 27, 2023

Choose a reason for hiding this comment

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

The specific error message will change. Very much a pedantic "we should at least be aware that we're changing something, even if it is something that nobody should reasonably rely on", and in this case very much in the "trivial, no custom messages, just go for it" category. New message "The value cannot be an empty string." or "Value cannot be null"

Copy link
Contributor Author

@wcontayon wcontayon May 27, 2023

Choose a reason for hiding this comment

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

Effectively, we do not have an overload that accepts a custom message.
I opened a Api Proposal on dotnet/runtime to have the option to set the custom message.
we can just update only those part with generic message

Copy link
Member

Choose a reason for hiding this comment

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

Ah, right. By precedent, I think we've had no problem changing exception messages, including in cases like this, so long as that doesn't lose actual useful information.

{
throw new ArgumentException("A valid non-empty content root must be provided.", nameof(contentRootPath));
}
ArgumentException.ThrowIfNullOrEmpty(contentRootPath);
Copy link
Member

Choose a reason for hiding this comment

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

Ditto less explanatory

@mgravell
Copy link
Member

mgravell commented May 27, 2023

Not going to comment on each and every one of these, but I think there are 3 main scenarios in here:

  • trivial, no custom messages, just go for it
  • vague custom message; observable change, but... not a problem (and probably desirable for consistency)
  • humanized, context-specific message, that is erased here

The first two categories: IMO absolutely fine, let's encourage that. It is this final category that worries me a little - some of the existing message IMO cross an invisible and subjective line where the current message is more helpful than the new.

Not at a PC to check, but: I assume there isn't an overload that accepts a custom message?

Clarification: I mean this comment as a discussion starter, not as a definitive "this is the review". So to all: thoughts?

@mkArtakMSFT mkArtakMSFT added area-runtime and removed area-identity Includes: Identity and providers labels May 30, 2023
@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
@@ -13,7 +13,11 @@ internal static void Initialize(this IHostingEnvironment hostingEnvironment, str
#pragma warning restore CS0618 // Type or member is obsolete
{
ArgumentNullException.ThrowIfNull(options);
ArgumentException.ThrowIfNullOrEmpty(contentRootPath);

if (string.IsNullOrEmpty(contentRootPath))
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
if (string.IsNullOrEmpty(contentRootPath))
if (string.IsNullOrEmpty(contentRootPath))

Co-authored-by: Dan Moseley <danmose@microsoft.com>
{
throw new ArgumentException("Value cannot be null or empty string.", nameof(value));
}
ArgumentException.ThrowIfNullOrEmpty(value, nameof(value));
Copy link
Member

Choose a reason for hiding this comment

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

the 2nd parameter is required here?

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
ArgumentException.ThrowIfNullOrEmpty(value, nameof(value));
ArgumentException.ThrowIfNullOrEmpty(value);

{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(folderPath));
}
ArgumentException.ThrowIfNullOrEmpty(folderPath, nameof(folderPath));
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
ArgumentException.ThrowIfNullOrEmpty(folderPath, nameof(folderPath));
ArgumentException.ThrowIfNullOrEmpty(folderPath);

throw new ArgumentException($"The {nameof(options.RootPath)} property " +
$"of {nameof(options)} cannot be null or empty.");
}
ArgumentException.ThrowIfNullOrEmpty(options.RootPath, nameof(options.RootPath));
Copy link
Member

Choose a reason for hiding this comment

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

does this require the 2nd parameter to get it right?

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
ArgumentException.ThrowIfNullOrEmpty(options.RootPath, nameof(options.RootPath));
ArgumentException.ThrowIfNullOrEmpty(options.RootPath);

{
throw new ArgumentException("Cannot be null or empty", nameof(scriptName));
}
ArgumentException.ThrowIfNullOrEmpty(sourcePath, nameof(spaBuilder));
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
ArgumentException.ThrowIfNullOrEmpty(sourcePath, nameof(spaBuilder));
ArgumentException.ThrowIfNullOrEmpty(sourcePath);

better I think

@@ -902,8 +880,6 @@ public static RoutePatternParameterPart ParameterPart(string parameterName, obje
throw new ArgumentException(Resources.TemplateRoute_OptionalCannotHaveDefaultValue);
}

ArgumentNullException.ThrowIfNull(parameterPolicies);
Copy link
Member

Choose a reason for hiding this comment

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

were these intentionally moved up?

{
throw new ArgumentNullException(nameof(name));
}
ArgumentNullException.ThrowIfNull(name, parameterName);
Copy link
Member

Choose a reason for hiding this comment

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

This is covered by the line below, and the implicit equals operator on the StringSegment struct, I think? Anyway in other cases you don't have this line.

Suggested change
ArgumentNullException.ThrowIfNull(name, parameterName);

@danmoseley
Copy link
Member

Just a few bits of feedback. Have you verified in the debugger that it's working as expected on StringSegment? Normally you can't compare structs to null but I guess it's using the implicit equality operator.

BTW, there's still 6 uses of Resources.ArgumentCannotBeNullOrEmpty in the tree. Do you want to remove those, and then remove the string as well?

{
throw new ArgumentNullException(nameof(value));
}
ArgumentException.ThrowIfNullOrWhiteSpace(value.Value);
Copy link
Member

Choose a reason for hiding this comment

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

Behavior change, Why is this more restrictive than before? This might explain most of the auth test failures, an empty value is commonly used when removing a cookie.

@ghost
Copy link

ghost commented Jun 20, 2023

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no breaking changes are introduced, please leave an /azp run comment here to rerun the CI pipeline and confirm success before merging the change.

@ghost ghost added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Jun 20, 2023
@wcontayon
Copy link
Contributor Author

/azp run

@azure-pipelines
Copy link

Commenter does not have sufficient privileges for PR 48414 in repo dotnet/aspnetcore

@danmoseley
Copy link
Member

@wcontayon To rerun validation you have to click close then click open again..

@danmoseley danmoseley closed this Jun 24, 2023
@danmoseley danmoseley reopened this Jun 24, 2023
@danmoseley
Copy link
Member

#48414 (comment)

@mkArtakMSFT this comment seems to only work for folks with write access -- should we clarify the text that other folks should close/reopen? (my assumption is that it runs on latest HEAD either way @wtgodbe ?)

@danmoseley
Copy link
Member

meanwhile, @wcontayon did you have a chance to address the feedback? You can click resolve when you have.
If it makes things easier, your option, you could split the PR up.

@wtgodbe
Copy link
Member

wtgodbe commented Jun 26, 2023

my assumption is that it runs on latest HEAD either way @wtgodbe ?

That's right

@danmoseley
Copy link
Member

@wcontayon do you expect to be able to continue addressing feedback? thanks for doing this. BTW, if it's easier for you, you could choose to split it into smaller PR's.

@wcontayon
Copy link
Contributor Author

@wcontayon do you expect to be able to continue addressing feedback? thanks for doing this. BTW, if it's easier for you, you could choose to split it into smaller PR's.

@danmoseley yes sure !!
I’ll try to push the update by the end of next week 🙂

@danmoseley
Copy link
Member

Cool! Work on your own schedule, just want to make sure we end up merging your work.

@danmoseley
Copy link
Member

@wcontayon do you plan to continue with this PR? I'll close it for now, but feel free to reopen when you're ready.

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 pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use ArgumentException.ThrowIfNullOrWhitespace
9 participants