{"record":{"id":"ba808b0f060f7b23","repo":"mckaywrigley/chatbot-ui","slug":"messages-not-found","errorCode":null,"errorMessage":"Messages not found","messagePattern":"Messages not found","errorType":"exception","errorClass":"Error","httpStatus":404,"severity":"warning","filePath":"db/messages.ts","lineNumber":25,"sourceCode":"    .select(\"*\")\n    .eq(\"id\", messageId)\n    .single()\n\n  if (!message) {\n    throw new Error(\"Message not found\")\n  }\n\n  return message\n}\n\nexport const getMessagesByChatId = async (chatId: string) => {\n  const { data: messages } = await supabase\n    .from(\"messages\")\n    .select(\"*\")\n    .eq(\"chat_id\", chatId)\n\n  if (!messages) {\n    throw new Error(\"Messages not found\")\n  }\n\n  return messages\n}\n\nexport const createMessage = async (message: TablesInsert<\"messages\">) => {\n  const { data: createdMessage, error } = await supabase\n    .from(\"messages\")\n    .insert([message])\n    .select(\"*\")\n    .single()\n\n  if (error) {\n    throw new Error(error.message)\n  }\n\n  return createdMessage\n}","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/mckaywrigley/chatbot-ui/blob/81328b61d2a4ab597a7a057be70e785cf756d9f8/db/messages.ts#L7-L43","documentation":"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.","triggerScenarios":"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'.","commonSituations":"Expired Supabase session after leaving a chat open overnight shows 'Messages not found', missing SELECT policies on messages, wrong chat id after workspace switch.","solutions":["Destructure error too and branch on it: if (error) surface the real cause; return messages ?? [] otherwise","Add/verify SELECT policy on messages for chat members","Refresh the auth session before fetching chat history","Validate chatId exists for the user before querying"],"exampleFix":"// before\nconst { data: messages } = await supabase.from(\"messages\").select(\"*\").eq(\"chat_id\", chatId)\nif (!messages) throw new Error(\"Messages not found\")\nreturn messages\n\n// after\nconst { data: messages, error } = await supabase.from(\"messages\").select(\"*\").eq(\"chat_id\", chatId)\nif (error) throw new Error(`Failed to load messages: ${error.message} (code=${error.code})`)\nreturn messages ?? []","handlingStrategy":"validation","validationCode":"const { data: chat } = await supabase.from(\"chats\").select(\"id\").eq(\"id\", chatId).maybeSingle()\nif (!chat.data) return []","typeGuard":"function isMessagesNotFound(e: unknown): e is Error {\n  return e instanceof Error && e.message === \"Messages not found\"\n}","tryCatchPattern":"try { return await getMessagesByChatId(chatId) }\ncatch (e) {\n  if (isMessagesNotFound(e)) return [] // empty chat or transient auth issue\n  throw e\n}","preventionTips":["Verify the chat exists before loading messages","Distinguish empty chats ([]) from query failures (error)","Refresh sessions before history loads in long-lived clients"],"tags":["not-found","messages","supabase","rls","misleading-error"],"backgroundTag":"record-not-found","analyzedSha":"81328b61d2a4ab597a7a057be70e785cf756d9f8","analyzedAt":"2026-08-27T19:24:42.689Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}