jackwener/OpenCLI · error · CommandExecutionError
mdn search returned malformed JSON: ${err?.message ?? err}
Error message
mdn search returned malformed JSON: ${err?.message ?? err} What it means
After a successful HTTP response, the adapter calls resp.json(). If the body is not valid JSON (HTML error page, truncated response, proxy interference), the JSON parse error is wrapped in this CommandExecutionError. It means the response succeeded at the HTTP layer but the payload is unusable.
Source
Thrown at clis/mdn/search.js:82
}
catch (err) {
throw new CommandExecutionError(`mdn search request failed: ${err?.message ?? err}`);
}
if (resp.status === 429) {
throw new CommandExecutionError(
'mdn search returned HTTP 429 (rate limited)',
'MDN throttles bursty traffic; wait a few seconds and retry.',
);
}
if (!resp.ok) {
throw new CommandExecutionError(`mdn search returned HTTP ${resp.status}`);
}
let body;
try {
body = await resp.json();
}
catch (err) {
throw new CommandExecutionError(`mdn search returned malformed JSON: ${err?.message ?? err}`);
}
const docs = Array.isArray(body?.documents) ? body.documents : [];
if (!docs.length) {
throw new EmptyResultError('mdn search', `No MDN results matched "${query}" (locale ${locale}).`);
}
return docs.slice(0, limit).map((doc, i) => ({
rank: i + 1,
title: String(doc.title ?? ''),
slug: String(doc.slug ?? ''),
locale: String(doc.locale ?? locale),
summary: String(doc.summary ?? '').replace(/\s+/g, ' ').trim(),
url: doc.mdn_url ? `${MDN_BASE}${doc.mdn_url}` : '',
}));
},
});
View on GitHub (pinned to 49907e53dc)
Solutions
- Retry the request — could be transient corruption.
- Verify what the endpoint actually returns: curl -s '<MDN search url>' and inspect the body.
- Check for proxy/captive-portal interference and fix network path or TLS interception.
- Update the adapter if MDN changed its API response format.
Example fix
// before
const r = await mdnSearch({ query: 'flexbox' });
// after
try {
const r = await mdnSearch({ query: 'flexbox' });
} catch (e) {
if (String(e.message).includes('malformed JSON')) return retryOrFallback(e);
throw e;
} Defensive patterns
Strategy: try-catch
Try / catch
try { return await mdnSearch({ query }); } catch (e) {
if (String(e.message).includes('malformed JSON')) { return fallbackLocalDocs(query); }
throw e;
} Prevention
- Verify endpoint payloads with curl when errors recur
- Watch for proxies/captive portals rewriting HTTPS bodies
- Retry once before failing — truncation is often transient
- Keep the adapter updated for MDN API format changes
When it happens
Trigger: MDN returning an HTML error/captcha page with 200; truncated or corrupted responses; middle-boxes (proxies, captive portals) rewriting the body.
Common situations: Captive portal Wi-Fi injecting HTML; corporate proxies intercepting TLS; MDN API format changes; intermittent network truncation.
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
- JSON parse failed (status=${response.status}, body[0..50]=${
- archive snapshots returned malformed JSON: ${error?.message
- Chess.com callback returned malformed JSON for ${url}: ${err
- `${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/4834a1aec214cdab.
Report an issue: GitHub.