Skip to content

Commit

Permalink
feat(crashlytics, native): add non-fatal exception logger for 3rd par…
Browse files Browse the repository at this point in the history
…ty native code use (#5015)

* Added native helper classes to handle non-fatal exceptions on the native side
* Updated crashlytics doc with native helper info
  • Loading branch information
powerserg17-bunch authored Mar 13, 2021
1 parent 5ba603c commit b3e6810
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
34 changes: 34 additions & 0 deletions docs/crashlytics/usage/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,37 @@ React Native Crashlytics module is generating additional non-fatal issues on Jav
}
}
```

## Crashlytics non-fatal exceptions native handling

In case you need to log non-fatal (handled) exceptions on the native side (e.g from `try catch` block), you may use the following static methods:
<br />
### Android

```
try {
//...
} catch (Exception e) {
ReactNativeFirebaseCrashlyticsNativeHelper.recordNativeException(e);
return null;
}
```

### iOS

```
@try {
//...
} @catch (NSException *exception) {
NSMutableDictionary * info = [NSMutableDictionary dictionary];
[info setValue:exception.name forKey:@"ExceptionName"];
[info setValue:exception.reason forKey:@"ExceptionReason"];
[info setValue:exception.callStackReturnAddresses forKey:@"ExceptionCallStackReturnAddresses"];
[info setValue:exception.callStackSymbols forKey:@"ExceptionCallStackSymbols"];
[info setValue:exception.userInfo forKey:@"ExceptionUserInfo"];
NSError *error = [[NSError alloc] initWithDomain:yourdomain code:errorcode userInfo:info];
[RNFBCrashlyticsNativeHelper recordNativeError:error];
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.invertase.firebase.crashlytics;

import com.google.firebase.crashlytics.FirebaseCrashlytics;

public class ReactNativeFirebaseCrashlyticsNativeHelper {

public static void recordNativeException(Throwable throwable) {
FirebaseCrashlytics.getInstance().recordException(throwable);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
2744B98621F45429004F8E3F /* RNFBCrashlyticsModule.m in Sources */ = {isa = PBXBuildFile; fileRef = 2744B98521F45429004F8E3F /* RNFBCrashlyticsModule.m */; };
2748D82422371C1C00FC8DC8 /* RNFBCrashlyticsInitProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = 2748D82322371C1C00FC8DC8 /* RNFBCrashlyticsInitProvider.m */; };
C66637BB25FA560700DCAA51 /* RNFBCrashlyticsNativeHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = C66637BA25FA560700DCAA51 /* RNFBCrashlyticsNativeHelper.m */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand All @@ -29,6 +30,8 @@
2744B98521F45429004F8E3F /* RNFBCrashlyticsModule.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = RNFBCrashlyticsModule.m; path = RNFBCrashlytics/RNFBCrashlyticsModule.m; sourceTree = SOURCE_ROOT; };
2748D82222371C0900FC8DC8 /* RNFBCrashlyticsInitProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNFBCrashlyticsInitProvider.h; sourceTree = "<group>"; };
2748D82322371C1C00FC8DC8 /* RNFBCrashlyticsInitProvider.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNFBCrashlyticsInitProvider.m; sourceTree = "<group>"; };
C66637BA25FA560700DCAA51 /* RNFBCrashlyticsNativeHelper.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNFBCrashlyticsNativeHelper.m; sourceTree = "<group>"; };
C66637BD25FA561D00DCAA51 /* RNFBCrashlyticsNativeHelper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNFBCrashlyticsNativeHelper.h; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -57,6 +60,8 @@
2744B98521F45429004F8E3F /* RNFBCrashlyticsModule.m */,
2748D82222371C0900FC8DC8 /* RNFBCrashlyticsInitProvider.h */,
2748D82322371C1C00FC8DC8 /* RNFBCrashlyticsInitProvider.m */,
C66637BD25FA561D00DCAA51 /* RNFBCrashlyticsNativeHelper.h */,
C66637BA25FA560700DCAA51 /* RNFBCrashlyticsNativeHelper.m */,
);
path = RNFBCrashlytics;
sourceTree = "<group>";
Expand Down Expand Up @@ -127,6 +132,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
C66637BB25FA560700DCAA51 /* RNFBCrashlyticsNativeHelper.m in Sources */,
2748D82422371C1C00FC8DC8 /* RNFBCrashlyticsInitProvider.m in Sources */,
2744B98621F45429004F8E3F /* RNFBCrashlyticsModule.m in Sources */,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
/**
* Copyright (c) 2016-present Invertase Limited & Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this library except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#import <Foundation/Foundation.h>

@interface RNFBCrashlyticsNativeHelper : NSObject

+ (void)recordNativeError:(NSError *)error;

@end

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
/**
* Copyright (c) 2016-present Invertase Limited & Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this library except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#import <Firebase/Firebase.h>
#import "RNFBCrashlyticsNativeHelper.h"

@implementation RNFBCrashlyticsNativeHelper

+ (void)recordNativeError:(NSError *)error {
[[FIRCrashlytics crashlytics] recordError:error];
}

@end

1 comment on commit b3e6810

@vercel
Copy link

@vercel vercel bot commented on b3e6810 Mar 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.