{"record":{"id":"51e5604b507da5f9","repo":"jackwener/OpenCLI","slug":"label-returned-invalid-json-51e560","errorCode":null,"errorMessage":"${label} returned invalid JSON","messagePattern":"(.+?) returned invalid JSON","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"clis/instagram/profile.js","lineNumber":26,"sourceCode":"    args: [\n        { name: 'username', required: true, positional: true, help: 'Instagram username' },\n    ],\n    columns: ['username', 'name', 'followers', 'following', 'posts', 'verified', 'bio'],\n    pipeline: [\n        { navigate: 'https://www.instagram.com' },\n        { evaluate: `(async () => {\n  const username = \\${{ args.username | json }};\n  const opts = { credentials: 'include', headers: { 'X-IG-App-ID': '936619743392459' } };\n  function normalizeInstagramUserId(value, label) {\n    const id = typeof value === 'number' ? String(value) : (typeof value === 'string' ? value.trim() : '');\n    if (!/^\\\\d+$/.test(id)) throw new Error(label);\n    return id;\n  }\n  async function readInstagramJson(response, label) {\n    try {\n      return await response.json();\n    } catch {\n      throw new Error(label + ' returned invalid JSON');\n    }\n  }\n  function throwInstagramHttpError(response, label) {\n    if (response.status === 404) throw new Error('User not found: ' + username);\n    if (response.status === 401 || response.status === 403) {\n      throw new Error('HTTP ' + response.status + ' - make sure you are logged in to Instagram');\n    }\n    throw new Error(label + ' failed: HTTP ' + response.status);\n  }\n  function mapProfileUser(u, countFields) {\n    if (!u || typeof u !== 'object' || typeof u.username !== 'string' || !u.username.trim()) {\n      throw new Error('Instagram profile returned malformed user payload for: ' + username);\n    }\n    return {\n      username: u.username,\n      name: typeof u.full_name === 'string' ? u.full_name : '',\n      bio: (typeof u.biography === 'string' ? u.biography : '').replace(/\\\\n/g, ' ').substring(0, 120),\n      followers: countFields.followers(u),","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/instagram/profile.js#L8-L44","documentation":"readInstagramJson wraps response.json() inside the profile.js evaluate script. If the HTTP response body cannot be parsed as JSON (HTML error page, empty body, encoding issues), it throws '<label> returned invalid JSON' where label identifies which Instagram endpoint failed (web_profile_info, feed-by-username, or users info).","triggerScenarios":"An Instagram profile API endpoint returns non-JSON (login redirect HTML, challenge page, empty 200 body, or 5xx HTML error) during clis/instagram/profile.","commonSituations":"Expired/absent session causing Instagram to return the login HTML page with 200; Instagram serving a challenge/captcha page; proxy or captive portal injecting HTML; transient 5xx with HTML body.","solutions":["Log in to Instagram in the CLI session so endpoints return JSON instead of login HTML","Capture and print response.text() for the failing request to see what Instagram actually returned","Check response.ok/status before parsing JSON to distinguish HTTP errors from parse errors","Retry after a short delay if it was a transient 5xx; disable interfering proxies"],"exampleFix":"// before\nasync function readInstagramJson(response, label) {\n  try { return await response.json(); }\n  catch { throw new Error(label + ' returned invalid JSON'); }\n}\n// after: include a body preview for diagnosis\nasync function readInstagramJson(response, label) {\n  const text = await response.text();\n  try { return JSON.parse(text); }\n  catch { throw new Error(label + ' returned invalid JSON: ' + text.slice(0, 200)); }\n}","handlingStrategy":"try-catch","validationCode":"// Pre-flight: confirm the session cookie exists so endpoints return JSON, not login HTML\nconst cookies = await page.evaluate(`document.cookie`);\nif (!cookies || !cookies.includes('sessionid')) {\n  throw new Error('Not logged in to Instagram; run the login command first');\n}","typeGuard":"function isJsonObject(text) {\n  try { const v = JSON.parse(text); return v !== null && typeof v === 'object'; } catch { return false; }\n}","tryCatchPattern":"try {\n  const profile = await instagramProfile(username);\n} catch (e) {\n  if (/returned invalid JSON/.test(e.message)) {\n    // usually a login/challenge HTML page — re-authenticate and retry once\n    await instagramLogin();\n    return instagramProfile(username);\n  }\n  throw e;\n}","preventionTips":["Always verify sessionid cookie presence before running profile queries","Check response content-type/status before parsing JSON","Avoid running through proxies that can inject HTML error pages","Retry 5xx responses after a delay rather than parsing their HTML bodies"],"tags":["instagram","json","network","response-parsing"],"backgroundTag":"invalid-json-response","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}