diff --git a/src/lib/taskLoadError.test.ts b/src/lib/taskLoadError.test.ts new file mode 100644 index 0000000..af9c82c --- /dev/null +++ b/src/lib/taskLoadError.test.ts @@ -0,0 +1,20 @@ +import { describe, expect, it } from 'vitest' +import { getTaskLoadErrorMessage } from './taskLoadError' + +describe('getTaskLoadErrorMessage', () => { + it('does not blame Supabase when the app is using local demo storage', () => { + expect(getTaskLoadErrorMessage(new Error('Browser storage is unavailable'), true)).toContain('Browser storage is unavailable') + }) + it('explains when the hosted tasks table has not been migrated', () => { + expect(getTaskLoadErrorMessage({ code: '42P01', message: 'relation "public.tasks" does not exist' })).toContain('Apply supabase/migrations/20250915000000_initial_schema.sql') + }) + it('explains RLS and permission failures', () => { + expect(getTaskLoadErrorMessage({ code: '42501', message: 'permission denied for table tasks' })).toContain('RLS policies') + }) + it('explains invalid session or API key errors', () => { + expect(getTaskLoadErrorMessage({ code: 'PGRST301', message: 'JWT expired' })).toContain('Sign out and sign in again') + }) + it('explains network failures without hiding the actual problem', () => { + expect(getTaskLoadErrorMessage(new Error('Failed to fetch'))).toContain('cannot reach Supabase') + }) +}) diff --git a/src/lib/taskLoadError.ts b/src/lib/taskLoadError.ts new file mode 100644 index 0000000..e8ead40 --- /dev/null +++ b/src/lib/taskLoadError.ts @@ -0,0 +1,25 @@ +type SupabaseTaskError = { code?: string; message?: string; details?: string; hint?: string } + +export function getTaskLoadErrorMessage(error: unknown, demoMode = false): string { + if (demoMode) { + const message = error instanceof Error ? error.message : '' + return message || 'Taskflow could not read browser storage. Enable site storage and reload the page.' + } + const value = (error && typeof error === 'object' ? error : {}) as SupabaseTaskError + const code = value.code || '' + const message = value.message || (error instanceof Error ? error.message : '') + + if (code === '42P01' || code === 'PGRST205' || /relation .*tasks.* does not exist|could not find the table .*tasks/i.test(message)) { + return 'The tasks table is missing from this Supabase project. Apply supabase/migrations/20250915000000_initial_schema.sql in the Supabase SQL Editor, then reload the app.' + } + if (code === '42501' || /permission denied|row-level security|violates row-level security/i.test(message)) { + return 'Supabase denied access to the tasks table. Check that the tasks RLS policies from the Taskflow migration are installed and that you are signed in with the correct account.' + } + if (code === 'PGRST301' || code === '401' || /invalid jwt|jwt expired|invalid token/i.test(message)) { + return 'Your Supabase session or public API key is invalid or expired. Sign out and sign in again, then verify the Vercel Supabase URL and anon/publishable key.' + } + if (/failed to fetch|networkerror|fetch failed|name_not_resolved/i.test(message)) { + return 'The browser cannot reach Supabase. Check the VITE_SUPABASE_URL in Vercel, the project status, and your network connection.' + } + return message || 'Unknown Supabase error. Verify the Vercel environment variables and Supabase project configuration.' +} diff --git a/src/pages/TasksPage.tsx b/src/pages/TasksPage.tsx index 541a71a..7c23e1e 100644 --- a/src/pages/TasksPage.tsx +++ b/src/pages/TasksPage.tsx @@ -7,6 +7,8 @@ import { TaskForm } from '../components/TaskForm' import { EmptyState } from '../components/EmptyState' import type { Task, TaskInput, TaskPriority, TaskStatus } from '../types' import { filterAndSortTasks, paginateTasks } from '../lib/taskFilters' +import { getTaskLoadErrorMessage } from '../lib/taskLoadError' +import { isDemoMode } from '../lib/supabase' export function TasksPage() { const {user}=useAuth() @@ -28,7 +30,7 @@ export function TasksPage() {
- {isLoading?