Stirling-Tools/Stirling-PDF · warning · Error

Expected number

Error message

Expected number

What it means

Thrown by readRequiredNumber when tryReadNumber returns null — i.e. the next characters are not leading digits. It is called after a '-' in a range (number '-' number), so this specifically means a range's end value is missing or non-numeric. Like sibling parser errors, it is caught by the CSV fallback unless strict mode is on.

Source

Thrown at frontend/editor/src/core/utils/bulkselection/parseSelection.ts:336

        return null;
      }
      c = sign === "-" ? -cVal : cVal;
    }
    return { k, c };
  }

  private tryReadNumber(): number | null {
    this.skipWs();
    const m = this.src.slice(this.idx).match(/^(\d+)/);
    if (!m) return null;
    this.consume(m[1].length);
    const num = parseInt(m[1], 10);
    return Number.isFinite(num) ? num : null;
  }

  private readRequiredNumber(): number {
    const n = this.tryReadNumber();
    if (n === null) throw new Error("Expected number");
    return n;
  }

  private readWord(): string | null {
    this.skipWs();
    const m = this.src.slice(this.idx).match(/^([A-Za-z]+)/);
    if (!m) return null;
    this.consume(m[1].length);
    return m[1];
  }

  private tryConsumeNot(): boolean {
    const start = this.idx;
    const word = this.readWord();
    if (!word) {
      this.idx = start;
      return false;
    }

View on GitHub (pinned to 9ef20dcab8)

Solutions

  1. Complete every range with an end number: 'start-end', e.g. '1-10'.
  2. Remove a trailing '-' if a single page was intended ('1' not '1-').
  3. Validate with a regex like /^\d+\s*-\s*\d+$/ per token before submitting in strict contexts.
  4. Use the diagnostics warning to flag incomplete ranges before strict enforcement.

Example fix

// before
parseSelectionWithDiagnostics('1-', max, { strict: true })
// after
parseSelectionWithDiagnostics('1-10', max, { strict: true })
Defensive patterns

Strategy: validation

Validate before calling

function rangesAreComplete(input: string): boolean {
  return input.split(',').every(tok => !/\d\s*-\s*$/.test(tok.trim()));
}

if (!rangesAreComplete(input)) {
  showUser('Every range must have an end number (e.g. 1-10).');
}

Type guard

function isCompleteRangeToken(tok: string): boolean {
  return /^\d+\s*-\s*\d+$/.test(tok.trim()) || /^\d+$/.test(tok.trim());
}

Try / catch

try {
  const { pages } = parseSelectionWithDiagnostics(input, maxPages, { strict: true });
} catch (e) {
  if (e instanceof Error && e.message === 'Expected number') {
    showUser('A range is missing its end number.');
  } else { throw e; }
}

Prevention

When it happens

Trigger: Input like '1-' (range with no end), '1-a' (range end is a letter), or '5-' followed by end-of-string. The parser read the start number, consumed '-', then expected digits but found none.

Common situations: User typed an incomplete range and submitted, or a trailing '-' was left from editing. Strict mode surfaces the error; non-strict falls back to CSV (which ignores the malformed token).

Related errors


AI-assisted analysis of Stirling-Tools/Stirling-PDF@9ef20dcab8 (2026-08-13). Data as JSON: /api/errors/0f4b728bd3a02069. Report an issue: GitHub.