diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ac1afc867..d4e8041cbc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ - Collect modules script for XCode builds supports NODE_BINARY to set path to node executable ([#2805](https://github.com/getsentry/sentry-react-native/pull/2805)) +### Fixes + +- React Native Error Handlers Integration doesn't crash if ErrorUtils are not available ([#2808](https://github.com/getsentry/sentry-react-native/pull/2808)) + ### Dependencies - Bump Android SDK from v6.12.1 to v6.13.1 ([#2790](https://github.com/getsentry/sentry-react-native/pull/2790), [#2809](https://github.com/getsentry/sentry-react-native/pull/2809)) diff --git a/src/js/integrations/reactnativeerrorhandlers.ts b/src/js/integrations/reactnativeerrorhandlers.ts index bd9fb231ca..d80e232316 100644 --- a/src/js/integrations/reactnativeerrorhandlers.ts +++ b/src/js/integrations/reactnativeerrorhandlers.ts @@ -161,11 +161,17 @@ export class ReactNativeErrorHandlers implements Integration { if (this._options.onerror) { let handlingFatal = false; + const errorUtils = RN_GLOBAL_OBJ.ErrorUtils; + if (!errorUtils) { + logger.warn('ErrorUtils not found. Can be caused by different environment for example react-native-web.'); + return; + } + const defaultHandler = - ErrorUtils.getGlobalHandler && ErrorUtils.getGlobalHandler(); + errorUtils.getGlobalHandler && errorUtils.getGlobalHandler(); // eslint-disable-next-line @typescript-eslint/no-explicit-any - ErrorUtils.setGlobalHandler(async (error: any, isFatal?: boolean) => { + errorUtils.setGlobalHandler(async (error: any, isFatal?: boolean) => { // We want to handle fatals, but only in production mode. const shouldHandleFatal = isFatal && !__DEV__; if (shouldHandleFatal) { diff --git a/src/js/utils/worldwide.ts b/src/js/utils/worldwide.ts index 76ddeaf9bb..59a3567fdd 100644 --- a/src/js/utils/worldwide.ts +++ b/src/js/utils/worldwide.ts @@ -1,5 +1,6 @@ import type { InternalGlobal } from '@sentry/utils'; import { GLOBAL_OBJ } from '@sentry/utils'; +import type { ErrorUtils } from 'react-native/types'; /** Internal Global object interface with common and Sentry specific properties */ export interface ReactNativeInternalGlobal extends InternalGlobal { @@ -7,6 +8,7 @@ export interface ReactNativeInternalGlobal extends InternalGlobal { __sentry_rn_v5_registered?: boolean; HermesInternal: unknown; Promise: unknown; + ErrorUtils?: ErrorUtils; } /** Get's the global object for the current JavaScript runtime */