{"record":{"id":"d7b093a5835869cb","repo":"jackwener/OpenCLI","slug":"label-returned-malformed-json-err-message-d7b093","errorCode":null,"errorMessage":"${label} returned malformed JSON: ${err?.message ?? err}","messagePattern":"(.+?) returned malformed JSON: (.+?)","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/maven/utils.js","lineNumber":99,"sourceCode":"    }\n    if (resp.status === 404) {\n        throw new EmptyResultError(label, `Maven Central returned 404 for ${url}.`);\n    }\n    if (resp.status === 429) {\n        throw new CommandExecutionError(\n            `${label} returned HTTP 429 (rate limited)`,\n            'Maven Central throttles bursts; wait a few seconds and retry.',\n        );\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/** Convert epoch-ms (Maven Solr `timestamp`) to ISO-8601 UTC. Returns null for falsy/invalid. */\nexport function epochMsToIso(value) {\n    if (value == null) return null;\n    const n = typeof value === 'number' ? value : Number(value);\n    if (!Number.isFinite(n) || n <= 0) return null;\n    return new Date(n).toISOString().replace(/\\.\\d+Z$/, 'Z');\n}\n","sourceCodeStart":81,"sourceCodeEnd":111,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/maven/utils.js#L81-L111","documentation":"mavenFetch() calls resp.json() to parse the response body; if parsing fails (invalid/truncated/non-JSON body), it throws a CommandExecutionError noting the label returned malformed JSON, including the underlying parse error.","triggerScenarios":"search.maven.org (or an intermediary proxy/captive portal) returning an HTML error page, empty body, truncated response, or otherwise invalid JSON with a 2xx status.","commonSituations":"Captive portal or corporate proxy injecting an HTML interstitial; CDN returning a compressed/garbled body the client cannot decode; intermittent infrastructure issue returning partial bodies; hitting a mirror/intercepting endpoint that returns HTML.","solutions":["Inspect the raw response (curl the same URL) to see what body is actually returned.","Retry — this is often transient; add retry with backoff.","Check for proxy/captive-portal interference returning HTML instead of JSON.","Ensure your fetch environment handles gzip (proper user-agent/accept-encoding) and supports HTTPS to the real host."],"exampleFix":"// before\nconst body = await mavenFetch(url, 'search');\n// after\nlet body;\ntry {\n  body = await mavenFetch(url, 'search');\n} catch (err) {\n  if (/malformed JSON/.test(String(err))) return withBackoff(() => mavenFetch(url, 'search'));\n  throw err;\n}","handlingStrategy":"retry","validationCode":"const resp = await fetch(url);\nconst text = await resp.text();\ntry { JSON.parse(text); } catch { throw new Error('endpoint returned non-JSON'); }","typeGuard":"function isJsonObject(v) {\n  return typeof v === 'object' && v !== null && !Array.isArray(v);\n}","tryCatchPattern":"try {\n  const body = await mavenFetch(url, 'search');\n  if (!isJsonObject(body)) throw new Error('unexpected payload');\n  return body;\n} catch (err) {\n  if (/malformed JSON/.test(err.message)) return withBackoff(() => mavenFetch(url, 'search'));\n  throw err;\n}","preventionTips":["Retry once or twice — truncated bodies are usually transient.","Detect captive portals/proxies injecting HTML by inspecting the raw body.","Verify gzip/encoding handling in your fetch runtime.","Pin to the real search.maven.org host; avoid intercepting middleboxes."],"tags":["json","malformed-response","maven","command-execution-error"],"backgroundTag":"malformed-json-response","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}