| name | error-handling |
| description | Error handling patterns and best practices. Use when implementing try/catch blocks, handling async errors, showing error messages, or managing error states in UI. Triggers on error, try, catch, exception, throw, fail, failure, error handling, error boundary, useAsyncCall, toast, fallback, error state. |
| allowed-tools | Read, Grep, Glob |
Error Handling
Best practices for error handling in applications.
Core Principles
- Use try/catch blocks for async operations that might fail
- Provide appropriate error messages and fallbacks
- Use
useAsyncCall hook for operations needing loading/error states
- Never swallow errors silently
Quick Reference
Basic Try/Catch
async function fetchData() {
try {
const result = await apiCall();
return result;
} catch (error) {
console.error('Failed to fetch data:', error);
throw error;
}
}
With Fallback Value
async function fetchDataWithFallback() {
try {
const result = await apiCall();
return result;
} catch (error) {
console.error('Failed to fetch, using fallback:', error);
return defaultValue;
}
}
Using Async Call Hook
import { useAsyncCall } from '@{scope}/kit/src/hooks/useAsyncCall';
function MyComponent() {
const { run, isLoading, error, result } = useAsyncCall(
async () => {
return await fetchData();
},
{
onError: (e) => {
Toast.error({ title: 'Failed to load data' });
},
}
);
if (error) {
return <ErrorView error={error} onRetry={run} />;
}
return <DataView data={result} loading={isLoading} />;
}
User-Facing Errors
async function submitForm(data: FormData) {
try {
await api.submit(data);
Toast.success({ title: 'Submitted successfully' });
} catch (error) {
Toast.error({
title: 'Submission failed',
message: getUserFriendlyMessage(error),
});
console.error('Form submission error:', error);
}
}
Anti-Patterns
Silent Error Swallowing
async function badExample() {
try {
await riskyOperation();
} catch (error) {
}
}
async function goodExample() {
try {
await riskyOperation();
} catch (error) {
console.error('Operation failed:', error);
}
}
Missing Error State in UI
function BadComponent() {
const { data } = useQuery();
return <View>{data}</View>;
}
function GoodComponent() {
const { data, isLoading, error } = useQuery();
if (isLoading) return <Loading />;
if (error) return <Error error={error} />;
return <View>{data}</View>;
}
Detailed Guide
For comprehensive error handling patterns and examples, see error-handling.md.
Topics covered:
- Core principles
- Error handling patterns (try/catch, fallbacks, hooks)
- Error boundaries for React
- Error types (network, validation, user-facing)
- Anti-patterns to avoid
- Error handling checklist
Checklist
Related Skills
/coding-patterns - General coding patterns and promise handling
/sentry-analysis - Sentry error analysis and fixes