error-handling · git:20260330.dd1a8a9 · 2026-03-30 · sha256 b95aaca6bd420958

error-handling git:20260330.dd1a8a9A

Immutable. This exact content never changes and is served at /api/v1/blob/b95aaca6bd420958.

---
description: Error boundaries and loading states guidelines
globs: ["**/app/**"]
---

# Error Handling and Loading States

EVERY page in `app/` router MUST have sibling files if it does data fetching:
- `loading.tsx` (Suspense boundary)
- `error.tsx` (Error boundary — must be `'use client'`)

`loading.tsx` template:
```tsx
export default function Loading() {
  return <div className="animate-pulse">Loading...</div>
}
```

`error.tsx` template:
```tsx
'use client'
export default function Error({ error, reset }: { error: Error; reset: () => void }) {
  return (
    <div>
      <h2>Something went wrong</h2>
      <button onClick={reset}>Try again</button>
    </div>
  )
}
```

Server Action error pattern:
```typescript
export async function createItem(formData: FormData) {
  try {
    // ... operation
    return { success: true }
  } catch (error) {
    return { error: 'Failed to create item' }
  }
}
```