agentleFS
vibestackdevvibestackdev/vibe-stack/.cursor/rules/error-handling.mdc

vibe-stack / rules

Cursor rule8 starsChanged 8 months agoMITView on GitHub

Error boundaries, loading states, and server error handling

The file

We split this error-handling.mdc into the rules it gives an agent

3 rules. About 0 are common, 1 is distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    EVERY page in app/ router that does data fetching MUST have:

    line 10
  2. Distinctive:

    Every Server Action MUST return a typed response — never throw.

    line 83
  3. Very specific:

    NEVER ignore the error field from Supabase queries.

    line 117

≈ “Common” is an estimate: at least 1 in 5 of the 3,030 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
error-handling.mdc
---
description: Error boundaries, loading states, and server error handling
globs: ["**/app/**"]
---

# Error Handling and Loading States

## RULE 1: Every Data-Fetching Page Needs Sibling Files

EVERY page in `app/` router that does data fetching MUST have:
- `loading.tsx` (Suspense boundary — shown during streaming)
- `error.tsx` (Error boundary — must be `'use client'`)

`loading.tsx` template:
```tsx
export default function Loading() {
  return (
    <div className="flex items-center justify-center min-h-[200px]">
      <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary" />
    </div>
  )
}
```

`error.tsx` template:
```tsx
'use client'

export default function Error({ 
  error, 
  reset 
}: { 
  error: Error & { digest?: string }
  reset: () => void 
}) {
  return (
    <div className="flex flex-col items-center justify-center min-h-[200px] gap-4">
      <h2 className="text-xl font-semibold">Something went wrong</h2>
      <p className="text-muted-foreground">An unexpected error occurred.</p>
      <button 
        onClick={reset}
        className="px-4 py-2 bg-primary text-primary-foreground rounded-md"
      >
        Try again
      </button>
    </div>
  )
}
```

## RULE 2: NEVER Expose Internal Errors to Clients

Server errors, database errors, and stack traces MUST stay server-side.
Return generic messages to the client. Log the real error.

✅ CORRECT:
```typescript
try {
  const { data, error } = await supabase.from('posts').select()
  if (error) throw error
  return data
} catch (error) {
  console.error('[POSTS_FETCH_ERROR]', error) // Detailed log server-side
  return NextResponse.json(
    { error: 'Failed to fetch posts' }, // Generic message to client
    { status: 500 }
  )
}
```

❌ WRONG:
```typescript
} catch (error) {
  return NextResponse.json(
    { error: error.message }, // Leaks: "relation 'posts' does not exist"
    { status: 500 }
  )
}
```

## RULE 3: Use ActionResponse for All Server Actions

Every Server Action MUST return a typed response — never throw.

```typescript
type ActionResponse<T = void> = 
  | { success: true; data: T }
  | { success: false; error: string }

// Usage:
export async function createPost(input: unknown): Promise<ActionResponse<Post>> {
  const supabase = await createClient()
  const { data: { user } } = await supabase.auth.getUser()
  if (!user) return { success: false, error: 'Unauthorized' }

  const parsed = PostSchema.safeParse(input)
  if (!parsed.success) return { success: false, error: 'Invalid input' }

  const { data, error } = await supabase
    .from('posts')
    .insert({ ...parsed.data, user_id: user.id })
    .select()
    .single()

  if (error) {
    console.error('[CREATE_POST_ERROR]', error)
    return { success: false, error: 'Failed to create post' }
  }

  revalidatePath('/dashboard')
  return { success: true, data }
}
```

## RULE 4: Handle Supabase Errors Explicitly

NEVER ignore the `error` field from Supabase queries.

✅ CORRECT:
```typescript
const { data, error } = await supabase.from('posts').select()
if (error) {
  console.error('[SUPABASE_ERROR]', error.message, error.code)
  throw new Error('Database query failed')
}
// data is now safely typed as non-null
```

❌ WRONG:
```typescript
const { data } = await supabase.from('posts').select()
// data could be null if query failed — using it causes runtime crash
return data.map(post => post.title) // TypeError: Cannot read property 'map' of null
```

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.