mckaywrigley/chatbot-ui · error · Error

error.message

Error message

error.message

What it means

Thrown when a single-row SELECT on the files table fails or returns no row. Note the bug in the source: it checks if (!file) but throws error.message — when the row genuinely doesn't exist, .single() sets error (PGRST116 'JSON object requested, multiple (or no) rows returned') and data is null, so the message is the PostgREST not-found message rather than a clear 'file not found'.

Source

Thrown at db/files.ts:15

import { supabase } from "@/lib/supabase/browser-client"
import { TablesInsert, TablesUpdate } from "@/supabase/types"
import mammoth from "mammoth"
import { toast } from "sonner"
import { uploadFile } from "./storage/files"

export const getFileById = async (fileId: string) => {
  const { data: file, error } = await supabase
    .from("files")
    .select("*")
    .eq("id", fileId)
    .single()

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

  return file
}

export const getFileWorkspacesByWorkspaceId = async (workspaceId: string) => {
  const { data: workspace, error } = await supabase
    .from("workspaces")
    .select(
      `
      id,
      name,
      files (*)
    `
    )
    .eq("id", workspaceId)
    .single()

View on GitHub (pinned to 81328b61d2)

Solutions

  1. Verify the file id exists (select id from files where id = ...) with the service-role client or in the Supabase SQL editor
  2. Add a SELECT RLS policy on files for authenticated users if rows are being filtered out
  3. Fix the check to distinguish not-found from real errors (see exampleFix) and surface 'File not found' when error.code is PGRST116
  4. Guard callers like fetchedFile to handle a null/missing file gracefully

Example fix

// before
if (!file) {
  throw new Error(error.message)
}

// after
if (error || !file) {
  if (error?.code === "PGRST116" || !file) throw new Error("File not found")
  throw new Error(`Failed to fetch file: ${error.message}`)
}
Defensive patterns

Strategy: try-catch

Validate before calling

const { data } = await supabase.from("files").select("id").eq("id", fileId).maybeSingle()
if (!data) throw new Error("File not found")

Type guard

function isFileNotFound(e: unknown): boolean {
  return e instanceof Error && /no rows|not found|PGRST116/i.test(e.message)
}

Try / catch

try {
  const file = await getFileById(fileId)
} catch (e) {
  if (isFileNotFound(e)) return notFound()
  throw e
}

Prevention

When it happens

Trigger: Calling getFileById(fileId) with an id that doesn't exist, an id belonging to another user (RLS filters it out so zero rows return), or a genuine DB error (connection, permission).

Common situations: Deep-linking to a deleted file, using an anon key without RLS SELECT policies, stale fileId in client state after a re-seed of the database, or typo'd uuid.

Related errors


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