mckaywrigley/chatbot-ui · warning · Error

Messages not found

Error message

Messages not found

What it means

An app-level 'Messages not found' error from getMessagesByChatId when the select returns null. In PostgREST a successful query for a chat with zero messages returns an empty array, so null almost always indicates an actual error (RLS/auth failure or transport issue) even though the message says 'not found' — the wording is misleading.

Source

Thrown at db/messages.ts:25

    .select("*")
    .eq("id", messageId)
    .single()

  if (!message) {
    throw new Error("Message not found")
  }

  return message
}

export const getMessagesByChatId = async (chatId: string) => {
  const { data: messages } = await supabase
    .from("messages")
    .select("*")
    .eq("chat_id", chatId)

  if (!messages) {
    throw new Error("Messages not found")
  }

  return messages
}

export const createMessage = async (message: TablesInsert<"messages">) => {
  const { data: createdMessage, error } = await supabase
    .from("messages")
    .insert([message])
    .select("*")
    .single()

  if (error) {
    throw new Error(error.message)
  }

  return createdMessage
}

View on GitHub (pinned to 81328b61d2)

Solutions

  1. Destructure error too and branch on it: if (error) surface the real cause; return messages ?? [] otherwise
  2. Add/verify SELECT policy on messages for chat members
  3. Refresh the auth session before fetching chat history
  4. Validate chatId exists for the user before querying

Example fix

// before
const { data: messages } = await supabase.from("messages").select("*").eq("chat_id", chatId)
if (!messages) throw new Error("Messages not found")
return messages

// after
const { data: messages, error } = await supabase.from("messages").select("*").eq("chat_id", chatId)
if (error) throw new Error(`Failed to load messages: ${error.message} (code=${error.code})`)
return messages ?? []
Defensive patterns

Strategy: validation

Validate before calling

const { data: chat } = await supabase.from("chats").select("id").eq("id", chatId).maybeSingle()
if (!chat.data) return []

Type guard

function isMessagesNotFound(e: unknown): e is Error {
  return e instanceof Error && e.message === "Messages not found"
}

Try / catch

try { return await getMessagesByChatId(chatId) }
catch (e) {
  if (isMessagesNotFound(e)) return [] // empty chat or transient auth issue
  throw e
}

Prevention

When it happens

Trigger: Loading messages for a chat id that is invalid or hidden by RLS, or — more often — a failed query (expired JWT, missing SELECT policy) that returns data:null, which this code reports as 'Messages not found'.

Common situations: Expired Supabase session after leaving a chat open overnight shows 'Messages not found', missing SELECT policies on messages, wrong chat id after workspace switch.

Related errors


AI-assisted analysis of mckaywrigley/chatbot-ui@81328b61d2 (2026-08-27). Data as JSON: /api/errors/ba808b0f060f7b23. Report an issue: GitHub.