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