/// <summary> /// Guards against an argument being null or whitespace. Will throw an /// <see cref="ArgumentNullException">ArgumentNullException</see> if the argument is null. Will throw an /// <see cref="ArgumentException">ArgumentException</see> if the argument is whitespace. /// </summary> /// <param name="argumentValue">The argument value to guard.</param> /// <param name="argumentName"> /// (Optional) Name of the argument. If specified it will be included in the thrown exception /// and therefore make it more informative. /// </param> /// <param name="exceptionMessage"> /// (Optional) Custom error message. A specific error message that can be used to describe /// the exception in more detail than the default message. /// </param> /// <param name="additionalData">(Optional) Additional information to add to the Data property of the thrown exception.</param> /// <exception cref="ArgumentNullException">Will be thrown when <c>argumentValue</c> is <c>null</c> .</exception> /// <exception cref="ArgumentException">Will be thrown when <c>argumentValue</c> is whitespace.</exception> /// <example> /// <code> /// public void MyAmazingMethod(string myArgument) /// { /// GuardAgainst.ArgumentBeingNullOrWhitespace(myArgument, nameof(myArgument)); /// /// // Remaining code omitted. /// } /// </code> /// </example> public static void ArgumentBeingNullOrWhitespace(string argumentValue, string argumentName = null, string exceptionMessage = null, IDictionary <object, object> additionalData = default) { if (!string.IsNullOrWhiteSpace(argumentValue)) { return; } if (ReferenceEquals(argumentValue, null)) { var ex = new ArgumentNullException(argumentName.ToNullIfWhitespace(), exceptionMessage.ToNullIfWhitespace()); ex.AddData(additionalData); throw ex; } else { var ex = new ArgumentException(exceptionMessage.ToNullIfWhitespace(), argumentName.ToNullIfWhitespace()); ex.AddData(additionalData); throw ex; } }
/// <summary> /// Guards against an argument being null or less than the specified minimum or greater than the specified /// maximum. Will throw an <see cref="ArgumentNullException">ArgumentNullException</see> if the argument is null. Will /// throw an <see cref="ArgumentOutOfRangeException">ArgumentOutOfRangeException</see> if the argument is less than the /// specified minimum or greater than the specified maximum. /// </summary> /// <typeparam name="T">A reference type.</typeparam> /// <param name="argumentValue">The argument value to guard.</param> /// <param name="minimumAllowedValue">The minimum allowed value.</param> /// <param name="maximumAllowedValue">The maximum allowed value.</param> /// <param name="argumentName">Name of the argument. Can be optionally specified to be included in the raised exception.</param> /// <param name="exceptionMessage"> /// (Optional) Custom error message. A specific error message that can be used to describe /// the exception in more detail than the default message. /// </param> /// <param name="additionalData">(Optional) Additional information to add to the Data property of the thrown exception.</param> /// <exception cref="ArgumentNullException">Will be thrown when <c>argumentValue</c> is null.</exception> /// <exception cref="ArgumentOutOfRangeException"> /// Will be thrown when <c>argumentValue</c> is less than the specified /// minimum or greater than the specified maximum. /// </exception> /// <example> /// <code> /// public void MyAmazingMethod(string myArgument, DateTime dob) /// { /// GuardAgainst.ArgumentBeingNullOrOutOfRange(myArgument, "A", "Z", nameof(myArgument)); /// GuardAgainst.ArgumentBeingNullOrOutOfRange(dob, yearTwoThousand, DateTime.Now, nameof(dob)); /// /// // Remaining code omitted. /// } /// </code> /// </example> public static void ArgumentBeingNullOrOutOfRange <T>(T argumentValue, T minimumAllowedValue, T maximumAllowedValue, string argumentName = null, string exceptionMessage = null, IDictionary <object, object> additionalData = default) where T : class, IComparable <T> { if (ReferenceEquals(argumentValue, null)) { var ex = new ArgumentNullException(argumentName.ToNullIfWhitespace(), exceptionMessage.ToNullIfWhitespace()); ex.AddData(additionalData); throw ex; } if (!argumentValue.IsInRange(minimumAllowedValue, maximumAllowedValue)) { var ex = new ArgumentOutOfRangeException(argumentName.ToNullIfWhitespace(), argumentValue, exceptionMessage.ToNullIfWhitespace()); ex.AddData(additionalData); throw ex; } }
/// <summary> /// Guards against an argument being null. Will throw an /// <see cref="ArgumentNullException">ArgumentNullException</see> if the argument is null. /// </summary> /// <param name="argumentValue">The argument value to guard.</param> /// <param name="argumentName"> /// (Optional) Name of the argument. If specified it will be included in the thrown exception /// and therefore make it more informative. /// </param> /// <param name="exceptionMessage"> /// (Optional) Custom error message. A specific error message that can be used to describe /// the exception in more detail than the default message. /// </param> /// <param name="additionalData">(Optional) Additional information to add to the Data property of the thrown exception.</param> /// <exception cref="ArgumentNullException">Will be thrown when <c>argumentValue</c> is <c>null</c> .</exception> /// <example> /// <code> /// public void MyAmazingMethod(string myArgument, Person person) /// { /// GuardAgainst.ArgumentBeingNull(myArgument, nameof(myArgument)); /// GuardAgainst.ArgumentBeingNull(person, nameof(person)); /// /// // Remaining code omitted. /// } /// </code> /// </example> public static void ArgumentBeingNull <T>(T argumentValue, string argumentName = null, string exceptionMessage = null, IDictionary <object, object> additionalData = default) where T : class { if (!ReferenceEquals(argumentValue, null)) { return; } var ex = new ArgumentNullException(argumentName.ToNullIfWhitespace(), exceptionMessage.ToNullIfWhitespace()); ex.AddData(additionalData); throw ex; }
/// <summary> /// Guards against an argument being null or an empty enumerable. Will throw an /// <see cref="ArgumentNullException">ArgumentNullException</see> if the argument is null. Will throw an /// <see cref="ArgumentException">ArgumentException</see> if the argument is an empty enumerable. /// </summary> /// <param name="argumentValue">The argument value to guard.</param> /// <param name="argumentName"> /// (Optional) Name of the argument. If specified it will be included in the thrown exception /// and therefore make it more informative. /// </param> /// <param name="exceptionMessage"> /// (Optional) Custom error message. A specific error message that can be used to describe /// the exception in more detail than the default message. /// </param> /// <param name="additionalData">(Optional) Additional information to add to the Data property of the thrown exception.</param> /// <exception cref="ArgumentNullException">Will be thrown when <c>argumentValue</c> is <c>null</c> .</exception> /// <exception cref="ArgumentException">Will be thrown when <c>argumentValue</c> is an empty enumerable.</exception> /// <example> /// <code> /// public void MyAmazingMethod(string[] myArgument) /// { /// GuardAgainst.ArgumentBeingNullOrEmpty(myArgument, nameof(myArgument)); /// /// // Remaining code omitted. /// } /// </code> /// </example> public static void ArgumentBeingNullOrEmpty <T>(IEnumerable <T> argumentValue, string argumentName = null, string exceptionMessage = null, IDictionary <object, object> additionalData = default) { if (ReferenceEquals(argumentValue, null)) { var ex = new ArgumentNullException(argumentName.ToNullIfWhitespace(), exceptionMessage.ToNullIfWhitespace()); ex.AddData(additionalData); throw ex; } if (!argumentValue.Any()) { var ex = new ArgumentException(exceptionMessage.ToNullIfWhitespace(), argumentName.ToNullIfWhitespace()); ex.AddData(additionalData); throw ex; } }