d2phap/ImageGlass Β· error Β· ArgumentException

Step '{stepStr}' is not a valid integer. ---------- πŸ‘‰πŸΌ Me

Error message

Step '{stepStr}' is not a valid integer.

----------
πŸ‘‰πŸΌ Method: IG_ViewByStep

What it means

Thrown by the string overload of IG_ViewByStep when int.TryParse(stepStr) fails. The method parses a navigation step (positive = forward, negative = backward) from a string before delegating to the int overload. A null or non-integer string raises ArgumentException naming the parameter.

Source

Thrown at source/ImageGlass.Lib/Common/ServiceProviders/AppAPIs/AppAPIProvider.cs:901

    /// View a photo in the list by the given index.
    /// </summary>
    public void IG_ViewByIndex(int photoIndex)
    {
        if (photoIndex < 0) return;

        var step = photoIndex - Core.Photos.CurrentIndex;
        IG_ViewByStep(step);
    }


    /// <summary>
    /// View a photo in the list by the given step.
    /// </summary>
    public void IG_ViewByStep(string? stepStr)
    {
        if (!int.TryParse(stepStr, out var step))
        {
            throw new ArgumentException($"""
                Step '{stepStr}' is not a valid integer.

                ----------
                πŸ‘‰πŸΌ Method: {nameof(IG_ViewByStep)}
                """,
                nameof(stepStr));
        }

        IG_ViewByStep(step);
    }


    /// <summary>
    /// View a photo in the list by the given step.
    /// </summary>
    public void IG_ViewByStep(int step)
    {
        // Sequential mode: ignore navigation until the current photo is painted, so holding

View on GitHub (pinned to 4a3c4fecef)

Solutions

  1. Pass a plain integer string such as '1', '-1', '5' (no plus sign, no decimals, no surrounding spaces).
  2. Validate with int.TryParse before calling and use the int overload directly.
  3. Trim whitespace and reject empty input upstream.

Example fix

// before
api.IG_ViewByStep(userInput);

// after
if (int.TryParse(userInput?.Trim(), out var step))
{
    api.IG_ViewByStep(step);
}
Defensive patterns

Strategy: validation

Validate before calling

if (!int.TryParse(stepStr?.Trim(), out var step))
    throw new ArgumentException("Step must be an integer.");
api.IG_ViewByStep(step);

Type guard

static bool IsValidStep(string? s) => int.TryParse(s?.Trim(), out _);

Try / catch

try { api.IG_ViewByStep(stepStr); }
catch (ArgumentException ex) when (ex.ParamName == nameof(stepStr))
{ /* reject the input, prompt again */ }

Prevention

When it happens

Trigger: Calling IG_ViewByStep(string) with a non-numeric string: 'next', '+1', '1.0', '' (note: empty string fails TryParse), or a value with a leading/trailing space.

Common situations: Hotkey or CLI argument passed as a word rather than a number; config file with a quoted string; user-typed input in a custom command that was not sanitized.

Related errors


AI-assisted analysis of d2phap/ImageGlass@4a3c4fecef (2026-08-13). Data as JSON: /api/errors/d39e74a649d68a03. Report an issue: GitHub.