Skip to content

Commit

Permalink
fix(rnerrorhanlers): Don't crash if ErrorUtils are not available (#2808)
Browse files Browse the repository at this point in the history
Co-authored-by: Kryštof Woldřich <31292499+krystofwoldrich@users.noreply.github.com>
  • Loading branch information
andrew-schenk and krystofwoldrich authored Feb 9, 2023
1 parent 29cdc20 commit 58a3c6f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
10 changes: 8 additions & 2 deletions src/js/integrations/reactnativeerrorhandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/js/utils/worldwide.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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 {
__sentry_rn_v4_registered?: boolean;
__sentry_rn_v5_registered?: boolean;
HermesInternal: unknown;
Promise: unknown;
ErrorUtils?: ErrorUtils;
}

/** Get's the global object for the current JavaScript runtime */
Expand Down

0 comments on commit 58a3c6f

Please sign in to comment.