Skip to content

Commit

Permalink
feat(crashlytics): add custom message ability to javascript stack tra…
Browse files Browse the repository at this point in the history
…ces (#4609)

* fix: crashlytics custom js error display in firebase ui
* add test
* fix lint
  • Loading branch information
William Lauzé authored Nov 26, 2020
1 parent e1b154a commit afaa95d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
10 changes: 10 additions & 0 deletions packages/crashlytics/e2e/crashlytics.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ describe('crashlytics()', () => {
firebase.crashlytics().recordError(new Error("I'm a teapot!"));
// TODO verify stack obj
});

it('accepts optional jsErrorName', async () => {
firebase
.crashlytics()
.recordError(
new Error("I'm a teapot!"),
'This message will display in crashlytics dashboard',
);
// TODO verify stack obj
});
});

describe('sendUnsentReports()', () => {
Expand Down
14 changes: 13 additions & 1 deletion packages/crashlytics/lib/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,25 @@ import { isError, once } from '@react-native-firebase/app/lib/common';
import tracking from 'promise/setimmediate/rejection-tracking';
import StackTrace from 'stacktrace-js';

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

nativeObj.message = `${error.message}`;
nativeObj.isUnhandledRejection = isUnhandledRejection;

nativeObj.frames = [];

if (jsErrorName) {
// Option to fix crashlytics display and alerting. You can add an error name to the recordError function
nativeObj.frames.push({
src: '<unknown>',
line: 0,
col: 0,
fn: '<unknown>',
file: jsErrorName,
});
}

for (let i = 0; i < stackFrames.length; i++) {
const { columnNumber, lineNumber, fileName, functionName, source } = stackFrames[i];
let fileNameParsed = '<unknown>';
Expand Down
3 changes: 2 additions & 1 deletion packages/crashlytics/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@ export namespace FirebaseCrashlyticsTypes {
* ```
*
* @param error Expects an instance of Error; e.g. classes that extend Error will also be supported.
* @param jsErrorName Optional string containing Javascript error name
*/
recordError(error: Error): void;
recordError(error: Error, jsErrorName?: string): void;
/**
* Enqueues any unsent reports on the device to upload to Crashlytics. This method only applies if
* automatic data collection is disabled.
Expand Down
4 changes: 2 additions & 2 deletions packages/crashlytics/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ class FirebaseCrashlyticsModule extends FirebaseModule {
return this.native.setUserId(userId);
}

recordError(error) {
recordError(error, jsErrorName) {
if (isError(error)) {
StackTrace.fromError(error, { offline: true }).then(stackFrames => {
this.native.recordError(createNativeErrorObj(error, stackFrames, false));
this.native.recordError(createNativeErrorObj(error, stackFrames, false, jsErrorName));
});
} else {
console.warn(
Expand Down

1 comment on commit afaa95d

@vercel
Copy link

@vercel vercel bot commented on afaa95d Nov 26, 2020

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.