Code/Exceptions/ArgumentNullOrInvalidException.cs
#nullable enable
namespace Nodebox.Exceptions;

public class ArgumentNullOrInvalidException : ArgumentException {
    private const string GenericMsg = "argument is null or invalid";

    public ArgumentNullOrInvalidException() : base(GenericMsg) { }

    private static string IsNullToString(bool isNull) => isNull ? "null" : "invalid";
    public ArgumentNullOrInvalidException(string? paramName, bool isNull) : base($"argument {paramName} is {IsNullToString(isNull)}", paramName) { }

    public static void ThrowIfNullOrNotValid(IValid value, string? paramName = null) {
        if (value == null) {
            throw new ArgumentNullOrInvalidException(paramName, true);
        }

        if (!value.IsValid()) {
            throw new ArgumentNullOrInvalidException(paramName, false);
        }
    }
}