jackwener/OpenCLI · error · CommandExecutionError
${label} returned malformed JSON: ${err?.message ?? err}
Error message
${label} returned malformed JSON: ${err?.message ?? err} What it means
dblpFetchJson calls res.json() and this error is thrown when the response body cannot be parsed as JSON. It means dblp returned an HTTP 2xx response whose body is not valid JSON (e.g. an HTML error page or truncated response). The underlying parse error message is included.
Source
Thrown at clis/dblp/utils.js:62
if (res.status === 429) {
throw new CommandExecutionError(`${label} returned HTTP 429 (rate limited)`, 'dblp throttles clients that fetch too aggressively. Wait a few seconds and retry, or lower --limit.');
}
if (res.status === 404) {
throw new EmptyResultError(label, 'dblp returned 404 — the requested record may not exist.');
}
throw new CommandExecutionError(`${label} returned HTTP ${res.status}`, 'Inspect the response in a browser at the same URL for more context.');
}
return res;
}
export async function dblpFetchJson(path, label) {
const res = await dblpFetch(`${DBLP_ORIGIN}${path}`, label, 'application/json');
let body;
try {
body = await res.json();
}
catch (err) {
throw new CommandExecutionError(`${label} returned malformed JSON: ${err?.message ?? err}`);
}
const statusCode = String(body?.result?.status?.['@code'] ?? '').trim();
if (!statusCode) {
throw new CommandExecutionError(
`${label} returned JSON without result.status.@code`,
'dblp changed its JSON envelope or returned a partial error payload; inspect the raw response in a browser.',
);
}
if (statusCode !== '200') {
const statusText = String(body?.result?.status?.text ?? '').trim();
throw new CommandExecutionError(
`${label} returned API status ${statusCode}${statusText ? ` (${statusText})` : ''}`,
'dblp accepted the HTTP request but reported an API-level failure. Retry later or inspect the same query in a browser.',
);
}
return body;
}
View on GitHub (pinned to 49907e53dc)
Solutions
- Retry the request — truncated/bodies and transient interstitials often resolve on retry
- Open the same URL in a browser to see what body dblp actually returns
- Check for a proxy/captive portal that could inject HTML into 200 responses
- If persistent, verify the path/format= parameters produce a JSON API endpoint
Example fix
// before
const body = await res.json(); // throws on HTML body
// after
const text = await res.text();
let body;
try { body = JSON.parse(text); }
catch { console.error('Non-JSON body:', text.slice(0, 200)); } Defensive patterns
Strategy: try-catch
Validate before calling
const res = await fetch(url);
const text = await res.text();
if (!text.trim().startsWith('{') && !text.trim().startsWith('[')) {
console.warn('Non-JSON body received:', text.slice(0, 120));
} Type guard
function isJsonObject(v) { return v !== null && typeof v === 'object' && !Array.isArray(v); } Try / catch
try {
const json = await dblpFetchJson(path, 'dblp search');
} catch (err) {
if (/malformed JSON/.test(err.message)) {
// log/inspect the raw body, then retry once for truncated responses
return retryFetch(path);
}
throw err;
} Prevention
- Check for proxies/captive portals that inject HTML into responses
- Read the body as text and preview it before JSON.parse when debugging
- Retry truncated responses once before failing
- Pin requests to the documented /api?format=json endpoints
When it happens
Trigger: dblpFetch succeeded (res.ok) but res.json() throws — the body is HTML (error/interstitial page), empty, or truncated mid-stream.
Common situations: A proxy or captive portal injecting an HTML page with a 200 status; dblp serving a maintenance/interstitial page; network interruption truncating the response; hitting an HTML endpoint by mistake.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- archive search returned malformed JSON: ${error?.message ||
- archive wayback returned malformed JSON: ${error?.message ||
- hf models returned malformed JSON: ${error?.message || error
- ${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/803d1babbb87357d.
Report an issue: GitHub.