From 453fe1ccc36a10b6c9c30fb48d749864aa4ab49a Mon Sep 17 00:00:00 2001 From: Cecil Worsley Date: Fri, 16 Feb 2018 00:44:01 -0800 Subject: [PATCH] fix: Fixing uncatchable, crashing, exception on Android (#355) * Fixing an issue where when a dsn, without a secret, is used, the Native part of the sentry module doesn't crash in a way that prevents us from catching the error in javascript' * Switching success and error callback order in method signature * Removing console.log and "good job" string argument for successCallback --- .../main/java/io/sentry/RNSentryModule.java | 13 ++++++- lib/NativeClient.js | 37 ++++++++++++------- lib/Sentry.js | 3 +- 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/android/src/main/java/io/sentry/RNSentryModule.java b/android/src/main/java/io/sentry/RNSentryModule.java index 2aa66489cf..3dfbeb2a41 100644 --- a/android/src/main/java/io/sentry/RNSentryModule.java +++ b/android/src/main/java/io/sentry/RNSentryModule.java @@ -7,6 +7,7 @@ import com.facebook.react.ReactApplication; import com.facebook.react.bridge.Arguments; +import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.Promise; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; @@ -90,13 +91,20 @@ public Map getConstants() { } @ReactMethod - public void startWithDsnString(String dsnString, final ReadableMap options) { + public void startWithDsnString(String dsnString, final ReadableMap options, Callback successCallback, Callback errorCallback) { if (sentryClient != null) { logger.info(String.format("Already started, use existing client '%s'", dsnString)); return; } - sentryClient = Sentry.init(dsnString, new AndroidSentryClientFactory(this.getReactApplicationContext())); + try { + sentryClient = Sentry.init(dsnString, new AndroidSentryClientFactory(this.getReactApplicationContext())); + } catch (Exception e) { + logger.info(String.format("Catching on startWithDsnString, calling callback" + e.getMessage())); + errorCallback.invoke(e.getMessage()); + return; + } + androidHelper = new AndroidEventBuilderHelper(this.getReactApplicationContext()); sentryClient.addEventSendCallback(new EventSendCallback() { @Override @@ -143,6 +151,7 @@ public boolean shouldSend(Event event) { } }); logger.info(String.format("startWithDsnString '%s'", dsnString)); + successCallback.invoke(); } @ReactMethod diff --git a/lib/NativeClient.js b/lib/NativeClient.js index a848317c3f..ac2213f983 100644 --- a/lib/NativeClient.js +++ b/lib/NativeClient.js @@ -54,22 +54,31 @@ export class NativeClient { deactivateStacktraceMerging: true }; Object.assign(this.options, options); + } - RNSentry.startWithDsnString(this._dsn, this.options); - if (this.options.deactivateStacktraceMerging === false) { - this._activateStacktraceMerging(); - const eventEmitter = new NativeEventEmitter(RNSentryEventEmitter); - eventEmitter.addListener(RNSentryEventEmitter.MODULE_TABLE, moduleTable => { - try { - this._updateIgnoredModules(JSON.parse(moduleTable.payload)); - } catch (e) { - // https://github.com/getsentry/react-native-sentry/issues/241 - // under some circumstances the the JSON is not valid - // the reason for this is yet to be found - } + async customInit() { + return new Promise((resolve, reject) => { + RNSentry.startWithDsnString(this._dsn, this.options, () => { + resolve(); + }, (err) => { + reject(err); }); - } - RNSentry.setLogLevel(options.logLevel); + + if (this.options.deactivateStacktraceMerging === false) { + this._activateStacktraceMerging(); + const eventEmitter = new NativeEventEmitter(RNSentryEventEmitter); + eventEmitter.addListener(RNSentryEventEmitter.MODULE_TABLE, moduleTable => { + try { + this._updateIgnoredModules(JSON.parse(moduleTable.payload)); + } catch (e) { + // https://github.com/getsentry/react-native-sentry/issues/241 + // under some circumstances the the JSON is not valid + // the reason for this is yet to be found + } + }); + } + RNSentry.setLogLevel(options.logLevel); + }); } nativeCrash() { diff --git a/lib/Sentry.js b/lib/Sentry.js index 683198135f..f61e56de6a 100644 --- a/lib/Sentry.js +++ b/lib/Sentry.js @@ -21,7 +21,7 @@ export const SentryLog = { }; export const Sentry = { - install() { + async install() { // We have to first setup raven otherwise react-native will freeze the options // and some properties like ignoreErrors can not be mutated by raven-js Sentry._ravenClient = new RavenClient(Sentry._dsn, Sentry.options); @@ -31,6 +31,7 @@ export const Sentry = { Sentry.options.disableNativeIntegration === false ) { Sentry._nativeClient = new NativeClient(Sentry._dsn, Sentry.options); + await Sentry._nativeClient.customInit(); Sentry.eventEmitter = new NativeEventEmitter(RNSentryEventEmitter); Sentry.eventEmitter.addListener( RNSentryEventEmitter.EVENT_SENT_SUCCESSFULLY,