-
Notifications
You must be signed in to change notification settings - Fork 10k
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
Changes from all commits
2ba2b7c
c536275
931d8a5
2d63c6e
32ceb03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -13,10 +13,12 @@ internal static void Initialize(this IHostingEnvironment hostingEnvironment, str | |||||
#pragma warning restore CS0618 // Type or member is obsolete | ||||||
{ | ||||||
ArgumentNullException.ThrowIfNull(options); | ||||||
if (string.IsNullOrEmpty(contentRootPath)) | ||||||
|
||||||
if (string.IsNullOrEmpty(contentRootPath)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
throw new ArgumentException("A valid non-empty content root must be provided.", nameof(contentRootPath)); | ||||||
} | ||||||
|
||||||
if (!Directory.Exists(contentRootPath)) | ||||||
{ | ||||||
throw new ArgumentException($"The content root '{contentRootPath}' does not exist.", nameof(contentRootPath)); | ||||||
|
@@ -67,10 +69,12 @@ internal static void Initialize( | |||||
IHostEnvironment? baseEnvironment = null) | ||||||
{ | ||||||
ArgumentNullException.ThrowIfNull(options); | ||||||
|
||||||
if (string.IsNullOrEmpty(contentRootPath)) | ||||||
{ | ||||||
throw new ArgumentException("A valid non-empty content root must be provided.", nameof(contentRootPath)); | ||||||
} | ||||||
|
||||||
if (!Directory.Exists(contentRootPath)) | ||||||
{ | ||||||
throw new ArgumentException($"The content root '{contentRootPath}' does not exist.", nameof(contentRootPath)); | ||||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -164,10 +164,8 @@ public static bool TryParseStrictList(IList<string>? inputs, [NotNullWhen(true)] | |||
|
||||
internal static void CheckNameFormat(StringSegment name, string parameterName) | ||||
{ | ||||
if (name == null) | ||||
{ | ||||
throw new ArgumentNullException(nameof(name)); | ||||
} | ||||
ArgumentNullException.ThrowIfNull(name, parameterName); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||
ArgumentException.ThrowIfNullOrEmpty(name.Value, parameterName); | ||||
|
||||
if (HttpRuleParser.GetTokenLength(name, 0) != name.Length) | ||||
{ | ||||
|
@@ -177,10 +175,7 @@ internal static void CheckNameFormat(StringSegment name, string parameterName) | |||
|
||||
internal static void CheckValueFormat(StringSegment value, string parameterName) | ||||
{ | ||||
if (value == null) | ||||
{ | ||||
throw new ArgumentNullException(nameof(value)); | ||||
} | ||||
ArgumentException.ThrowIfNullOrWhiteSpace(value.Value); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||||
|
||||
var offset = 0; | ||||
var result = CookieHeaderParserShared.GetCookieValue(value, ref offset); | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -709,10 +709,7 @@ private static RoutePatternPathSegment SegmentCore(RoutePatternPart[] parts) | |
/// <returns>The <see cref="RoutePatternLiteralPart"/>.</returns> | ||
public static RoutePatternLiteralPart LiteralPart(string content) | ||
{ | ||
if (string.IsNullOrEmpty(content)) | ||
{ | ||
throw new ArgumentException(Resources.Argument_NullOrEmpty, nameof(content)); | ||
} | ||
ArgumentException.ThrowIfNullOrEmpty(content); | ||
|
||
if (content.Contains('?')) | ||
{ | ||
|
@@ -735,11 +732,7 @@ private static RoutePatternLiteralPart LiteralPartCore(string content) | |
/// <returns>The <see cref="RoutePatternSeparatorPart"/>.</returns> | ||
public static RoutePatternSeparatorPart SeparatorPart(string content) | ||
{ | ||
if (string.IsNullOrEmpty(content)) | ||
{ | ||
throw new ArgumentException(Resources.Argument_NullOrEmpty, nameof(content)); | ||
} | ||
|
||
ArgumentException.ThrowIfNullOrEmpty(content); | ||
return SeparatorPartCore(content); | ||
} | ||
|
||
|
@@ -755,10 +748,7 @@ private static RoutePatternSeparatorPart SeparatorPartCore(string content) | |
/// <returns>The <see cref="RoutePatternParameterPart"/>.</returns> | ||
public static RoutePatternParameterPart ParameterPart(string parameterName) | ||
{ | ||
if (string.IsNullOrEmpty(parameterName)) | ||
{ | ||
throw new ArgumentException(Resources.Argument_NullOrEmpty, nameof(parameterName)); | ||
} | ||
ArgumentException.ThrowIfNullOrEmpty(parameterName); | ||
|
||
if (parameterName.AsSpan().IndexOfAny(RoutePatternParser.InvalidParameterNameChars) >= 0) | ||
{ | ||
|
@@ -781,10 +771,7 @@ public static RoutePatternParameterPart ParameterPart(string parameterName) | |
/// <returns>The <see cref="RoutePatternParameterPart"/>.</returns> | ||
public static RoutePatternParameterPart ParameterPart(string parameterName, object @default) | ||
{ | ||
if (string.IsNullOrEmpty(parameterName)) | ||
{ | ||
throw new ArgumentException(Resources.Argument_NullOrEmpty, nameof(parameterName)); | ||
} | ||
ArgumentException.ThrowIfNullOrEmpty(parameterName); | ||
|
||
if (parameterName.AsSpan().IndexOfAny(RoutePatternParser.InvalidParameterNameChars) >= 0) | ||
{ | ||
|
@@ -811,10 +798,7 @@ public static RoutePatternParameterPart ParameterPart( | |
object? @default, | ||
RoutePatternParameterKind parameterKind) | ||
{ | ||
if (string.IsNullOrEmpty(parameterName)) | ||
{ | ||
throw new ArgumentException(Resources.Argument_NullOrEmpty, nameof(parameterName)); | ||
} | ||
ArgumentException.ThrowIfNullOrEmpty(parameterName); | ||
|
||
if (parameterName.AsSpan().IndexOfAny(RoutePatternParser.InvalidParameterNameChars) >= 0) | ||
{ | ||
|
@@ -848,10 +832,8 @@ public static RoutePatternParameterPart ParameterPart( | |
RoutePatternParameterKind parameterKind, | ||
IEnumerable<RoutePatternParameterPolicyReference> parameterPolicies) | ||
{ | ||
if (string.IsNullOrEmpty(parameterName)) | ||
{ | ||
throw new ArgumentException(Resources.Argument_NullOrEmpty, nameof(parameterName)); | ||
} | ||
ArgumentException.ThrowIfNullOrEmpty(parameterName); | ||
ArgumentNullException.ThrowIfNull(parameterPolicies); | ||
|
||
if (parameterName.AsSpan().IndexOfAny(RoutePatternParser.InvalidParameterNameChars) >= 0) | ||
{ | ||
|
@@ -863,8 +845,6 @@ public static RoutePatternParameterPart ParameterPart( | |
throw new ArgumentException(Resources.TemplateRoute_OptionalCannotHaveDefaultValue); | ||
} | ||
|
||
ArgumentNullException.ThrowIfNull(parameterPolicies); | ||
|
||
return ParameterPartCore( | ||
parameterName: parameterName, | ||
@default: @default, | ||
|
@@ -887,10 +867,8 @@ public static RoutePatternParameterPart ParameterPart( | |
RoutePatternParameterKind parameterKind, | ||
params RoutePatternParameterPolicyReference[] parameterPolicies) | ||
{ | ||
if (string.IsNullOrEmpty(parameterName)) | ||
{ | ||
throw new ArgumentException(Resources.Argument_NullOrEmpty, nameof(parameterName)); | ||
} | ||
ArgumentException.ThrowIfNullOrEmpty(parameterName); | ||
ArgumentNullException.ThrowIfNull(parameterPolicies); | ||
|
||
if (parameterName.AsSpan().IndexOfAny(RoutePatternParser.InvalidParameterNameChars) >= 0) | ||
{ | ||
|
@@ -902,8 +880,6 @@ public static RoutePatternParameterPart ParameterPart( | |
throw new ArgumentException(Resources.TemplateRoute_OptionalCannotHaveDefaultValue); | ||
} | ||
|
||
ArgumentNullException.ThrowIfNull(parameterPolicies); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. were these intentionally moved up? |
||
|
||
return ParameterPartCore( | ||
parameterName: parameterName, | ||
@default: @default, | ||
|
@@ -986,11 +962,7 @@ public static RoutePatternParameterPolicyReference Constraint(IRouteConstraint c | |
/// <returns>The <see cref="RoutePatternParameterPolicyReference"/>.</returns> | ||
public static RoutePatternParameterPolicyReference Constraint(string constraint) | ||
{ | ||
if (string.IsNullOrEmpty(constraint)) | ||
{ | ||
throw new ArgumentException(Resources.Argument_NullOrEmpty, nameof(constraint)); | ||
} | ||
|
||
ArgumentException.ThrowIfNullOrEmpty(constraint); | ||
return ParameterPolicyCore(constraint); | ||
} | ||
|
||
|
@@ -1017,11 +989,7 @@ public static RoutePatternParameterPolicyReference ParameterPolicy(IParameterPol | |
/// <returns>The <see cref="RoutePatternParameterPolicyReference"/>.</returns> | ||
public static RoutePatternParameterPolicyReference ParameterPolicy(string parameterPolicy) | ||
{ | ||
if (string.IsNullOrEmpty(parameterPolicy)) | ||
{ | ||
throw new ArgumentException(Resources.Argument_NullOrEmpty, nameof(parameterPolicy)); | ||
} | ||
|
||
ArgumentException.ThrowIfNullOrEmpty(parameterPolicy); | ||
return ParameterPolicyCore(parameterPolicy); | ||
} | ||
|
||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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"
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.