How do React error boundaries and ASP.NET Core error responses work together to create a resilient user experience?
CRED · Full Stack Developer · 3-4-years · technical
Published
Year not specified · .NET, React, Software Engineering
Community answers
Posted by wasAsked editorial collection
A React error boundary catches render/lifecycle errors below it; it does not catch event-handler errors, async rejections, or failed HTTP responses. Handle those where the operation occurs and show a recoverable state.
Example:
try {
const response = await api.save(order, { signal });
setOrder(response);
} catch (error) {
if (!isAbort(error)) setProblem(toUserProblem(error));
}
Use a top-level boundary plus smaller boundaries around independent panels. Log a correlation ID and component context, not tokens or personal data. Map RFC 7807 errors to safe messages, preserve field validation, provide retry only for safe operations, and distinguish empty, loading, offline, forbidden, and server-error states.
Backbencher Developer · · 0 likes
They solve the same problem at different layers: failures should be contained, explained and recoverable rather than crashing the whole app.
ASP.NET Core side — consistent error responses
- Use a global exception handler (UseExceptionHandler with IExceptionHandler in .NET 8+) so unhandled exceptions become a clean 500 response without stack traces.
- Return RFC 7807 Problem Details (AddProblemDetails) for every error: type, title, status, detail, plus a traceId for support.
- Use correct status codes: 400 validation (with field errors), 401/403 auth, 404 not found, 409 conflict, 429 rate limit, 503 when a dependency is down.
React side — error boundaries
- An error boundary is a component that catches errors thrown while rendering its children and shows fallback UI instead of unmounting the whole tree.
- Place them around independent regions (a widget, a route, a sidebar) so one failure does not blank the page. react-error-boundary provides a convenient component with a reset function.
- Error boundaries do not catch errors in event handlers, async code or API calls. Those must be handled in data-fetching logic (try/catch, or TanStack Query's error state) — or rethrown during render to reach a boundary.
Making them work together:
- A shared API client parses Problem Details and maps them to typed errors: validation errors show next to form fields; 401 redirects to login; 403 shows "no access"; 409 offers to reload; 5xx shows a friendly retry message with the traceId.
- Retry idempotent GET requests automatically with backoff; never auto-retry non-idempotent writes without an idempotency key.
- Log frontend errors to the same monitoring system (Sentry, Application Insights) with the traceId, so a support ticket can be followed from the browser to the server log.
- Provide recovery actions: "Try again" resets the boundary and refetches.
Result: users see helpful, local messages; developers get correlated diagnostics; and partial failures do not take down the whole application.