Skip to content

Commit 6372a53

Browse files
authored
Use ArgumentException.ThrowIfNullOrEmpty and ThrowIfNullOrWhitespace (#52897)
1 parent 15a4a77 commit 6372a53

File tree

93 files changed

+450
-872
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+450
-872
lines changed

src/Caching/SqlServer/src/Microsoft.Extensions.Caching.SqlServer.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
<ItemGroup>
2626
<Compile Include="$(SharedSourceRoot)ThrowHelpers\ArgumentNullThrowHelper.cs" LinkBase="Shared" />
27+
<Compile Include="$(SharedSourceRoot)ThrowHelpers\ArgumentThrowHelper.cs" LinkBase="Shared" />
2728
<Compile Include="$(SharedSourceRoot)CallerArgument\CallerArgumentExpressionAttribute.cs" LinkBase="Shared" />
2829
</ItemGroup>
2930

src/Caching/SqlServer/src/SqlServerCache.cs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,10 @@ public SqlServerCache(IOptions<SqlServerCacheOptions> options)
3535
{
3636
var cacheOptions = options.Value;
3737

38-
if (string.IsNullOrEmpty(cacheOptions.ConnectionString))
39-
{
40-
throw new ArgumentException(
41-
$"{nameof(SqlServerCacheOptions.ConnectionString)} cannot be empty or null.");
42-
}
43-
if (string.IsNullOrEmpty(cacheOptions.SchemaName))
44-
{
45-
throw new ArgumentException(
46-
$"{nameof(SqlServerCacheOptions.SchemaName)} cannot be empty or null.");
47-
}
48-
if (string.IsNullOrEmpty(cacheOptions.TableName))
49-
{
50-
throw new ArgumentException(
51-
$"{nameof(SqlServerCacheOptions.TableName)} cannot be empty or null.");
52-
}
38+
ArgumentThrowHelper.ThrowIfNullOrEmpty(cacheOptions.ConnectionString);
39+
ArgumentThrowHelper.ThrowIfNullOrEmpty(cacheOptions.SchemaName);
40+
ArgumentThrowHelper.ThrowIfNullOrEmpty(cacheOptions.TableName);
41+
5342
if (cacheOptions.ExpiredItemsDeletionInterval.HasValue &&
5443
cacheOptions.ExpiredItemsDeletionInterval.Value < MinimumExpiredItemsDeletionInterval)
5544
{

src/Components/Components/src/Routing/Resources.resx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,6 @@
159159
<data name="ArgumentMustBeGreaterThanOrEqualTo" xml:space="preserve">
160160
<value>Value must be greater than or equal to {0}.</value>
161161
</data>
162-
<data name="Argument_NullOrEmpty" xml:space="preserve">
163-
<value>Value cannot be null or empty.</value>
164-
</data>
165162
<data name="TemplateRoute_CannotHaveCatchAllInMultiSegment" xml:space="preserve">
166163
<value>A path segment that contains more than one section, such as a literal section or a parameter, cannot contain a catch-all parameter.</value>
167164
</data>

src/Components/Server/src/ProtectedBrowserStorage/ProtectedBrowserStorage.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ private protected ProtectedBrowserStorage(string storeName, IJSRuntime jsRuntime
3333
throw new PlatformNotSupportedException($"{GetType()} cannot be used when running in a browser.");
3434
}
3535

36-
if (string.IsNullOrEmpty(storeName))
37-
{
38-
throw new ArgumentException("The value cannot be null or empty", nameof(storeName));
39-
}
36+
ArgumentException.ThrowIfNullOrEmpty(storeName);
4037

4138
_storeName = storeName;
4239
_jsRuntime = jsRuntime ?? throw new ArgumentNullException(nameof(jsRuntime));
@@ -71,15 +68,8 @@ public ValueTask SetAsync(string key, object value)
7168
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
7269
public ValueTask SetAsync(string purpose, string key, object value)
7370
{
74-
if (string.IsNullOrEmpty(purpose))
75-
{
76-
throw new ArgumentException("Cannot be null or empty", nameof(purpose));
77-
}
78-
79-
if (string.IsNullOrEmpty(key))
80-
{
81-
throw new ArgumentException("Cannot be null or empty", nameof(key));
82-
}
71+
ArgumentException.ThrowIfNullOrEmpty(purpose);
72+
ArgumentException.ThrowIfNullOrEmpty(key);
8373

8474
return SetProtectedJsonAsync(key, Protect(purpose, value));
8575
}

src/Components/Web/src/JSComponents/JSComponentConfigurationStore.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ internal void Add([DynamicallyAccessedMembers(LinkerFlags.Component)] Type compo
5656
{
5757
Add(componentType, identifier);
5858

59-
if (string.IsNullOrEmpty(javaScriptInitializer))
60-
{
61-
throw new ArgumentException($"'{nameof(javaScriptInitializer)}' cannot be null or empty.", nameof(javaScriptInitializer));
62-
}
59+
ArgumentException.ThrowIfNullOrEmpty(javaScriptInitializer);
6360

6461
// Since it has a JS initializer, prepare the metadata we'll supply to JS code
6562
if (!JSComponentIdentifiersByInitializer.TryGetValue(javaScriptInitializer, out var identifiersForInitializer))

src/FileProviders/Embedded/src/Manifest/ManifestDirectory.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,7 @@ public override ManifestEntry Traverse(StringSegment segment)
4747

4848
public static ManifestDirectory CreateDirectory(string name, ManifestEntry[] children)
4949
{
50-
if (string.IsNullOrWhiteSpace(name))
51-
{
52-
throw new ArgumentException($"'{nameof(name)}' must not be null, empty or whitespace.", nameof(name));
53-
}
54-
50+
ArgumentThrowHelper.ThrowIfNullOrWhiteSpace(name);
5551
ArgumentNullThrowHelper.ThrowIfNull(children);
5652

5753
var result = new ManifestDirectory(name, children);

src/FileProviders/Embedded/src/Manifest/ManifestFile.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using Microsoft.AspNetCore.Shared;
56
using Microsoft.Extensions.Primitives;
67

78
namespace Microsoft.Extensions.FileProviders.Embedded.Manifest;
@@ -11,15 +12,8 @@ internal sealed class ManifestFile : ManifestEntry
1112
public ManifestFile(string name, string resourcePath)
1213
: base(name)
1314
{
14-
if (string.IsNullOrWhiteSpace(name))
15-
{
16-
throw new ArgumentException($"'{nameof(name)}' must not be null, empty or whitespace.", nameof(name));
17-
}
18-
19-
if (string.IsNullOrWhiteSpace(resourcePath))
20-
{
21-
throw new ArgumentException($"'{nameof(resourcePath)}' must not be null, empty or whitespace.", nameof(resourcePath));
22-
}
15+
ArgumentThrowHelper.ThrowIfNullOrWhiteSpace(name);
16+
ArgumentThrowHelper.ThrowIfNullOrWhiteSpace(resourcePath);
2317

2418
ResourcePath = resourcePath;
2519
}

src/FileProviders/Embedded/src/Microsoft.Extensions.FileProviders.Embedded.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
<ItemGroup>
4343
<Compile Include="$(SharedSourceRoot)ThrowHelpers\ArgumentNullThrowHelper.cs" LinkBase="Shared" />
44+
<Compile Include="$(SharedSourceRoot)ThrowHelpers\ArgumentThrowHelper.cs" LinkBase="Shared" />
4445
<Compile Include="$(SharedSourceRoot)CallerArgument\CallerArgumentExpressionAttribute.cs" LinkBase="Shared" />
4546
<Compile Include="$(SharedSourceRoot)TrimmingAttributes.cs" LinkBase="Shared"
4647
Condition="'$(TargetFramework)' != '$(DefaultNetCoreTargetFramework)'" />

src/Hosting/Hosting/src/Internal/HostingEnvironmentExtensions.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ internal static void Initialize(this IHostingEnvironment hostingEnvironment, str
1313
#pragma warning restore CS0618 // Type or member is obsolete
1414
{
1515
ArgumentNullException.ThrowIfNull(options);
16-
if (string.IsNullOrEmpty(contentRootPath))
17-
{
18-
throw new ArgumentException("A valid non-empty content root must be provided.", nameof(contentRootPath));
19-
}
16+
ArgumentException.ThrowIfNullOrEmpty(contentRootPath);
2017
if (!Directory.Exists(contentRootPath))
2118
{
2219
throw new ArgumentException($"The content root '{contentRootPath}' does not exist.", nameof(contentRootPath));
@@ -67,10 +64,7 @@ internal static void Initialize(
6764
IHostEnvironment? baseEnvironment = null)
6865
{
6966
ArgumentNullException.ThrowIfNull(options);
70-
if (string.IsNullOrEmpty(contentRootPath))
71-
{
72-
throw new ArgumentException("A valid non-empty content root must be provided.", nameof(contentRootPath));
73-
}
67+
ArgumentException.ThrowIfNullOrEmpty(contentRootPath);
7468
if (!Directory.Exists(contentRootPath))
7569
{
7670
throw new ArgumentException($"The content root '{contentRootPath}' does not exist.", nameof(contentRootPath));

src/Hosting/Server.IntegrationTesting/src/Common/DeploymentParameters.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ public DeploymentParameters(
5252
RuntimeFlavor runtimeFlavor,
5353
RuntimeArchitecture runtimeArchitecture)
5454
{
55-
if (string.IsNullOrEmpty(applicationPath))
56-
{
57-
throw new ArgumentException("Value cannot be null.", nameof(applicationPath));
58-
}
55+
ArgumentException.ThrowIfNullOrEmpty(applicationPath);
5956

6057
if (!Directory.Exists(applicationPath))
6158
{

0 commit comments

Comments
 (0)