Skip to content

Commit b3e6810

Browse files
feat(crashlytics, native): add non-fatal exception logger for 3rd party native code use (#5015)
* Added native helper classes to handle non-fatal exceptions on the native side * Updated crashlytics doc with native helper info
1 parent 5ba603c commit b3e6810

File tree

5 files changed

+105
-0
lines changed

5 files changed

+105
-0
lines changed

docs/crashlytics/usage/index.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,37 @@ React Native Crashlytics module is generating additional non-fatal issues on Jav
233233
}
234234
}
235235
```
236+
237+
## Crashlytics non-fatal exceptions native handling
238+
239+
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:
240+
<br />
241+
### Android
242+
243+
```
244+
try {
245+
//...
246+
} catch (Exception e) {
247+
ReactNativeFirebaseCrashlyticsNativeHelper.recordNativeException(e);
248+
return null;
249+
}
250+
```
251+
252+
### iOS
253+
254+
```
255+
@try {
256+
//...
257+
} @catch (NSException *exception) {
258+
NSMutableDictionary * info = [NSMutableDictionary dictionary];
259+
[info setValue:exception.name forKey:@"ExceptionName"];
260+
[info setValue:exception.reason forKey:@"ExceptionReason"];
261+
[info setValue:exception.callStackReturnAddresses forKey:@"ExceptionCallStackReturnAddresses"];
262+
[info setValue:exception.callStackSymbols forKey:@"ExceptionCallStackSymbols"];
263+
[info setValue:exception.userInfo forKey:@"ExceptionUserInfo"];
264+
265+
NSError *error = [[NSError alloc] initWithDomain:yourdomain code:errorcode userInfo:info];
266+
[RNFBCrashlyticsNativeHelper recordNativeError:error];
267+
}
268+
269+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.invertase.firebase.crashlytics;
2+
3+
import com.google.firebase.crashlytics.FirebaseCrashlytics;
4+
5+
public class ReactNativeFirebaseCrashlyticsNativeHelper {
6+
7+
public static void recordNativeException(Throwable throwable) {
8+
FirebaseCrashlytics.getInstance().recordException(throwable);
9+
}
10+
11+
}

packages/crashlytics/ios/RNFBCrashlytics.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/* Begin PBXBuildFile section */
1010
2744B98621F45429004F8E3F /* RNFBCrashlyticsModule.m in Sources */ = {isa = PBXBuildFile; fileRef = 2744B98521F45429004F8E3F /* RNFBCrashlyticsModule.m */; };
1111
2748D82422371C1C00FC8DC8 /* RNFBCrashlyticsInitProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = 2748D82322371C1C00FC8DC8 /* RNFBCrashlyticsInitProvider.m */; };
12+
C66637BB25FA560700DCAA51 /* RNFBCrashlyticsNativeHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = C66637BA25FA560700DCAA51 /* RNFBCrashlyticsNativeHelper.m */; };
1213
/* End PBXBuildFile section */
1314

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

3437
/* Begin PBXFrameworksBuildPhase section */
@@ -57,6 +60,8 @@
5760
2744B98521F45429004F8E3F /* RNFBCrashlyticsModule.m */,
5861
2748D82222371C0900FC8DC8 /* RNFBCrashlyticsInitProvider.h */,
5962
2748D82322371C1C00FC8DC8 /* RNFBCrashlyticsInitProvider.m */,
63+
C66637BD25FA561D00DCAA51 /* RNFBCrashlyticsNativeHelper.h */,
64+
C66637BA25FA560700DCAA51 /* RNFBCrashlyticsNativeHelper.m */,
6065
);
6166
path = RNFBCrashlytics;
6267
sourceTree = "<group>";
@@ -127,6 +132,7 @@
127132
isa = PBXSourcesBuildPhase;
128133
buildActionMask = 2147483647;
129134
files = (
135+
C66637BB25FA560700DCAA51 /* RNFBCrashlyticsNativeHelper.m in Sources */,
130136
2748D82422371C1C00FC8DC8 /* RNFBCrashlyticsInitProvider.m in Sources */,
131137
2744B98621F45429004F8E3F /* RNFBCrashlyticsModule.m in Sources */,
132138
);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
/**
3+
* Copyright (c) 2016-present Invertase Limited & Contributors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this library except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
#import <Foundation/Foundation.h>
20+
21+
@interface RNFBCrashlyticsNativeHelper : NSObject
22+
23+
+ (void)recordNativeError:(NSError *)error;
24+
25+
@end
26+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
/**
3+
* Copyright (c) 2016-present Invertase Limited & Contributors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this library except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
#import <Firebase/Firebase.h>
20+
#import "RNFBCrashlyticsNativeHelper.h"
21+
22+
@implementation RNFBCrashlyticsNativeHelper
23+
24+
+ (void)recordNativeError:(NSError *)error {
25+
[[FIRCrashlytics crashlytics] recordError:error];
26+
}
27+
28+
@end

0 commit comments

Comments
 (0)