Lesson 6 of 9
Error Handling - Layered Strategy
Production apps have three levels of error handling: request-level (axios interceptors), query-level (TanStack Query error state), and global (ErrorBoundary + Sentry crash reporting). Each layer catches different failure modes.
Layered Error Handling
// Layer 1: Axios interceptor - network/auth errors
// (covered in Topic 1)
// Layer 2: TanStack Query error boundary for a screen
import { QueryErrorResetBoundary } from '@tanstack/react-query';
import { ErrorBoundary } from 'react-error-boundary';
function FeedScreen() {
return (
<QueryErrorResetBoundary>
{({ reset }) => (
<ErrorBoundary
onReset={reset}
fallbackRender={({ error, resetErrorBoundary }) => (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center', padding: 32 }}>
<Text style={{ fontSize: 40, marginBottom: 16 }}>๐</Text>
<Text style={{ color: '#f1f5f9', fontSize: 18, fontWeight: '700', marginBottom: 8 }}>Something went wrong</Text>
<Text style={{ color: '#94a3b8', textAlign: 'center', marginBottom: 24 }}>{error.message}</Text>
<Pressable onPress={resetErrorBoundary} style={{ backgroundColor: '#229c6b', padding: 14, borderRadius: 10 }}>
<Text style={{ color: 'white', fontWeight: '700' }}>Try Again</Text>
</Pressable>
</View>
)}
>
<PostFeed />
</ErrorBoundary>
)}
</QueryErrorResetBoundary>
);
}
// Layer 3: Sentry for unhandled crashes
// npx expo install @sentry/react-native
import * as Sentry from '@sentry/react-native';
Sentry.init({
dsn: process.env.EXPO_PUBLIC_SENTRY_DSN,
environment: process.env.EXPO_PUBLIC_ENV ?? 'development',
debug: __DEV__,
tracesSampleRate: 0.2, // 20% of transactions traced (perf monitoring)
profilesSampleRate: 0.1,
});
// Wrap root component
export default Sentry.wrap(App);
// Manual error capture
function handlePaymentError(err: Error, context: Record<string, unknown>) {
Sentry.withScope(scope => {
scope.setTag('flow', 'payment');
scope.setContext('payment', context);
Sentry.captureException(err);
});
}
function PostFeed() { return null; }
function App() { return null; }Tip
Tip
Practice Error Handling Layered Strategy in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Categorizing API errors, UI Error Boundaries, and Sentry telemetry reporting
Practice Task
Note
Practice Task - (1) Write a working example of Error Handling Layered Strategy from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Error Handling Layered Strategy is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.
Finished this lesson?