mgth/LittleBigMouse · error · ArgumentException

Enter a valid IPv4 address.

Error message

Enter a valid IPv4 address.

What it means

Ipv4Address.Require validates that a string is a literal IPv4 address and returns it trimmed; otherwise it throws ArgumentException naming the offending parameter. The library centralizes IPv4 validation so callers get a consistent error with an optional custom message (InvalidMessage by default: 'Enter a valid IPv4 address.').

Solutions

  1. Validate the input with Ipv4Address.IsValid before passing it to APIs that call Require
  2. Convert hostnames to IP addresses first (DNS/DNS-SD lookup) — Require only accepts literal IPv4
  3. Show an inline IP-format validator in the UI so malformed input never reaches the service
  4. Catch ArgumentException and surface the parameter name/message to the user

Example fix

// before
service.Associate(monitorId, userInput); // throws ArgumentException
// after
if (!Ipv4Address.IsValid(userInput))
    throw new ArgumentException("Enter a valid IPv4 address.", nameof(userInput));
service.Associate(monitorId, Ipv4Address.Require(userInput, nameof(userInput)));
Defensive patterns

Strategy: validation

Validate before calling

if (!Ipv4Address.IsValid(userInput))
    throw new ArgumentException("Enter a valid IPv4 address.", nameof(userInput));

Type guard

static bool IsLiteralIPv4(string? s) => Ipv4Address.IsValid(s);

Try / catch

try
{
    value = Ipv4Address.Require(input, nameof(input));
}
catch (ArgumentException ex)
{
    // show ex.Message to the user and re-prompt for the IP
}

Prevention

When it happens

Trigger: Passing a null, empty, hostname (e.g. 'mytv.local'), IPv6, or malformed string (e.g. '192.168.1.999', '192.168.1') to Ipv4Address.Require, or to any API whose parameter is validated through it.

Common situations: User typed a hostname instead of an IP in settings; copy-paste with stray characters or whitespace-only input; display configured via DHCP so the user entered a stale or wrong address; transposed digits producing an out-of-range octet.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of mgth/LittleBigMouse@7a42f01d47 (2026-09-16). Data as JSON: /api/errors/c0a49acf0e824575. Report an issue: GitHub.

Appendix: source

Thrown at LittleBigMouse.Plugins/LittleBigMouse.Plugin.Vcp/Networking/Ipv4Address.cs:43

    {
        address = null;
        if (string.IsNullOrWhiteSpace(value)) return false;
        if (!IPAddress.TryParse(value.Trim(), out var parsed)
            || parsed.AddressFamily != AddressFamily.InterNetwork) return false;

        address = parsed;
        return true;
    }

    /// <summary>
    /// Returns the address without its surrounding blanks, so callers store and send the same
    /// text they validated.
    /// </summary>
    /// <exception cref="ArgumentException">The value is not a literal IPv4 address.</exception>
    public static string Require(string? value, string parameterName, string? message = null)
        => IsValid(value)
            ? value.Trim()
            : throw new ArgumentException(message ?? InvalidMessage, parameterName);
}

View on GitHub (pinned to 7a42f01d47)