git-ecosystem/git-credential-manager · error · ArgumentOutOfRangeException

Argument must be positive.

Error message

Argument must be positive.

What it means

This ArgumentOutOfRangeException is thrown by EnsureArgument.Positive when an int argument is zero or negative. The parameter must be strictly greater than zero (> 0), e.g., a page size, batch size, or count. The parameter name is included in the exception.

Solutions

  1. Ensure the value is >= 1 before the call, e.g., Math.Max(1, value) or a proper default
  2. Check initialization order - the value may not have been set before first use
  3. Fix the config/source producing 0 or a negative value
  4. If 0 is valid "unset", switch to int? and validate only when set

Example fix

// before
EnsureArgument.Positive(batchSize, nameof(batchSize)); // batchSize defaults to 0
// after
int batchSize = config.BatchSize > 0 ? config.BatchSize : DefaultBatchSize;
EnsureArgument.Positive(batchSize, nameof(batchSize));
Defensive patterns

Strategy: validation

Validate before calling

if (value <= 0) throw new InvalidOperationException($"{name} must be > 0, was {value}");
// or default: value = value > 0 ? value : DefaultValue;

Type guard

static bool IsPositive(int v) => v > 0;

Try / catch

try { EnsureArgument.Positive(batchSize, nameof(batchSize)); }
catch (ArgumentOutOfRangeException ex)
{
    logger.LogError("{Param}={Value}; must be strictly positive", ex.ParamName, batchSize);
    batchSize = DefaultBatchSize;
}

Prevention

When it happens

Trigger: Calling EnsureArgument.Positive(arg, name) with arg <= 0, e.g., an uninitialized/default(int) == 0 value, an empty collection's count, or a config value of "0" or negative.

Common situations: Forgetting to initialize a field so it stays at the default 0; dividing work by a batch size that was configured as 0; taking .Count of an empty list; config placeholder "0" never replaced.

Related errors


AI-assisted analysis of git-ecosystem/git-credential-manager@e8ce762cd0 (2026-09-11). Data as JSON: /api/errors/88783f0f409f5bed. Report an issue: GitHub.

Appendix: source

Thrown at src/Core/EnsureArgument.cs:57

            if (!arg.IsAbsoluteUri)
            {
                throw new ArgumentException("Argument must be an absolute URI.", name);
            }
        }

        public static void PositiveOrZero(int arg, string name)
        {
            if (arg < 0)
            {
                throw new ArgumentOutOfRangeException(name, "Argument must be positive or zero (non-negative).");
            }
        }

        public static void Positive(int arg, string name)
        {
            if (arg <= 0)
            {
                throw new ArgumentOutOfRangeException(name, "Argument must be positive.");
            }
        }

        public static void NegativeOrZero(int arg, string name)
        {
            if (arg > 0)
            {
                throw new ArgumentOutOfRangeException(name, "Argument must be negative or zero (non-positive).");
            }
        }

        public static void Negative(int arg, string name)
        {
            if (arg >= 0)
            {
                throw new ArgumentOutOfRangeException(name, "Argument must be negative.");
            }
        }

View on GitHub (pinned to e8ce762cd0)