React error handler for Metric Insights Portal Pages (Cutstom Apps) with smart error classification, network interception, and an overlay UI. Catches runtime errors, unhandled promises, network failures (fetch, XHR, Axios), and React render errors β all in one provider.
- Automatic error capture β React render errors, unhandled rejections, global errors
- Network interception β Patches
fetch()andXMLHttpRequest; optional Axios instance support - Smart classification β Categorizes errors as Runtime, Network, API, Chunk Load, Render, or Unhandled Promise
- Two view modes β Friendly client view for end-users, detailed developer view with stack traces
- Network correlation β Buffers network errors and only shows the overlay when they surface as unhandled rejections
- Copy error reports β One-click text or JSON report for support/debugging
- Fully customizable β Custom fallback components, ignore patterns, error callbacks
- Zero runtime dependencies β Only requires React 18+ as a peer dependency
npm install @metricinsights/pp-error-handlerPeer dependencies: React 18+ and ReactDOM 18+ must be installed in your project.
Import the CSS file in your app entry point:
import '@metricinsights/pp-error-handler/styles.css';import { ErrorProvider } from '@metricinsights/pp-error-handler';
function App() {
return (
<ErrorProvider>
<YourApp />
</ErrorProvider>
);
}That's it! The error handler will now automatically catch and display errors.
| Prop | Type | Default | Description |
|---|---|---|---|
mode |
'client' | 'dev' | 'auto' |
'auto' |
Initial view mode. 'auto' uses NODE_ENV to decide. |
catchNetwork |
boolean |
true |
Intercept fetch() and XMLHttpRequest failures. |
axiosInstances |
any[] |
[] |
Axios instances to attach interceptors to. |
ignoreStatuses |
number[] |
[] |
HTTP status codes to ignore (e.g., [401, 404]). |
ignoreUrls |
(string | RegExp)[] |
[] |
URL patterns to exclude from interception. |
onError |
(error: CapturedError) => void |
β | Callback for every captured error (for Sentry, LogRocket, etc.). |
maxLogSize |
number |
50 |
Maximum errors to keep in the error log. |
dismissible |
boolean |
true in dev, false in prod |
Whether the overlay can be dismissed. |
fallback |
React.ComponentType<{ error: CapturedError; dismiss: () => void }> |
β | Custom component to replace the entire overlay. |
import { ErrorProvider } from '@metricinsights/pp-error-handler';
import '@metricinsights/pp-error-handler/styles.css';
import { apiClient } from './api';
function App() {
return (
<ErrorProvider
mode="auto"
catchNetwork={true}
axiosInstances={[apiClient]}
ignoreStatuses={[401, 404]}
ignoreUrls={['/health', /\/analytics\//]}
onError={(error) => {
// Send to your error tracking service
console.error('[ErrorHandler]', error.category, error.message);
}}
dismissible={true}
maxLogSize={100}
>
<YourApp />
</ErrorProvider>
);
}Access error handler context for programmatic error reporting.
import { useErrorHandler } from '@metricinsights/pp-error-handler';
function MyComponent() {
const { reportError, errorLog, clearLog, dismiss } = useErrorHandler();
const handleAction = async () => {
try {
await riskyOperation();
} catch (error) {
reportError(error as Error, { context: 'MyComponent.handleAction' });
}
};
return <button onClick={handleAction}>Do Something</button>;
}API:
| Method | Signature | Description |
|---|---|---|
reportError |
(error: Error | string, metadata?: Record<string, unknown>) => void |
Manually report an error to show the overlay. |
errorLog |
CapturedError[] |
List of all captured errors. |
clearLog |
() => void |
Clear the error log. |
dismiss |
() => void |
Dismiss the current overlay. |
Track the browser's network connectivity status.
import { useNetworkStatus } from '@metricinsights/pp-error-handler';
function NetworkIndicator() {
const { online, effectiveType, downlink, rtt } = useNetworkStatus();
return (
<span>{online ? 'Online' : 'Offline'} ({effectiveType})</span>
);
}Pass your Axios instances to ErrorProvider to intercept their errors. If no instances are provided, the Axios interceptor is not activated.
import axios from 'axios';
import { ErrorProvider } from '@metricinsights/pp-error-handler';
const apiClient = axios.create({ baseURL: '/api' });
function App() {
return (
<ErrorProvider axiosInstances={[apiClient]}>
<YourApp />
</ErrorProvider>
);
}The package exports utility functions for advanced use cases:
import {
classifyError,
getErrorMeta,
isErrorCritical,
getFriendlyError,
parseStack,
buildTextReport,
buildJsonReport,
generateErrorId,
} from '@metricinsights/pp-error-handler';
// Classify an error
const category = classifyError(new TypeError('Cannot read properties of undefined'));
// => 'RUNTIME'
// Get metadata for a category
const meta = getErrorMeta('NETWORK');
// => { label: 'Network Error', icon: 'π', color: '#f97316', tips: [...] }
// Build a copyable report from a CapturedError
const report = buildTextReport(capturedError);| Category | Description |
|---|---|
RUNTIME |
TypeError, ReferenceError, SyntaxError, RangeError |
NETWORK |
Network failures (no response, DNS, CORS) |
API |
HTTP errors (4xx, 5xx status codes) |
CHUNK |
Dynamic import / chunk loading failures |
RENDER |
React component render errors (caught by ErrorBoundary) |
UNHANDLED_PROMISE |
Unhandled promise rejections |
UNKNOWN |
Errors that don't match any other category |
Replace the default overlay with your own UI:
import { ErrorProvider } from '@metricinsights/pp-error-handler';
import type { CapturedError } from '@metricinsights/pp-error-handler';
function CustomErrorUI({ error, dismiss }: { error: CapturedError; dismiss: () => void }) {
return (
<div className="my-error-overlay">
<h1>Oops! Something went wrong</h1>
<p>{error.message}</p>
<button onClick={dismiss}>Dismiss</button>
<button onClick={() => window.location.reload()}>Refresh</button>
</div>
);
}
function App() {
return (
<ErrorProvider fallback={CustomErrorUI}>
<YourApp />
</ErrorProvider>
);
}The package ships with full TypeScript declarations. Key types:
import type {
CapturedError,
ErrorCategory,
ErrorMeta,
StackFrame,
NetworkErrorDetails,
EnvironmentInfo,
ErrorProviderProps,
ErrorContextValue,
UseErrorHandler,
ViewMode,
ProviderMode,
} from '@metricinsights/pp-error-handler';MIT