{"record":{"id":"29e8c2f85d353628","repo":"jackwener/OpenCLI","slug":"hf-datasets-returned-malformed-json-error-mess","errorCode":null,"errorMessage":"hf datasets returned malformed JSON: ${error?.message || error}","messagePattern":"hf datasets returned malformed JSON: (.+?)","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/hf/datasets.js","lineNumber":65,"sourceCode":"        let resp;\n        try {\n            resp = await fetch(url, {\n                headers: {\n                    'Accept': 'application/json',\n                    'User-Agent': 'opencli/1.0 (+https://github.com/jackwener/opencli)',\n                },\n            });\n        } catch (error) {\n            throw new CommandExecutionError(`hf datasets request failed: ${error?.message || error}`);\n        }\n        if (!resp.ok) {\n            throw new CommandExecutionError(`hf datasets failed: HTTP ${resp.status}`);\n        }\n        let data;\n        try {\n            data = await resp.json();\n        } catch (error) {\n            throw new CommandExecutionError(`hf datasets returned malformed JSON: ${error?.message || error}`);\n        }\n        const list = Array.isArray(data) ? data : [];\n        if (list.length === 0) {\n            throw new EmptyResultError('hf datasets', 'No matching datasets on huggingface.co.');\n        }\n        return list.slice(0, limit).map((d, i) => {\n            const id = d.id || '';\n            const slashIdx = id.indexOf('/');\n            const author = slashIdx > 0 ? id.slice(0, slashIdx) : '';\n            const tags = Array.isArray(d.tags) ? d.tags.filter(t => !t.startsWith('license:')).slice(0, 10).join(', ') : '';\n            return {\n                rank: i + 1,\n                id,\n                author,\n                downloads: d.downloads ?? 0,\n                likes: d.likes ?? 0,\n                tags,\n                lastModified: d.lastModified ? String(d.lastModified).slice(0, 10) : '',","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/hf/datasets.js#L47-L83","documentation":"The datasets API response body could not be parsed as JSON (resp.json() threw), so the command throws CommandExecutionError with the parse error message. This usually means the endpoint returned HTML (error page, login/redirect page) instead of JSON.","triggerScenarios":"resp.json() rejects: HF returned an HTML error/interstitial page, empty body, or truncated response due to proxy/gateway interference.","commonSituations":"Captive portal or proxy returning HTML; HF serving a 200 HTML maintenance page; content-encoding mismatch; network truncation.","solutions":["Inspect the raw response body (curl the same URL) to see what was actually returned","Bypass or correctly configure proxies that rewrite responses","Retry if the response was truncated; check Accept: application/json header is sent","Update the CLI if HF changed the endpoint's content type"],"exampleFix":"// before\ndata = await resp.json(); // throws on HTML\n// after\nconst text = await resp.text(); try { data = JSON.parse(text); } catch (e) { console.error('non-JSON body:', text.slice(0, 200)); }","handlingStrategy":"fallback","validationCode":"const pre = await fetch(url, { headers: { Accept: 'application/json' } }); const ct = pre.headers.get('content-type') || ''; if (!ct.includes('application/json')) throw new Error(`expected JSON, got ${ct}`);","typeGuard":"function isDatasetArray(v) { return Array.isArray(v) && v.every(d => d && typeof d.id === 'string'); }","tryCatchPattern":"try { await hfDatasets(args); } catch (e) { if (String(e.message).includes('malformed JSON')) { console.error('non-JSON body from HF (proxy/HTML page?); bypass proxy or retry'); return null; } throw e; }","preventionTips":["Always send Accept: application/json","Verify proxies/gateways do not rewrite responses","Check content-type before parsing JSON","Retry on truncated responses"],"tags":["json","parsing","huggingface","api"],"backgroundTag":"invalid-json-response","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}