/** * ErrorCapture — hooks into the RN global error handler and unhandled promise * rejections to automatically forward errors to BugCollect.captureError. */ // React Native exposes ErrorUtils on the global object declare const ErrorUtils: { getGlobalHandler(): ((error: Error, isFatal?: boolean) => void) | null setGlobalHandler(handler: (error: Error, isFatal?: boolean) => void): void } // React Native / Hermes exposes onunhandledrejection on globalThis type UnhandledRejectionHandler = ((event: { reason: unknown }) => void) | null | undefined export const ErrorCapture = { start(onError: (error: unknown, meta?: Record) => void): void { // JS global error handler const prevError = typeof ErrorUtils !== 'undefined' ? ErrorUtils.getGlobalHandler() : null if (typeof ErrorUtils !== 'undefined') { ErrorUtils.setGlobalHandler((error, isFatal) => { onError(error, { isFatal: isFatal ?? false }) prevError?.(error, isFatal) }) } // Unhandled Promise rejection const g = globalThis as unknown as Record const prevUnhandled = (g['onunhandledrejection'] as UnhandledRejectionHandler) ?? null g['onunhandledrejection'] = (event: { reason: unknown }) => { onError(event.reason, { type: 'unhandledRejection' }) prevUnhandled?.(event) } }, }