/// <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 a DateTime argument being 'Unspecified'. Will throw an /// <see cref="ArgumentException">ArgumentException</see> when the 'Kind' is 'Unspecified'. /// </summary> /// <param name="argumentValue">The argument value to guard.</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="ArgumentException">Will be thrown when <c>argumentValueInvalid</c> is evaluates to <c>true</c> .</exception> /// <example> /// <code> /// public void MyAmazingMethod(DateTime myArgument) /// { /// GuardAgainst.ArgumentBeingUnspecifiedDateTime(myArgument, nameof(myArgument)); /// /// // Remaining code omitted. /// } /// </code> /// </example> public static void ArgumentBeingUnspecifiedDateTime(DateTime argumentValue, string argumentName = null, string exceptionMessage = null, IDictionary <object, object> additionalData = default) { if (argumentValue.Kind != DateTimeKind.Unspecified) { return; } var ex = new ArgumentException(exceptionMessage.ToNullIfWhitespace(), argumentName.ToNullIfWhitespace()); ex.AddData(additionalData); throw ex; }
/// <summary> /// Guards against an argument being an empty enumerable. 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="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 ArgumentBeingEmpty <T>(IEnumerable <T> argumentValue, string argumentName = null, string exceptionMessage = null, IDictionary <object, object> additionalData = default) { if (ReferenceEquals(argumentValue, default(IEnumerable <T>)) || argumentValue.Any()) { return; } var ex = new ArgumentException(exceptionMessage.ToNullIfWhitespace(), argumentName.ToNullIfWhitespace()); ex.AddData(additionalData); throw ex; }
/// <summary> /// Guards against an argument being invalid. The argument is invalid if the condition evaluates to <c>True</c> . /// Will throw an <see cref="ArgumentException">ArgumentException</see> . /// </summary> /// <param name="argumentValueInvalid">A boolean condition, which if <c>true</c> , indicates if the argument is invalid.</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="ArgumentException">Will be thrown when <c>argumentValueInvalid</c> is evaluates to <c>true</c> .</exception> /// <example> /// <code> /// public void MyAmazingMethod(string myArgument) /// { /// GuardAgainst.ArgumentBeingInvalid(myArgument.StartsWith("!"), nameof(myArgument)); /// /// // Remaining code omitted. /// } /// </code> /// </example> public static void ArgumentBeingInvalid(bool argumentValueInvalid, string argumentName = null, string exceptionMessage = null, IDictionary <object, object> additionalData = default) { if (!argumentValueInvalid) { return; } var ex = new ArgumentException(exceptionMessage.ToNullIfWhitespace(), argumentName.ToNullIfWhitespace()); ex.AddData(additionalData); throw ex; }