{"record":{"id":"3de200bb34d8b16a","repo":"davila7/claude-code-templates","slug":"invalid-session-file-corrupted-or-not-a-claude-c","errorCode":null,"errorMessage":"Invalid session file - corrupted or not a Claude Code session","messagePattern":"Invalid session file - corrupted or not a Claude Code session","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"cli-tool/src/session-sharing.js","lineNumber":321,"sourceCode":"   * @returns {Promise<Object>} Session data\n   */\n  async downloadSession(url) {\n    try {\n      // Use curl to download (works with x0.at and other services)\n      const { stdout, stderr } = await execAsync(`curl -L \"${url}\"`, {\n        maxBuffer: 50 * 1024 * 1024 // 50MB buffer for large sessions\n      });\n\n      if (stderr && !stdout) {\n        throw new Error(`Download failed: ${stderr}`);\n      }\n\n      // Parse JSON response\n      const sessionData = JSON.parse(stdout);\n      return sessionData;\n    } catch (error) {\n      if (error.message.includes('Unexpected token')) {\n        throw new Error('Invalid session file - corrupted or not a Claude Code session');\n      }\n      throw error;\n    }\n  }\n\n  /**\n   * Validate session data structure\n   * @param {Object} sessionData - Session data to validate\n   * @throws {Error} If validation fails\n   */\n  validateSessionData(sessionData) {\n    if (!sessionData.version) {\n      throw new Error('Invalid session file - missing version');\n    }\n\n    if (!sessionData.conversation || !sessionData.conversation.id) {\n      throw new Error('Invalid session file - missing conversation data');\n    }","sourceCodeStart":303,"sourceCodeEnd":339,"githubUrl":"https://github.com/davila7/claude-code-templates/blob/a0851ed10c7c60463dac8cfaaca124cf32d5804d/cli-tool/src/session-sharing.js#L303-L339","documentation":"Thrown by downloadSession() in cli-tool/src/session-sharing.js when JSON.parse of the downloaded body fails with a 'Unexpected token' SyntaxError. That means the URL returned valid HTTP content that is not JSON — typically an HTML error page, a plaintext notice, or a truncated file — so it cannot be a Claude Code session export.","triggerScenarios":"Calling downloadSession(url) where the URL returns HTML (404 page, x0.at notice page) or arbitrary text. The curl download 'succeeds' (stdout non-empty) but the body is not JSON.","commonSituations":"Following a link to a file x0.at already deleted but that now serves an HTML notice; pasting a generic URL instead of the session share URL; truncated download of a large session that cut off mid-JSON.","solutions":["Open the URL in a browser and inspect what is actually returned — if it's HTML, the session file is gone or the URL is wrong","Re-run the download (transient truncation on large files) and confirm Content-Length matches","Get a fresh share link from the original session owner","If you maintain the code, log the first 200 chars of stdout to see exactly what came back"],"exampleFix":"// before\nconst sessionData = JSON.parse(stdout);\n\n// after\nlet sessionData;\ntry {\n  sessionData = JSON.parse(stdout);\n} catch (e) {\n  throw new Error(`Invalid session file - corrupted or not a Claude Code session (got: ${stdout.slice(0, 120)})`);\n}","handlingStrategy":"validation","validationCode":"const raw = await fetchText(url);\nconst trimmed = raw.trim();\nif (!trimmed.startsWith('{') && !trimmed.startsWith('[')) {\n  throw new Error('URL did not return JSON — likely an HTML error page');\n}","typeGuard":"function looksLikeSessionJson(text) {\n  const t = text.trim();\n  return (t.startsWith('{') || t.startsWith('[')) && !/^<!doctype html/i.test(t);\n}","tryCatchPattern":"try {\n  return await downloader.downloadSession(url);\n} catch (e) {\n  if (e.message.includes('Invalid session file - corrupted')) {\n    throw new Error('The link returned non-JSON content (probably expired or wrong URL)');\n  }\n  throw e;\n}","preventionTips":["Sniff the first bytes of downloads for '<' (HTML) before JSON.parse","Log a short prefix of the body when parsing fails","Validate the content-type header when the host provides one"],"tags":["json-parse","download","session-sharing","corrupt-data"],"backgroundTag":"invalid-json-response","analyzedSha":"a0851ed10c7c60463dac8cfaaca124cf32d5804d","analyzedAt":"2026-08-28T14:11:56.058Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}