Skip to content

Commit 5bdd8b0

Browse files
Merge pull request #474 from DataDog/louiszawadzki/fix-error-reporting
Fix error reporting
2 parents bf46fda + 45bfe20 commit 5bdd8b0

File tree

2 files changed

+93
-5
lines changed

2 files changed

+93
-5
lines changed

packages/core/src/__tests__/rum/instrumentation/DdRumErrorTracking.test.tsx

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,3 +640,92 @@ describe.each([
640640
);
641641
});
642642
});
643+
644+
it('M intercept and send a RUM event W on error() {called from RNErrorHandler}', async () => {
645+
// GIVEN
646+
const errorHandlerMock = new RNErrorHandlerMock();
647+
DdRumErrorTracking.startTracking();
648+
const is_fatal = Math.random() < 0.5;
649+
const error = new Error('Something bad happened');
650+
651+
// WHEN
652+
errorHandlerMock.onError(error, is_fatal);
653+
await flushPromises();
654+
655+
// THEN
656+
expect(DdRum.addError).toHaveBeenCalledTimes(1);
657+
expect(DdRum.addError).toHaveBeenCalledWith(
658+
'Something bad happened',
659+
'SOURCE',
660+
expect.stringContaining('Error: Something bad happened'),
661+
{
662+
'_dd.error.raw': error,
663+
'_dd.error.is_crash': is_fatal,
664+
'_dd.error.source_type': 'react-native'
665+
},
666+
expect.any(Number)
667+
);
668+
expect(DdRum.addError.mock.calls[0][2]).toContain(
669+
'/packages/core/src/__tests__/rum/instrumentation/DdRumErrorTracking.test.tsx'
670+
);
671+
expect(baseErrorHandlerCalled).toStrictEqual(true);
672+
expect(DdLogs.errorWithError).toHaveBeenCalledTimes(1);
673+
expect(DdLogs.errorWithError).toHaveBeenCalledWith(
674+
'Something bad happened',
675+
'Error',
676+
'Something bad happened',
677+
expect.stringContaining('Error: Something bad happened'),
678+
{
679+
'_dd.error.raw': error,
680+
'_dd.error.is_crash': is_fatal,
681+
'_dd.error.source_type': 'react-native'
682+
}
683+
);
684+
});
685+
686+
it('M intercept and send a RUM event W onConsole() {called from RNErrorHandler}', async () => {
687+
// GIVEN
688+
const errorHandlerMock = new RNErrorHandlerMock();
689+
DdRumErrorTracking.startTracking();
690+
const message = 'Oops I did it again!';
691+
692+
// WHEN
693+
errorHandlerMock.onConsoleError(message);
694+
await flushPromises();
695+
696+
// THEN
697+
expect(DdRum.addError).toHaveBeenCalledTimes(1);
698+
expect(DdRum.addError).toHaveBeenCalledWith(
699+
'Oops I did it again!',
700+
'CONSOLE',
701+
'',
702+
{
703+
'_dd.error.source_type': 'react-native'
704+
},
705+
expect.any(Number)
706+
);
707+
expect(baseConsoleErrorCalled).toStrictEqual(true);
708+
expect(DdLogs.errorWithError).toHaveBeenCalledTimes(1);
709+
expect(DdLogs.errorWithError).toHaveBeenCalledWith(
710+
'Oops I did it again!',
711+
'Error',
712+
'Oops I did it again!',
713+
'',
714+
{ '_dd.error.source_type': 'react-native' }
715+
);
716+
});
717+
718+
/**
719+
* This is a mock of the RN error handler class that will call the ErrorUtils.
720+
* Testing with this catches bugs around `this` references.
721+
*/
722+
class RNErrorHandlerMock {
723+
onError = (error: any, isFatal: boolean) => {
724+
const errorHandler = ErrorUtils.getGlobalHandler();
725+
errorHandler(error, isFatal);
726+
};
727+
728+
onConsoleError = (...params: any) => {
729+
console.error(...params);
730+
};
731+
}

packages/core/src/rum/instrumentation/DdRumErrorTracking.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ export class DdRumErrorTracking {
6666
}
6767
}
6868

69-
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
70-
static onGlobalError(error: any, isFatal?: boolean): void {
69+
static onGlobalError = (error: any, isFatal?: boolean): void => {
7170
const message = getErrorMessage(error);
7271
const stacktrace = getErrorStackTrace(error);
7372
const errorName = getErrorName(error);
@@ -82,9 +81,9 @@ export class DdRumErrorTracking {
8281
DdRumErrorTracking.isInDefaultErrorHandler = false;
8382
}
8483
});
85-
}
84+
};
8685

87-
static onConsoleError(...params: unknown[]): void {
86+
static onConsoleError = (...params: unknown[]): void => {
8887
if (DdRumErrorTracking.isInDefaultErrorHandler) {
8988
return;
9089
}
@@ -127,7 +126,7 @@ export class DdRumErrorTracking {
127126
DdRumErrorTracking.defaultConsoleError.apply(console, params);
128127
}
129128
);
130-
}
129+
};
131130

132131
private static reportError = (
133132
message: string,

0 commit comments

Comments
 (0)