jackwener/OpenCLI · error · CommandExecutionError

${label} returned malformed JSON: ${err?.message ?? err}

Error message

${label} returned malformed JSON: ${err?.message ?? err}

What it means

oeisFetch parses the response with resp.json(); if the body is not valid JSON (or the stream fails mid-read), it throws this CommandExecutionError including the parse error message. This guards against OEIS returning HTML error pages, truncated responses, or encoding problems behind a 200 status.

Source

Thrown at clis/oeis/utils.js:71

            `${label} request failed: ${err?.message ?? err}`,
            'Check that oeis.org is reachable from this network.',
        );
    }
    if (resp.status === 404) {
        throw new EmptyResultError(label, `OEIS returned 404 for ${url}.`);
    }
    if (resp.status === 429) {
        throw new CommandExecutionError(`${label} returned HTTP 429 (rate limited)`);
    }
    if (!resp.ok) {
        throw new CommandExecutionError(`${label} returned HTTP ${resp.status}`);
    }
    let body;
    try {
        body = await resp.json();
    }
    catch (err) {
        throw new CommandExecutionError(`${label} returned malformed JSON: ${err?.message ?? err}`);
    }
    return body;
}

/** Format OEIS' `number: 40` into the canonical zero-padded id `A000040`. */
export function formatId(number) {
    if (typeof number !== 'number' || !Number.isInteger(number) || number < 0) return null;
    return `A${String(number).padStart(6, '0')}`;
}

/** Take the first N comma-separated terms from OEIS' `data` string. */
export function previewTerms(data, max = 12) {
    if (typeof data !== 'string') return '';
    const terms = data.split(',').map((t) => t.trim()).filter(Boolean);
    if (terms.length <= max) return terms.join(', ');
    return [...terms.slice(0, max), `(+${terms.length - max})`].join(', ');
}

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Retry the request — truncation is often transient.
  2. Inspect the raw response (`curl -s <url> | head`) to see what was actually returned.
  3. Disable or bypass interfering proxies/VPNs and retry.

Example fix

// before (proxy injects HTML page)
oeis id A000045
// after
export NO_PROXY=oeis.org
oeis id A000045
Defensive patterns

Strategy: retry

Validate before calling

null

Type guard

null

Try / catch

try { return await oeisFetch(url, label); }
catch (e) {
  if (String(e.message).includes('malformed JSON')) {
    const raw = await fetch(url).then(r => r.text());
    console.error('Raw response head:', raw.slice(0, 200));
  }
  throw e;
}

Prevention

When it happens

Trigger: A captive portal/proxy injecting an HTML page, a truncated response body, or OEIS serving an error page with HTTP 200.

Common situations: Corporate proxies or VPNs rewriting responses, flaky mobile connections truncating the body, or the endpoint format changing to non-JSON.

Understand the failure class

Background: "Invalid JSON response" and "Failed to parse response" errors: when an API answers 200 but the body isn't the JSON your library expected — this error's family across 28 libraries.

Related errors


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