Hmbown/CodeWhale · error · ValueError

unit must be 'chars' or 'lines'

Error message

unit must be 'chars' or 'lines'

What it means

peek() returns a bounded slice of the REPL input buffer either by character offsets or by line numbers. The unit parameter is normalized (case-insensitively) and must resolve to 'chars'/'char' or 'lines'/'line'; anything else is rejected with this ValueError. It is an eager fail-loud guard so a misspelled unit never silently falls back to a default slicing mode.

Solutions

  1. Pass unit="chars" (default) or unit="lines"
  2. Fix typos in the unit string; matching is case-insensitive so 'Lines' is fine
  3. Omit the unit argument entirely to get the default 'chars' behavior
  4. If you need word- or byte-based slicing, implement it on top of peek's char slices

Example fix

// before
peek(0, 500, unit="bytes")
// after
peek(0, 500, unit="chars")
Defensive patterns

Strategy: validation

Validate before calling

const UNIT_OK = new Set(["char", "chars", "line", "lines"]);
if (!UNIT_OK.has(String(unit).toLowerCase())) throw new Error(`unit must be 'chars' or 'lines', got ${unit}`);

Type guard

const isPeekUnit = (u) => typeof u === "string" && ["char","chars","line","lines"].includes(u.toLowerCase());

Try / catch

try { const slice = peek(start, end, unit); } catch (e) { if (String(e).includes("unit must be")) { /* retry with unit='chars' */ } else throw e; }

Prevention

When it happens

Trigger: Calling peek(start, end, unit=...) with a unit string other than chars/char/lines/line, e.g. unit="words", unit="byte", unit=None, unit=3, or a typo like unit="charr".

Common situations: Hand-writing tool calls to the REPL peek helper and misspelling the unit; passing a non-string (int, None) unit from generated code; copying an example that used a different helper's unit vocabulary such as 'bytes' or 'words'.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/4562a73727ab600c. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/repl/runtime.rs:865

def _slice_chars(start, end):
    total = len(_context)
    s = max(0, int(start))
    e = max(s, min(total, int(end)))
    return _context[s:e]

def _slice_lines(start, end):
    lines = _context.splitlines()
    s = max(0, int(start))
    e = max(s, min(len(lines), int(end)))
    return "\n".join(lines[s:e])

def peek(start, end, unit="chars"):
    """Return a bounded slice of the input by char offsets or line numbers."""
    if str(unit).lower() in ("line", "lines"):
        return _slice_lines(start, end)
    if str(unit).lower() not in ("char", "chars"):
        raise ValueError("unit must be 'chars' or 'lines'")
    return _slice_chars(start, end)

def search(pattern, max_hits=100):
    """Regex-search the input and return bounded hit records with snippets."""
    max_hits = max(0, int(max_hits))
    hits = []
    if max_hits == 0:
        return hits
    rx = _re.compile(str(pattern), _re.MULTILINE)
    for i, m in enumerate(rx.finditer(_context)):
        if i >= max_hits:
            break
        start, end = m.span()
        snippet_start = max(0, start - 120)
        snippet_end = min(len(_context), end + 120)
        hits.append({
            "index": i,
            "start": start,

View on GitHub (pinned to 73e0f67d83)