jackwener/OpenCLI · error · ArgumentError

weread-official: ${label} contains invalid characters

Error message

weread-official: ${label} contains invalid characters

What it means

requireBookId first trims via requireText, then enforces /^[A-Za-z0-9_-]+$/ so only gateway-safe book identifiers are sent. Anything containing URL prefixes, slashes, punctuation, or unicode throws ArgumentError with the hint to use a bookId from `weread-official search`.

Source

Thrown at clis/weread-official/utils.js:254

export function parseRange(range) {
    const text = String(range ?? '').trim();
    const match = text.match(/^(\d+)-(\d+)$/);
    if (!match) return { rangeStart: '', rangeEnd: '' };
    return { rangeStart: match[1], rangeEnd: match[2] };
}

// ── Argument validation ─────────────────────────────────────────────────────

export function requireText(value, label) {
    const text = String(value ?? '').trim();
    if (!text) throw new ArgumentError(`weread-official: ${label} cannot be empty`);
    return text;
}

export function requireBookId(value, label = 'bookId') {
    const text = requireText(value, label);
    if (!/^[A-Za-z0-9_-]+$/.test(text)) {
        throw new ArgumentError(`weread-official: ${label} contains invalid characters`, 'Pass a bookId from `weread-official search`.');
    }
    return text;
}

export function requirePositiveInt(value, label, { defaultValue, max } = {}) {
    if (value === undefined || value === null || value === '') {
        if (defaultValue === undefined) {
            throw new ArgumentError(`weread-official: ${label} is required`);
        }
        return defaultValue;
    }
    const text = String(value).trim();
    if (!/^\d+$/.test(text)) {
        throw new ArgumentError(`weread-official: ${label} must be a positive integer`);
    }
    const n = Number(text);
    if (!Number.isSafeInteger(n) || n < 1) {
        throw new ArgumentError(`weread-official: ${label} must be a positive integer`);

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Run `weread-official search --query <title>` and use the bookId field from its output verbatim.
  2. Strip URL prefixes: take only the final path segment of a bookDetail link.
  3. Validate the id locally against /^[A-Za-z0-9_-]+$/ before calling.
  4. Regenerate rather than hand-edit bookIds from notes or spreadsheets.

Example fix

// before
const bookId = 'https://weread.qq.com/web/bookDetail/wra_abc123';
// after
const bookId = 'wra_abc123'; // from `weread-official search` output
Defensive patterns

Strategy: validation

Validate before calling

const BOOK_ID_RE = /^[A-Za-z0-9_-]+$/;
if (!BOOK_ID_RE.test(bookId)) throw new Error(`bookId must match ${BOOK_ID_RE} — got: ${bookId}`);

Type guard

const isValidBookId = (v) => typeof v === 'string' && /^[A-Za-z0-9_-]+$/.test(v.trim());

Try / catch

try {
  return await highlights(requireBookId(rawBookId));
} catch (e) {
  if (e instanceof ArgumentError && /invalid characters/.test(e.message)) {
    console.error('bookId malformed — run `weread-official search` and copy bookId exactly');
    process.exitCode = 2;
  } else throw e;
}

Prevention

When it happens

Trigger: Passing a full WeRead URL like https://weread.qq.com/web/bookDetail/abc123 instead of the id; pasting an id with trailing spaces/newlines is fine, but quotes, '%xx' encodings, or CJK characters are not; fabricating/guessing a bookId.

Common situations: Copying a book link from the browser and using the whole URL as bookId; storing ids in CSV where Excel mangles long numeric ids; using an ISBN or internal numeric id that is not the gateway's book format.

Understand the failure class

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/b19a9c619d25b47d. Report an issue: GitHub.