ErrorBoundary
A React class component that catches JavaScript errors anywhere in its child component tree, logs those errors to the Cairn API, and displays a fallback UI.
Import
import { ErrorBoundary } from '@mpratyush54/sdk-react';
Props
interface ErrorBoundaryProps {
config?: PlatformConfig;
children: ReactNode;
fallback?: ReactNode;
}
| Prop | Type | Description |
|---|---|---|
config? |
PlatformConfig |
Cairn configuration (see PlatformProvider). Defaults to the enclosing <PlatformProvider>'s config; with neither, crashes are not reported and a warning is logged. |
children |
ReactNode |
Component tree to wrap |
fallback? |
ReactNode |
Custom error UI (defaults to a red "Something went wrong" message) |
Behaviour
- Catches errors via
componentDidCatch(error, errorInfo). - POSTs a bug report to
POST /api/sdk/bug-report(Authorization: Bearer <token>) with:category: 'react-crash'description:error.messageconsoleLogs: error message, stack trace (credential-like values redacted), and component stackbrowserInfo:userAgentand current URL
- Renders the
fallbackprop if provided, or a default error message.
Full Example
import { PlatformProvider, ErrorBoundary } from '@mpratyush54/sdk-react';
function App() {
const config = {
apiBase: 'https://platform.example.com',
token: process.env.NEXT_PUBLIC_PLATFORM_TOKEN!,
projectId: 'my-app',
};
return (
<PlatformProvider config={config}>
<ErrorBoundary config={config} fallback={<h1>Something broke!</h1>}>
<YourApp />
</ErrorBoundary>
</PlatformProvider>
);
}
Error Handling
- The bug-report POST is wrapped in a try/catch — a failure to report never crashes the app.
- Only errors in the rendering phase are caught. Event handler errors are not captured by
ErrorBoundary(usetry/catchin those cases).