Cursor rule
.cursor/rules/astro-error-handling.mdcError handling patterns and best practices for Astro components
Cursor rules
Quality
58/100
Scores the file, not the repository.Length
630 words
13 headings · 6 code blocksRepository
0
— · pushed 262 days agoLast changed
3 days ago
First indexed 3 days ago.123456# Astro Error Handling Patterns78## Frontmatter Error Handling910### Individual Operation Error Handling11Wrap each async operation in its own try-catch to isolate failures:1213```astro14---15let userData: { user: any; error?: string } = { user: null };16let postsData: { posts: any[]; error?: string } = { posts: [] };17let commentsData: { comments: any[]; error?: string } = { comments: [] };1819if (db) {20 try {21 const service = DataService.getInstance(db);2223 // Each operation isolated24 try {25 const user = await service.getUser(userId);26 userData = { user };27 } catch (err) {28 console.error('Error fetching user:', err);29 userData = { user: null, error: err instanceof Error ? err.message : 'User error' };30 }3132 try {33 const posts = await service.getPosts(userId);34 postsData = { posts };35 } catch (err) {36 console.error('Error fetching posts:', err);37 postsData = { posts: [], error: err instanceof Error ? err.message : 'Posts error' };38 }3940 try {41 const comments = await service.getComments(userId);42 commentsData = { comments };43 } catch (err) {44 console.error('Error fetching comments:', err);45 commentsData = { comments: [], error: err instanceof Error ? err.message : 'Comments error' };46 }4748 } catch (err) {49 console.error('Service initialization error:', err);50 }51}52---53```5455## Template Error Display5657### Consistent Error UI Pattern58```astro59<div class="section">60 {data.error ? (61 <p class="text-red-500">Error: {data.error}</p>62 ) : data.items.length > 0 ? (63 <div>64 {data.items.map((item) => (65 <div class="item">{item.name}</div>66 ))}67 </div>68 ) : (69 <p class="text-gray-500">No data available</p>70 )}71</div>72```7374### Error State Classes75Use consistent Tailwind classes for error states:76- `text-red-500` for error messages77- `text-gray-500` for empty states78- `text-yellow-600` for warning states7980## Error Logging Strategy8182### Server-Side Logging83```astro84---85try {86 const result = await service.fetchData();87 data = { result };88} catch (err) {89 // Log detailed error for debugging90 console.error('Detailed error context:', {91 error: err,92 symbol,93 timestamp: new Date().toISOString(),94 operation: 'fetchData'95 });9697 // Provide user-friendly error message98 const userMessage = err instanceof Error ? err.message : 'Unable to load data';99 data = { result: null, error: userMessage };100}101---102```103104## Database Error Handling105106### Connection Availability107```astro108---109const db = (Astro.locals.runtime?.env?.DB as D1Database) || null;110111if (!db) {112 console.warn('Database not available in current environment');113 data = { items: [], error: 'Database not available' };114} else {115 // Proceed with database operations116}117---118```119120### Service Layer Error Propagation121```astro122---123try {124 const service = OptionsService.getInstance(db);125 const result = await service.fetchData(symbol);126 data = { result };127} catch (err) {128 if (err instanceof Error) {129 // Different handling based on error type130 if (err.message.includes('rate limit')) {131 data = { result: null, error: 'API rate limit exceeded. Please try again later.' };132 } else if (err.message.includes('not found')) {133 data = { result: null, error: `Symbol ${symbol} not found.` };134 } else {135 data = { result: null, error: 'Unable to fetch data. Please try again.' };136 }137 } else {138 data = { result: null, error: 'An unexpected error occurred.' };139 }140}141---142```143144## Error Recovery Patterns145146### Graceful Degradation147```astro148---149// Primary data source150let primaryData: { items: any[]; error?: string } = { items: [] };151// Fallback data source152let fallbackData: { items: any[]; error?: string } = { items: [] };153154try {155 const primary = await service.getPrimaryData();156 primaryData = { items: primary };157} catch (err) {158 console.error('Primary data source failed:', err);159 primaryData = { items: [], error: 'Primary source unavailable' };160161 // Try fallback162 try {163 const fallback = await service.getFallbackData();164 fallbackData = { items: fallback };165 } catch (fallbackErr) {166 console.error('Fallback data source failed:', fallbackErr);167 fallbackData = { items: [], error: 'All data sources unavailable' };168 }169}170171// Use the best available data172const displayData = primaryData.items.length > 0 ? primaryData : fallbackData;173---174```
Also in sferg989/fergfo.om
Diff this repo’s formatsOne repository carrying more than one format is the comparison this product exists for: does anyone actually write different content in each file, or is one a copy of the other?
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| sferg989/fergfo.om.cursor/rules/astro-component-structure.mdc · 0 | Cursor rules | stylearchui | 58/100 | 3 days ago | |
| sferg989/fergfo.om.cursor/rules/astro-database-integration.mdc · 0 | Cursor rules | database | 50/100 | 3 days ago | |
| sferg989/fergfo.om.cursor/rules/astro-performance-patterns.mdc · 0 | Cursor rules | buildstyledatabaseperformance | 62/100 | 3 days ago | |
| sferg989/fergfo.om.cursor/rules/astro-ssr-patterns.mdc · 0 | Cursor rules | style | 49/100 | 3 days ago | |
| sferg989/fergfo.om.cursor/rules/ts-typecheck.mdc · 0 | Cursor rules | do-not | 23/100 | 3 days ago | |
| sferg989/fergfo.om.cursor/rules/ts.mdc · 0 | Cursor rules | styletypesdo-notdocs | 55/100 | 3 days ago | |
| sferg989/fergfo.om.cursorrules · 0 | .cursorrules | do-not | 49/100 | 3 days ago | |
| sferg989/fergfo.omCLAUDE.md · 0 | CLAUDE.md | setupbuildteststyle+7 | 89/100 | 3 days ago |
Diff against .cursor/rules/astro-component-structure.mdc Diff against .cursor/rules/astro-database-integration.mdc Diff against .cursor/rules/astro-performance-patterns.mdc Diff against .cursor/rules/astro-ssr-patterns.mdc Diff against .cursor/rules/ts-typecheck.mdc Diff against .cursor/rules/ts.mdc Diff against .cursorrules Diff against CLAUDE.md
Similar configs
Same format, overlapping stack, ranked by quality.
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| hiromaily/go-crypto-wallet.cursor/rules/typescript.mdc · 126 | Cursor rules | setupbuildtestlint-format+6 | 100/100 | 3 days ago | |
| TechSquidTV/Hermes.cursor/rules/10-hermes-api.mdc · 45 | Cursor rules | testlint-formatstylearch+5 | 100/100 | 3 days ago | |
| markstev/mark-starter.cursor/rules/frontend.mdc · 0 | Cursor rules | setuptestlint-formatstyle+6 | 99/100 | 3 days ago | |
| Allymahmoud/case-intake-platform.cursor/rules/frontend.mdc · 0 | Cursor rules | setuptestlint-formatstyle+7 | 99/100 | 3 days ago | |
| dodgecfr/combatfilms-webapp.cursor/rules/frontend.mdc · 0 | Cursor rules | setuptestlint-formatstyle+7 | 99/100 | 3 days ago | |
| deifos/clipmira-subtitles.cursor/rules/frontend.mdc · 1 | Cursor rules | setuptestlint-formatstyle+7 | 99/100 | 3 days ago | |
| langflow-ai/langflow.cursor/rules/docs_development.mdc · 153k | Cursor rules | setupbuildtestlint-format+7 | 97/100 | 3 days ago | |
| TechSquidTV/Hermes.cursor/rules/20-hermes-api-tests.mdc · 45 | Cursor rules | teststyletesting-strategysecurity+3 | 97/100 | 3 days ago |
