d2phap/ImageGlass Β· error Β· ArgumentException

'{optionStr}' is not a valid rotation option. ---------- πŸ‘‰

Error message

'{optionStr}' is not a valid rotation option.

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

What it means

Thrown by the string overload of IG_Rotate when Enum.TryParse<RotateOption>(optionStr) fails. RotateOption has only two members, case-insensitively parsed: Left (rotate -90) and Right (rotate +90). Any other string raises ArgumentException.

Source

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

        if (enabled.Value)
        {
            Viewer.StartAnimator();
        }
        else
        {
            Viewer.StopAnimator();
        }
    }


    /// <summary>
    /// Rotate the current image according to the rotation options.
    /// </summary>
    public void IG_Rotate(string? optionStr)
    {
        if (!Enum.TryParse<RotateOption>(optionStr, out var options))
        {
            throw new ArgumentException($"""
                '{optionStr}' is not a valid rotation option.

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

        IG_Rotate(options);
    }


    /// <summary>
    /// Rotate the current image according to the rotation options.
    /// </summary>
    public static void IG_Rotate(RotateOption options)
    {
        if (Viewer.SourceKind == PhotoSource.None || Core.IsBusy) return;

View on GitHub (pinned to 4a3c4fecef)

Solutions

  1. Use exactly 'Left' or 'Right' (case-insensitive).
  2. Validate with Enum.TryParse<RotateOption> upstream and call the typed overload.
  3. Map common aliases ('cw' -> Right, 'ccw' -> Left, '90' -> Right, '-90' -> Left) before calling.

Example fix

// before
api.IG_Rotate(userInput);

// after
if (Enum.TryParse<RotateOption>(userInput?.Trim(), true, out var opt))
{
    api.IG_Rotate(opt);
}
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.TryParse<RotateOption>(optionStr?.Trim(), true, out var opt))
    throw new ArgumentException($"Unknown rotation option '{optionStr}'.");
api.IG_Rotate(opt);

Type guard

static bool IsValidRotateOption(string? s) => Enum.TryParse<RotateOption>(s?.Trim(), true, out _);

Try / catch

try { api.IG_Rotate(optionStr); }
catch (ArgumentException ex) when (ex.ParamName == nameof(optionStr))
{ /* valid: Left, Right */ }

Prevention

When it happens

Trigger: Calling IG_Rotate(string) with 'left'/'right' in another language, '90'/'-90' as a non-numeric label, 'cw'/'ccw', 'rotate-left', or any value other than Left/Right.

Common situations: Config with a descriptive label instead of the enum name; localized user input; legacy v9 keyword; trailing whitespace.

Related errors


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