Crosstalk-Solutions/project-nomad · error · Error

Failed to add message

Error message

Failed to add message

What it means

ChatService.addMessage rethrows this after logging when ChatMessage.create fails — i.e. inserting a row into chat_messages rejected. Common root causes are a missing/truncated foreign key to chat_sessions, oversized content exceeding a TEXT/VARCHAR limit, or DB unavailability.

Source

Thrown at admin/app/services/chat_service.ts:211

      // Update session's updated_at timestamp
      const session = await ChatSession.findOrFail(sessionId)
      session.updated_at = DateTime.now()
      await session.save()

      return {
        id: message.id.toString(),
        role: message.role,
        content: message.content,
        timestamp: message.created_at.toJSDate(),
      }
    } catch (error) {
      logger.error(
        `[ChatService] Failed to add message to session ${sessionId}: ${
          error instanceof Error ? error.message : error
        }`
      )
      throw new Error('Failed to add message')
    }
  }

  async deleteSession(sessionId: number) {
    try {
      const session = await ChatSession.findOrFail(sessionId)
      await session.delete()
      return { success: true }
    } catch (error) {
      logger.error(
        `[ChatService] Failed to delete session ${sessionId}: ${
          error instanceof Error ? error.message : error
        }`
      )
      throw new Error('Failed to delete chat session')
    }
  }

View on GitHub (pinned to 0bd1c6f4f9)

Solutions

  1. Check logs for '[ChatService] Failed to add message to session <id>: <cause>' for the exact DB error
  2. Verify the session exists before adding (or catch FK errors and surface 'session not found')
  3. If content can be large, ensure the column is TEXT/MEDIUMTEXT (or truncate before insert)
  4. Run pending migrations and confirm FK constraints match the model
  5. Increase/verify DB pool sizing if failures correlate with concurrency

Example fix

// before
const message = await ChatMessage.create({ ... })
// (throws 'Failed to add message' on FK violation)

// after
const session = await ChatSession.find(sessionId)
if (!session) throw new Error('Chat session not found')
const message = await ChatMessage.create({ ... })
Defensive patterns

Strategy: validation

Validate before calling

if (!(await ChatSession.find(sessionId))) throw new Error('Chat session not found')
const content = body.slice(0, MAX_MESSAGE_LEN)

Type guard

async function canAddMessage(sessionId: number, content: string): Promise<boolean> {
  return (await ChatSession.find(sessionId)) !== null && content.length <= MAX_MESSAGE_LEN
}

Try / catch

try { await chat.addMessage(...) } catch (e) { if (e.message === 'Failed to add message') { showRetryToast(); return } throw e }

Prevention

When it happens

Trigger: Adding a message to a sessionId that doesn't exist in chat_sessions (FK violation), inserting message content larger than the column allows, or inserting while the DB connection pool is exhausted.

Common situations: Posting to a session deleted in another request, very long pasted content or base64 payloads blowing past column size, migrations not run in a new deploy, or connection pool exhaustion under concurrent chat traffic.

Related errors


AI-assisted analysis of Crosstalk-Solutions/project-nomad@0bd1c6f4f9 (2026-08-27). Data as JSON: /api/errors/5859b4eda9ff99c5. Report an issue: GitHub.