-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Throw.cs
104 lines (97 loc) · 4.76 KB
/
Throw.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for
// license information.
namespace Microsoft.Azure.Search.Common
{
using System;
/// <summary>
/// Defines utility methods for validating arguments.
/// </summary>
public static class Throw
{
/// <summary>
/// Throws ArgumentException with the given parameter name and optional message if the given Boolean
/// value is true.
/// </summary>
/// <param name="isInvalid">The flag to test. This method throws if it's true and does nothing if
/// it's false.</param>
/// <param name="paramName">The name of the parameter being validated. This is passed to the
/// ArgumentException constructor.</param>
/// <param name="message">An optional error message to include in the ArgumentException. The default
/// message is "Invalid argument."</param>
public static void IfArgument(bool isInvalid, string paramName, string message = null)
{
if (isInvalid)
{
message = message ?? "Invalid argument.";
throw new ArgumentException(message, paramName);
}
}
/// <summary>
/// Throws ArgumentOutOfRangeException with the given parameter name and optional message if the given Boolean
/// value is true.
/// </summary>
/// <param name="isInvalid">The flag to test. This method throws if it's true and does nothing if
/// it's false.</param>
/// <param name="paramName">The name of the parameter being validated. This is passed to the
/// ArgumentOutOfRangeException constructor.</param>
/// <param name="message">An optional error message to include in the ArgumentOutOfRangeException. The default
/// message is "Argument out of range."</param>
public static void IfArgumentOutOfRange(bool isInvalid, string paramName, string message = null)
{
if (isInvalid)
{
message = message ?? "Argument out of range.";
throw new ArgumentOutOfRangeException(paramName, message);
}
}
/// <summary>
/// Throws ArgumentNullException with the given parameter name and optional message if the given
/// reference is null.
/// </summary>
/// <typeparam name="T">The type of value to test. Must be a reference type.</typeparam>
/// <param name="value">The reference to test for null.</param>
/// <param name="paramName">The name of the parameter being validated. This is passed to the
/// ArgumentNullException constructor.</param>
/// <param name="message">An optional error message to include in the ArgumentNullException.</param>
public static void IfArgumentNull<T>(T value, string paramName, string message = null) where T : class
{
if (value == null)
{
if (message == null)
{
throw new ArgumentNullException(paramName);
}
else
{
throw new ArgumentNullException(paramName, message);
}
}
}
/// <summary>
/// Throws ArgumentNullException or ArgumentException with the given parameter name and optional message
/// if the given string is null or empty, respectively.
/// </summary>
/// <param name="value">The string to test for null or empty.</param>
/// <param name="paramName">The name of the parameter being validated. This is passed to the
/// ArgumentNullException or ArgumentException constructor.</param>
/// <param name="message">An optional error message to include in the ArgumentNullException
/// or ArgumentException.</param>
public static void IfArgumentNullOrEmpty(string value, string paramName, string message = null)
{
IfArgumentNull(value, paramName, message);
message = message ?? "Argument cannot be an empty string.";
IfArgument(value.Length == 0, paramName, message);
}
/// <summary>
/// Throws ArgumentNullException or ArgumentException with a pre-determined message if the given search
/// service name is null or empty, respectively.
/// </summary>
/// <param name="searchServiceName">The search service name to validate.</param>
public static void IfNullOrEmptySearchServiceName(string searchServiceName) =>
IfArgumentNullOrEmpty(
searchServiceName,
nameof(searchServiceName),
"Invalid search service name. Name cannot be null or an empty string.");
}
}