Skip to content

Commit 7a9e39f

Browse files
authored
Add defensive type checking to logError (#672)
1 parent a5de757 commit 7a9e39f

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

__tests__/index.spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ describe('Instabug Module', () => {
301301
const message = 'log';
302302
Instabug.logVerbose(message);
303303

304-
expect(logVerbose.calledOnceWithExactly(message)).toBe(true);
304+
expect(logVerbose.calledOnce).toBe(true);
305305

306306
});
307307

@@ -310,7 +310,7 @@ describe('Instabug Module', () => {
310310
const message = 'log';
311311
Instabug.logDebug(message);
312312

313-
expect(logDebug.calledOnceWithExactly(message)).toBe(true);
313+
expect(logDebug.calledOnce).toBe(true);
314314

315315
});
316316

@@ -319,7 +319,7 @@ describe('Instabug Module', () => {
319319
const message = 'log';
320320
Instabug.logInfo(message);
321321

322-
expect(logInfo.calledOnceWithExactly(message)).toBe(true);
322+
expect(logInfo.calledOnce).toBe(true);
323323

324324
});
325325

@@ -328,7 +328,7 @@ describe('Instabug Module', () => {
328328
const message = 'log';
329329
Instabug.logWarn(message);
330330

331-
expect(logWarn.calledOnceWithExactly(message)).toBe(true);
331+
expect(logWarn.calledOnce).toBe(true);
332332

333333
});
334334

@@ -337,7 +337,7 @@ describe('Instabug Module', () => {
337337
const message = 'log';
338338
Instabug.logError(message);
339339

340-
expect(logError.calledOnceWithExactly(message)).toBe(true);
340+
expect(logError.calledOnce).toBe(true);
341341

342342
});
343343

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from 'react-native';
77
let { Instabug } = NativeModules;
88
import IBGEventEmitter from './utils/IBGEventEmitter';
9-
import InstabugUtils from './utils/InstabugUtils';
9+
import InstabugUtils, { stringifyIfNotString } from './utils/InstabugUtils';
1010
import InstabugConstants from './utils/InstabugConstants';
1111
import Report from './models/Report';
1212
import BugReporting from './modules/BugReporting';
@@ -398,6 +398,7 @@ const InstabugModule = {
398398
*/
399399
logVerbose(message) {
400400
if (!message) return;
401+
message = stringifyIfNotString(message)
401402
Instabug.logVerbose(message);
402403
},
403404

@@ -416,6 +417,7 @@ const InstabugModule = {
416417
*/
417418
logInfo(message) {
418419
if (!message) return;
420+
message = stringifyIfNotString(message)
419421
Instabug.logInfo(message);
420422
},
421423

@@ -434,6 +436,7 @@ const InstabugModule = {
434436
*/
435437
logDebug(message) {
436438
if (!message) return;
439+
message = stringifyIfNotString(message)
437440
Instabug.logDebug(message);
438441
},
439442

@@ -452,6 +455,7 @@ const InstabugModule = {
452455
*/
453456
logError(message) {
454457
if (!message) return;
458+
message = stringifyIfNotString(message)
455459
Instabug.logError(message);
456460
},
457461

@@ -470,6 +474,7 @@ const InstabugModule = {
470474
*/
471475
logWarn(message) {
472476
if (!message) return;
477+
message = stringifyIfNotString(message)
473478
Instabug.logWarn(message);
474479
},
475480

jest/mockInstabugUtils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ jest.mock('../utils/InstabugUtils', () => {
55
setOnReportHandler: jest.fn(),
66
isOnReportHandlerSet: jest.fn(),
77
getActiveRouteName: jest.fn(),
8+
stringifyIfNotString: jest.fn(),
89
getStackTrace: jest.fn(() => 'javascriptStackTrace')
910
}
1011
});

utils/InstabugUtils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ export const captureJsErrors = () => {
100100
global.ErrorUtils.setGlobalHandler(errorHandler);
101101
};
102102

103+
export const stringifyIfNotString = input => {
104+
return typeof input === 'string' ? input : JSON.stringify(input);
105+
};
106+
103107
export default {
104108
parseErrorStack,
105109
captureJsErrors,
@@ -108,4 +112,5 @@ export default {
108112
getActiveRouteName,
109113
getFullRoute,
110114
getStackTrace,
115+
stringifyIfNotString,
111116
};

0 commit comments

Comments
 (0)