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
- Retry the request — truncation is often transient.
- Inspect the raw response (`curl -s <url> | head`) to see what was actually returned.
- 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
- Bypass intercepting proxies/captive portals for oeis.org.
- Retry truncated responses; log the raw body on parse failure.
- Validate the content-type is application/json when fetching manually.
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.
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- ${label} returned a non-JSON response
- archive snapshots returned malformed JSON: ${error?.message
- COMMAND_EXEC
- `${label} returned malformed JSON: ${err?.message ?? err}`
- ${label} returned malformed JSON: ${err?.message ?? err}
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/058a08f1df905c47.
Report an issue: GitHub.