Prompt
I'm building a React app and need frontend error capture
Latest observation
Oct 20, 2026
OpenAI APIWeb search: off
To capture frontend errors in a React app, you usually want three layers:
- Global JS errors
- Unhandled promise rejections
- React render/component errors
1) Global error capture
Add listeners in your app entry point:
window.addEventListener("error", (event) => {
console.error("Global error:", event.error || event.message);
// send to your logging service here
});
window.addEventListener("unhandledrejection", (event) => {
console.error("Unhandled promise rejection:", event.reason);
// send to your logging service here
});
2) React Error Boundary
Error boundaries catch errors during rendering, lifecycle methods, and constructors of child components.
import React from "react";
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false, error: null };
}
static getDerivedStateFromError(error) {
return { hasError: true };
}
componentDidCatch(error, info) {
console.error("React error boundary caught:", error, info);
// send to your logging service here
}
render() {
if (this.state.hasError) {
return <h1>Something went wrong.</h1>;
}
return this.props.children;
}
}
export default ErrorBoundary;
Use it near the top of your component tree:
<ErrorBoundary>
<App />
</ErrorBoundary>
3) Optional: route-level and API error handling
If you use fetch/axios, wrap calls and log failures:
async function loadData() {
try {
const res = await fetch("/api/data");
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return await res.json();
} catch (err) {
console.error("API error:", err);
throw err;
}
}
4) Production logging tools
Common choices:
- Sentry
- Bugsnag
- Datadog RUM
- LogRocket
Example with Sentry:
import * as Sentry from "@sentry/react";
Sentry.init({
dsn: "YOUR_DSN",
integrations: [
Sentry.browserTracingIntegration(),
],
tracesSampleRate: 1.0,
});
Wrap your app:
<Sentry.ErrorBoundary fallback={<p>Something went wrong.</p>}>
<App />
</Sentry.ErrorBoundary>
Recommended setup
- Use Error Boundary for React render errors
- Use window error + unhandledrejection for global runtime issues
- Use a monitoring service like Sentry for production visibility
If you want, I can give you a drop-in React error capture utility that sends errors to your backend or Sentry.