Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement: Make additional non-fatal error generation on JS exception optional #4904

Merged
merged 1 commit into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/crashlytics/usage/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,16 @@ React Native. You can disable Crashlytics NDK in your `firebase.json` config.
}
}
```

## Crashlytics additional non-fatal issue generation

React Native Crashlytics module is generating additional non-fatal issues on JavaScript exceptions by default. Sometimes it is not desirable behavior since it might duplicate issues and hide original exceptions logs. You can disable this behavior by setting appropriate option to false:

```json
// <project-root>/firebase.json
{
"react-native": {
"crashlytics_is_error_generation_on_js_crash_enabled": false
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ class Constants {
final static String KEY_CRASHLYTICS_NDK_ENABLED = "crashlytics_ndk_enabled";
final static String KEY_CRASHLYTICS_DEBUG_ENABLED = "crashlytics_debug_enabled";
final static String KEY_CRASHLYTICS_AUTO_COLLECTION_ENABLED = "crashlytics_auto_collection_enabled";
final static String KEY_CRASHLYTICS_IS_ERROR_GENERATION_ON_JS_CRASH_ENABLED = "crashlytics_is_error_generation_on_js_crash_enabled";
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ static boolean isCrashlyticsCollectionEnabled() {
return enabled;
}

static boolean isErrorGenerationOnJSCrashEnabled() {
boolean enabled;
ReactNativeFirebaseJSON json = ReactNativeFirebaseJSON.getSharedInstance();
ReactNativeFirebaseMeta meta = ReactNativeFirebaseMeta.getSharedInstance();
ReactNativeFirebasePreferences prefs = ReactNativeFirebasePreferences.getSharedInstance();

if (prefs.contains(KEY_CRASHLYTICS_IS_ERROR_GENERATION_ON_JS_CRASH_ENABLED)) {
enabled = prefs.getBooleanValue(KEY_CRASHLYTICS_IS_ERROR_GENERATION_ON_JS_CRASH_ENABLED, true);
} else if (json.contains(KEY_CRASHLYTICS_IS_ERROR_GENERATION_ON_JS_CRASH_ENABLED)) {
enabled = json.getBooleanValue(KEY_CRASHLYTICS_IS_ERROR_GENERATION_ON_JS_CRASH_ENABLED, true);
} else {
enabled = meta.getBooleanValue(KEY_CRASHLYTICS_IS_ERROR_GENERATION_ON_JS_CRASH_ENABLED, true);
}

return enabled;
}

@Override
public boolean onCreate() {
super.onCreate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ public Map<String, Object> getConstants() {
"isCrashlyticsCollectionEnabled",
ReactNativeFirebaseCrashlyticsInitProvider.isCrashlyticsCollectionEnabled()
);
constants.put(
"isErrorGenerationOnJSCrashEnabled",
ReactNativeFirebaseCrashlyticsInitProvider.isErrorGenerationOnJSCrashEnabled()
);
return constants;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

+ (BOOL)isCrashlyticsCollectionEnabled;

+ (BOOL)isErrorGenerationOnJSCrashEnabled;

/// Returns one or more FIRComponents that will be registered in
/// FIRApp and participate in dependency resolution and injection.
+ (NSArray<FIRComponent *> *)componentsToRegister;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

NSString *const KEY_CRASHLYTICS_DEBUG_ENABLED = @"crashlytics_debug_enabled";
NSString *const KEY_CRASHLYTICS_AUTO_COLLECTION_ENABLED = @"crashlytics_auto_collection_enabled";
NSString *const KEY_CRASHLYTICS_IS_ERROR_GENERATION_ON_JS_CRASH_ENABLED = @"crashlytics_is_error_generation_on_js_crash_enabled";

@implementation RNFBCrashlyticsInitProvider

Expand Down Expand Up @@ -52,6 +53,26 @@ + (BOOL)isCrashlyticsCollectionEnabled {
return enabled;
}

+ (BOOL)isErrorGenerationOnJSCrashEnabled {
BOOL enabled;

if ([[RNFBPreferences shared] contains:KEY_CRASHLYTICS_IS_ERROR_GENERATION_ON_JS_CRASH_ENABLED]) {
enabled = [[RNFBPreferences shared] getBooleanValue:KEY_CRASHLYTICS_IS_ERROR_GENERATION_ON_JS_CRASH_ENABLED defaultValue:YES];
DLog(@"isErrorGenerationOnJSCrashEnabled via RNFBPreferences: %d", enabled);
} else if ([[RNFBJSON shared] contains:KEY_CRASHLYTICS_IS_ERROR_GENERATION_ON_JS_CRASH_ENABLED]) {
enabled = [[RNFBJSON shared] getBooleanValue:KEY_CRASHLYTICS_IS_ERROR_GENERATION_ON_JS_CRASH_ENABLED defaultValue:YES];
DLog(@"isErrorGenerationOnJSCrashEnabled via RNFBJSON: %d", enabled);
} else {
// Note that if we're here, and the key is not set on the app's bundle, we default to "YES"
enabled = [RNFBMeta getBooleanValue:KEY_CRASHLYTICS_IS_ERROR_GENERATION_ON_JS_CRASH_ENABLED defaultValue:YES];
DLog(@"isErrorGenerationOnJSCrashEnabled via RNFBMeta: %d", enabled);
}

DLog(@"isErrorGenerationOnJSCrashEnabled: %d", enabled);

return enabled;
}

+ (NSArray<FIRComponent *> *)componentsToRegister {
return @[];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ @implementation RNFBCrashlyticsModule
- (NSDictionary *)constantsToExport {
NSMutableDictionary *constants = [NSMutableDictionary new];
constants[@"isCrashlyticsCollectionEnabled"] = @([RCTConvert BOOL:@([RNFBCrashlyticsInitProvider isCrashlyticsCollectionEnabled])]);
constants[@"isErrorGenerationOnJSCrashEnabled"] = @([RCTConvert BOOL:@([RNFBCrashlyticsInitProvider isErrorGenerationOnJSCrashEnabled])]);
return constants;
}

Expand Down
12 changes: 7 additions & 5 deletions packages/crashlytics/lib/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@ export const setGlobalErrorHandler = once(nativeModule => {
return originalHandler(error, fatal);
}

try {
const stackFrames = await StackTrace.fromError(error, { offline: true });
await nativeModule.recordErrorPromise(createNativeErrorObj(error, stackFrames, false));
} catch (_) {
// do nothing
if (nativeModule.isErrorGenerationOnJSCrashEnabled) {
try {
const stackFrames = await StackTrace.fromError(error, { offline: true });
await nativeModule.recordErrorPromise(createNativeErrorObj(error, stackFrames, false));
} catch (_) {
// do nothing
}
}
return originalHandler(error, fatal);
}
Expand Down
1 change: 1 addition & 0 deletions tests/firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"crashlytics_debug_enabled": true,
"crashlytics_disable_auto_disabler": false,
"crashlytics_auto_collection_enabled": true,
"crashlytics_is_error_generation_on_js_crash_enabled": true,

"messaging_auto_init_enabled": true,
"messaging_android_headless_task_timeout": 30000,
Expand Down