Skip to content

Commit

Permalink
fix: Fixing uncatchable, crashing, exception on Android (#355)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
cworsley4 authored and HazAT committed Feb 16, 2018
1 parent 2a13eb7 commit 453fe1c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
13 changes: 11 additions & 2 deletions android/src/main/java/io/sentry/RNSentryModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -90,13 +91,20 @@ public Map<String, Object> 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
Expand Down Expand Up @@ -143,6 +151,7 @@ public boolean shouldSend(Event event) {
}
});
logger.info(String.format("startWithDsnString '%s'", dsnString));
successCallback.invoke();
}

@ReactMethod
Expand Down
37 changes: 23 additions & 14 deletions lib/NativeClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion lib/Sentry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand Down

0 comments on commit 453fe1c

Please sign in to comment.