Skip to content

Commit

Permalink
feat(crashlytics): flag fatal errors for crashlytics and analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehardy committed Apr 2, 2021
1 parent 2b121fb commit c94546d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/analytics/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const ReservedEventNames = [
'ad_reward',
'app_background',
'app_clear_data',
'app_exception',
// 'app_exception',
'app_remove',
'app_store_refund',
'app_store_subscription_cancel',
Expand Down
31 changes: 30 additions & 1 deletion packages/crashlytics/lib/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
*
*/

import { firebase } from '@react-native-firebase/app';
import { isError, once } from '@react-native-firebase/app/lib/common';
import tracking from 'promise/setimmediate/rejection-tracking';
import StackTrace from 'stacktrace-js';

export const FATAL_FLAG = 'com.firebase.crashlytics.reactnative.fatal';

export function createNativeErrorObj(error, stackFrames, isUnhandledRejection, jsErrorName) {
const nativeObj = {};

Expand Down Expand Up @@ -79,8 +82,34 @@ export const setGlobalErrorHandler = once(nativeModule => {
try {
const stackFrames = await StackTrace.fromError(error, { offline: true });
await nativeModule.recordErrorPromise(createNativeErrorObj(error, stackFrames, false));
} catch (_) {

// The backend conversion scan converts the closest event to this timestamp without going over
// from the timestamp here. So the timestamp *must* be greater then the event log time.
//
// For that reason we always round up (`.ceil`) and add a second in case of latency
//
// Time is specified as seconds since start of Unix epoch as a baseline, as a string
const fatalTime = Math.ceil(new Date() / 1000) + 1 + '';

// Flag the Crashlytics backend that we have a fatal error, they will transform it
await nativeModule.setAttribute(FATAL_FLAG, fatalTime);

// Notify analytics, if it exists - throws error if not
try {
await firebase.app().analytics().logEvent(
'app_exception', // 'app_exception' is reserved but we make an exception for JS->fatal transforms
{
fatal: 1, // as in firebase-android-sdk
timestamp: fatalTime,
},
);
} catch (e) {
// This just means analytics was not present, so we could not log the analytics event
// console.log('error logging analytics app_exception: ' + e);
}
} catch (e) {
// do nothing
// console.log('error logging handling the exception: ' + e);
}
}
return originalHandler(error, fatal);
Expand Down

1 comment on commit c94546d

@vercel
Copy link

@vercel vercel bot commented on c94546d Apr 2, 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.