davila7/claude-code-templates · error

Failed to export session

Error message

Failed to export session

What it means

500 from POST /api/conversations/:id/download: the conversation was found but SessionSharing.exportSessionAsMarkdown threw — typically reading/parsing the session file or writing the export output. Response includes error.message with the root cause.

Source

Thrown at cli-tool/src/chats-mobile.js:509

        console.log(chalk.cyan(`📥 Exporting conversation ${conversationId} as markdown...`));

        // Export the session as markdown using SessionSharing module
        const exportResult = await this.sessionSharing.exportSessionAsMarkdown(conversationId, conversation);

        res.json({
          success: true,
          conversationId: conversationId,
          markdown: exportResult.markdown,
          filename: exportResult.filename,
          messageCount: exportResult.messageCount,
          totalMessageCount: exportResult.totalMessageCount,
          wasLimited: exportResult.wasLimited,
          timestamp: new Date().toISOString()
        });
      } catch (error) {
        console.error('Error exporting conversation:', error);
        res.status(500).json({
          error: 'Failed to export session',
          message: error.message
        });
      }
    });

    // API to get detailed analytics for a conversation
    this.app.get('/api/conversations/:id/analytics', async (req, res) => {
      try {
        const conversationId = req.params.id;
        const conversation = this.data.conversations.find(conv => conv.id === conversationId);

        if (!conversation) {
          return res.status(404).json({ error: 'Conversation not found' });
        }

        console.log(chalk.cyan(`📊 Fetching analytics for conversation ${conversationId}...`));

View on GitHub (pinned to a0851ed10c)

Solutions

  1. Read the 'message' field — it identifies I/O vs parse failures
  2. Check write permissions on the export output directory
  3. Retry when Claude Code isn't actively appending to that session
  4. Repair or remove the corrupt session file, then rebuild the index by restarting the server
Defensive patterns

Strategy: try-catch

Validate before calling

await fs.promises.access(conv.filePath, fs.constants.R_OK);
await fs.promises.access(exportDir, fs.constants.W_OK);

Try / catch

try { return await exportConversation(id); } catch (e) { const msg = e.response?.body?.message ?? e.message; throw new Error(`export failed: ${msg}`); }

Prevention

When it happens

Trigger: Export when the session .jsonl is unreadable/corrupt, the export destination isn't writable, or the session content hits an unexpected structure the exporter can't serialize.

Common situations: Read-only output directory; concurrent writes to the session; very large conversations exceeding buffers; exporter/parser version mismatch.

Related errors


AI-assisted analysis of davila7/claude-code-templates@a0851ed10c (2026-08-28). Data as JSON: /api/errors/5383d9efa084544e. Report an issue: GitHub.