{"record":{"id":"058a08f1df905c47","repo":"jackwener/OpenCLI","slug":"label-returned-malformed-json-err-message-058a08","errorCode":null,"errorMessage":"${label} returned malformed JSON: ${err?.message ?? err}","messagePattern":"(.+?) returned malformed JSON: (.+?)","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/oeis/utils.js","lineNumber":71,"sourceCode":"            `${label} request failed: ${err?.message ?? err}`,\n            'Check that oeis.org is reachable from this network.',\n        );\n    }\n    if (resp.status === 404) {\n        throw new EmptyResultError(label, `OEIS returned 404 for ${url}.`);\n    }\n    if (resp.status === 429) {\n        throw new CommandExecutionError(`${label} returned HTTP 429 (rate limited)`);\n    }\n    if (!resp.ok) {\n        throw new CommandExecutionError(`${label} returned HTTP ${resp.status}`);\n    }\n    let body;\n    try {\n        body = await resp.json();\n    }\n    catch (err) {\n        throw new CommandExecutionError(`${label} returned malformed JSON: ${err?.message ?? err}`);\n    }\n    return body;\n}\n\n/** Format OEIS' `number: 40` into the canonical zero-padded id `A000040`. */\nexport function formatId(number) {\n    if (typeof number !== 'number' || !Number.isInteger(number) || number < 0) return null;\n    return `A${String(number).padStart(6, '0')}`;\n}\n\n/** Take the first N comma-separated terms from OEIS' `data` string. */\nexport function previewTerms(data, max = 12) {\n    if (typeof data !== 'string') return '';\n    const terms = data.split(',').map((t) => t.trim()).filter(Boolean);\n    if (terms.length <= max) return terms.join(', ');\n    return [...terms.slice(0, max), `(+${terms.length - max})`].join(', ');\n}\n","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/oeis/utils.js#L53-L89","documentation":"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.","triggerScenarios":"A captive portal/proxy injecting an HTML page, a truncated response body, or OEIS serving an error page with HTTP 200.","commonSituations":"Corporate proxies or VPNs rewriting responses, flaky mobile connections truncating the body, or the endpoint format changing to non-JSON.","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."],"exampleFix":"// before (proxy injects HTML page)\noeis id A000045\n// after\nexport NO_PROXY=oeis.org\noeis id A000045","handlingStrategy":"retry","validationCode":"null","typeGuard":"null","tryCatchPattern":"try { return await oeisFetch(url, label); }\ncatch (e) {\n  if (String(e.message).includes('malformed JSON')) {\n    const raw = await fetch(url).then(r => r.text());\n    console.error('Raw response head:', raw.slice(0, 200));\n  }\n  throw e;\n}","preventionTips":["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."],"tags":["json","parse","http"],"backgroundTag":"invalid-json-response","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}