MiniMax-AI/skills · error · ArgumentOutOfRangeException

level

Error message

level

What it means

`throw new ArgumentOutOfRangeException(nameof(level))` inside `CreateHeadingStyle(int level, FontConfig fonts)`. The C# factory guards its `level` parameter: only 1–9 are valid (1–6 are the standard Word headings). When `level` is below 1 or above 9 the method refuses to build the heading style because the internal `headingSizes` and `outlineLevels` arrays (each length 9) would be indexed out of range, and Word only recognizes outline levels 0–8.

Source

Thrown at skills/minimax-docx/references/openxml_encyclopedia_part1.md:879

```csharp
// =============================================================================
// HEADING STYLES WITH PROPER INHERITANCE CHAIN
// =============================================================================
// Word's built-in heading system uses style inheritance:
// Normal (base) -> Heading1 -> Heading2 -> Heading3 -> Heading4 -> Heading5 -> Heading6
//
// Why this matters:
// - Each heading INHERITS from its parent (basedOn)
// - Define common properties in Normal, override in each heading
// - Change body font once in Normal, all headings inherit it
// - Heading-specific properties override as needed

// --- HEADING STYLE FACTORY ---
public static Style CreateHeadingStyle(int level, FontConfig fonts)
{
    // Validate level (1-9 are valid, 1-6 are standard)
    if (level < 1 || level > 9)
        throw new ArgumentOutOfRangeException(nameof(level));

    double[] headingSizes = [26.0, 20.0, 16.0, 14.0, 12.0, 11.0, 11.0, 11.0, 11.0];
    string[] outlineLevels = ["0", "1", "2", "3", "4", "5", "6", "7", "8"};

    var style = new Style(
        new StyleName { Val = $"heading {level}" },  // Display name
        new BasedOn { Val = level == 1 ? "Normal" : $"Heading{level - 1}" },  // Parent style
        new NextParagraphStyle { Val = "Normal" },   // After heading -> Normal
        new PrimaryStyle(),                          // Show in Styles gallery
        new UIPriority { Val = 9 - level },         // Priority in gallery (H1 = 8, H2 = 7, etc.)
        new QuickStyle(),                           // Appears in Quick Styles gallery
        // Paragraph properties: spacing, keep options, outline level
        new StyleParagraphProperties(
            new KeepNext(),                         // Keep heading with next paragraph
            new KeepLines(),                        // Keep all lines of heading together
            new SpacingBetweenLines                 // Spacing before/after
            {
                Before = level == 1 ? "480" : "240",  // H1 = 240pt before, others = 120pt

View on GitHub (pinned to 60aaae52bb)

Solutions

  1. Clamp/validate the caller's value before invoking: ensure level is within 1–9 (and 1–6 if you only want standard headings).
  2. Fix the off-by-one in the loop: start at `i = 1` and use `<` rather than `<=` where appropriate.
  3. If generating from user input, map/clamp: `level = Math.Clamp(parsed, 1, 6);` before the call.

Example fix

// before
for (var i = 0; i < headings.Count; i++)
    styles.Add(CreateHeadingStyle(i, fonts)); // i=0 throws

// after
for (var i = 1; i <= headings.Count && i <= 9; i++)
    styles.Add(CreateHeadingStyle(i, fonts));
Defensive patterns

Strategy: validation

Validate before calling

static int NormalizeLevel(int level)
{
    if (level < 1 || level > 9)
        throw new ArgumentOutOfRangeException(nameof(level),
            $"Heading level must be 1-9, was {level}.");
    return level;
}

// at the call site
int safe = Math.Clamp(userLevel, 1, 6); // restrict to standard headings
CreateHeadingStyle(safe, fonts);

Type guard

bool IsValidHeadingLevel(int level) => level >= 1 && level <= 9;

// usage
if (!IsValidHeadingLevel(parsed))
    throw new ArgumentException($"Invalid heading depth: {parsed}");

Try / catch

try
{
    var style = CreateHeadingStyle(depth, fonts);
}
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == nameof(level))
{
    // clamp and retry, or skip this heading
    logger.LogWarning("Bad heading level {Depth}; clamping to 1-6", depth);
    style = CreateHeadingStyle(Math.Clamp(depth, 1, 6), fonts);
}

Prevention

When it happens

Trigger: Calling `CreateHeadingStyle(0, fonts)`, `CreateHeadingStyle(-1, ...)`, or `CreateHeadingStyle(10, ...)` (any value < 1 or > 9). Common when generating headings programmatically from a loop boundary that is off-by-one or from untrusted user/UI input mapped directly into the level.

Common situations: Off-by-one loop (e.g. `for (var i = 0; i <= headings.Count; i++)` starting at 0), parsing a markdown heading depth of 0 or a value beyond 6 without clamping, or treating a 0-indexed array slot as a 1-indexed Word level.

Related errors


AI-assisted analysis of MiniMax-AI/skills@60aaae52bb (2026-08-13). Data as JSON: /api/errors/caa3424dfb216ca6. Report an issue: GitHub.