@datadog/mobile-react-native 1.0.0-rc5 → 1.0.0-rc8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/DatadogSDKReactNative.podspec +1 -1
  2. package/README.md +48 -17
  3. package/android/build.gradle +1 -1
  4. package/android/gradle.properties +4 -4
  5. package/ios/Sources/DdSdk.swift +6 -3
  6. package/lib/commonjs/DdSdkReactNative.js +35 -31
  7. package/lib/commonjs/DdSdkReactNative.js.map +1 -1
  8. package/lib/commonjs/DdSdkReactNativeConfiguration.js +20 -7
  9. package/lib/commonjs/DdSdkReactNativeConfiguration.js.map +1 -1
  10. package/lib/commonjs/InternalLog.js +10 -5
  11. package/lib/commonjs/InternalLog.js.map +1 -1
  12. package/lib/commonjs/ProxyConfiguration.js.map +1 -1
  13. package/lib/commonjs/SdkVerbosity.js.map +1 -1
  14. package/lib/commonjs/TimeProvider.js +72 -0
  15. package/lib/commonjs/TimeProvider.js.map +1 -0
  16. package/lib/commonjs/Timer.js +10 -23
  17. package/lib/commonjs/Timer.js.map +1 -1
  18. package/lib/commonjs/TrackingConsent.js.map +1 -1
  19. package/lib/commonjs/foundation.js +19 -54
  20. package/lib/commonjs/foundation.js.map +1 -1
  21. package/lib/commonjs/index.js +27 -19
  22. package/lib/commonjs/index.js.map +1 -1
  23. package/lib/commonjs/rum/instrumentation/DdEventsInterceptor.js +16 -11
  24. package/lib/commonjs/rum/instrumentation/DdEventsInterceptor.js.map +1 -1
  25. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js +19 -23
  26. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  27. package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js +118 -42
  28. package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js.map +1 -1
  29. package/lib/commonjs/rum/instrumentation/DdRumUserInteractionTracking.js +46 -19
  30. package/lib/commonjs/rum/instrumentation/DdRumUserInteractionTracking.js.map +1 -1
  31. package/lib/commonjs/rum/instrumentation/DdRumXhr.js +0 -4
  32. package/lib/commonjs/rum/instrumentation/NoOpEventsInterceptor.js +1 -1
  33. package/lib/commonjs/rum/instrumentation/NoOpEventsInterceptor.js.map +1 -1
  34. package/lib/commonjs/rum/instrumentation/ShallowObjectEqualityChecker.js +34 -0
  35. package/lib/commonjs/rum/instrumentation/ShallowObjectEqualityChecker.js.map +1 -0
  36. package/lib/commonjs/rum/instrumentation/TraceIdentifier.js.map +1 -1
  37. package/lib/commonjs/rum/instrumentation/URLHostParser.js +33 -0
  38. package/lib/commonjs/rum/instrumentation/URLHostParser.js.map +1 -0
  39. package/lib/commonjs/types.js.map +1 -1
  40. package/lib/commonjs/version.js +1 -1
  41. package/lib/commonjs/version.js.map +1 -1
  42. package/lib/module/DdSdkReactNative.js +33 -29
  43. package/lib/module/DdSdkReactNative.js.map +1 -1
  44. package/lib/module/DdSdkReactNativeConfiguration.js +21 -8
  45. package/lib/module/DdSdkReactNativeConfiguration.js.map +1 -1
  46. package/lib/module/InternalLog.js +11 -5
  47. package/lib/module/InternalLog.js.map +1 -1
  48. package/lib/module/ProxyConfiguration.js.map +1 -1
  49. package/lib/module/SdkVerbosity.js.map +1 -1
  50. package/lib/module/TimeProvider.js +63 -0
  51. package/lib/module/TimeProvider.js.map +1 -0
  52. package/lib/module/Timer.js +14 -23
  53. package/lib/module/Timer.js.map +1 -1
  54. package/lib/module/TrackingConsent.js.map +1 -1
  55. package/lib/module/foundation.js +16 -39
  56. package/lib/module/foundation.js.map +1 -1
  57. package/lib/module/index.js +4 -3
  58. package/lib/module/index.js.map +1 -1
  59. package/lib/module/rum/instrumentation/DdEventsInterceptor.js +12 -10
  60. package/lib/module/rum/instrumentation/DdEventsInterceptor.js.map +1 -1
  61. package/lib/module/rum/instrumentation/DdRumErrorTracking.js +19 -23
  62. package/lib/module/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  63. package/lib/module/rum/instrumentation/DdRumResourceTracking.js +113 -39
  64. package/lib/module/rum/instrumentation/DdRumResourceTracking.js.map +1 -1
  65. package/lib/module/rum/instrumentation/DdRumUserInteractionTracking.js +44 -17
  66. package/lib/module/rum/instrumentation/DdRumUserInteractionTracking.js.map +1 -1
  67. package/lib/module/rum/instrumentation/DdRumXhr.js +1 -1
  68. package/lib/module/rum/instrumentation/NoOpEventsInterceptor.js +1 -1
  69. package/lib/module/rum/instrumentation/NoOpEventsInterceptor.js.map +1 -1
  70. package/lib/module/rum/instrumentation/ShallowObjectEqualityChecker.js +25 -0
  71. package/lib/module/rum/instrumentation/ShallowObjectEqualityChecker.js.map +1 -0
  72. package/lib/module/rum/instrumentation/TraceIdentifier.js.map +1 -1
  73. package/lib/module/rum/instrumentation/URLHostParser.js +24 -0
  74. package/lib/module/rum/instrumentation/URLHostParser.js.map +1 -0
  75. package/lib/module/types.js.map +1 -1
  76. package/lib/module/version.js +1 -1
  77. package/lib/module/version.js.map +1 -1
  78. package/lib/typescript/DdSdkReactNative.d.ts +6 -6
  79. package/lib/typescript/DdSdkReactNativeConfiguration.d.ts +15 -3
  80. package/lib/typescript/InternalLog.d.ts +5 -0
  81. package/lib/typescript/TimeProvider.d.ts +20 -0
  82. package/lib/typescript/Timer.d.ts +3 -2
  83. package/lib/typescript/index.d.ts +4 -3
  84. package/lib/typescript/rum/instrumentation/DdEventsInterceptor.d.ts +1 -0
  85. package/lib/typescript/rum/instrumentation/DdRumErrorTracking.d.ts +2 -2
  86. package/lib/typescript/rum/instrumentation/DdRumResourceTracking.d.ts +19 -9
  87. package/lib/typescript/rum/instrumentation/DdRumUserInteractionTracking.d.ts +3 -3
  88. package/lib/typescript/rum/instrumentation/DdRumXhr.d.ts +13 -3
  89. package/lib/typescript/rum/instrumentation/ShallowObjectEqualityChecker.d.ts +4 -0
  90. package/lib/typescript/rum/instrumentation/URLHostParser.d.ts +6 -0
  91. package/lib/typescript/version.d.ts +1 -1
  92. package/package.json +6 -3
  93. package/src/DdSdkReactNative.tsx +116 -70
  94. package/src/DdSdkReactNativeConfiguration.tsx +22 -13
  95. package/src/InternalLog.tsx +18 -16
  96. package/src/ProxyConfiguration.tsx +5 -5
  97. package/src/SdkVerbosity.tsx +4 -4
  98. package/src/TimeProvider.tsx +64 -0
  99. package/src/Timer.ts +20 -35
  100. package/src/TrackingConsent.tsx +3 -3
  101. package/src/__mocks__/InternalLog.tsx +9 -0
  102. package/src/__tests__/DdRumWrapper.test.tsx +40 -33
  103. package/src/__tests__/DdSdkReactNative.test.tsx +712 -513
  104. package/src/__tests__/InternalLog.test.tsx +114 -117
  105. package/src/__tests__/TimeProvider.test.tsx +75 -0
  106. package/src/__tests__/Timer.test.ts +148 -57
  107. package/src/__tests__/rum/instrumentation/DdEventsInterceptor.test.tsx +151 -92
  108. package/src/__tests__/rum/instrumentation/DdRumErrorTracking.test.tsx +168 -116
  109. package/src/__tests__/rum/instrumentation/DdRumResourceTracking.test.tsx +789 -644
  110. package/src/__tests__/rum/instrumentation/DdRumUserInteractionTracking.test.tsx +239 -85
  111. package/src/__tests__/rum/instrumentation/ShallowObjectEqualityChecker.test.ts +31 -0
  112. package/src/__tests__/rum/instrumentation/TraceIdentifier.test.tsx +13 -15
  113. package/src/__tests__/rum/instrumentation/URLHostParser.test.ts +40 -0
  114. package/src/__tests__/rum/instrumentation/__utils__/XMLHttpRequestMock.ts +97 -0
  115. package/src/foundation.tsx +13 -10
  116. package/src/index.tsx +15 -4
  117. package/src/rum/instrumentation/DdEventsInterceptor.tsx +51 -35
  118. package/src/rum/instrumentation/DdRumErrorTracking.tsx +49 -44
  119. package/src/rum/instrumentation/DdRumResourceTracking.tsx +369 -234
  120. package/src/rum/instrumentation/DdRumUserInteractionTracking.tsx +72 -29
  121. package/src/rum/instrumentation/DdRumXhr.tsx +21 -11
  122. package/src/rum/instrumentation/EventsInterceptor.tsx +3 -3
  123. package/src/rum/instrumentation/NoOpEventsInterceptor.tsx +2 -2
  124. package/src/rum/instrumentation/ShallowObjectEqualityChecker.ts +24 -0
  125. package/src/rum/instrumentation/TraceIdentifier.tsx +19 -19
  126. package/src/rum/instrumentation/URLHostParser.ts +23 -0
  127. package/src/types.tsx +199 -158
  128. package/src/version.ts +1 -1
@@ -8,6 +8,9 @@ import { NativeModules } from 'react-native';
8
8
  import { DdSdkConfiguration, DdSdkType, DdLogsType, DdTraceType, DdRumType } from './types';
9
9
  import {InternalLog} from "./InternalLog"
10
10
  import {SdkVerbosity} from "./SdkVerbosity";
11
+ import {TimeProvider} from "./TimeProvider";
12
+
13
+ const timeProvider = new TimeProvider();
11
14
 
12
15
  class DdLogsWrapper implements DdLogsType {
13
16
 
@@ -39,13 +42,13 @@ class DdTraceWrapper implements DdTraceType {
39
42
 
40
43
  private nativeTrace: DdTraceType = NativeModules.DdTrace;
41
44
 
42
- startSpan(operation: string, context: object = {}, timestampMs: number = Date.now()): Promise<string> {
45
+ startSpan(operation: string, context: object = {}, timestampMs: number = timeProvider.now()): Promise<string> {
43
46
  let spanId = this.nativeTrace.startSpan(operation, context, timestampMs);
44
47
  InternalLog.log("Starting span “" + operation + "” #" + spanId, SdkVerbosity.DEBUG);
45
48
  return spanId
46
49
  }
47
50
 
48
- finishSpan(spanId: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
51
+ finishSpan(spanId: string, context: object = {}, timestampMs: number = timeProvider.now()): Promise<void> {
49
52
  InternalLog.log("Finishing span #" + spanId, SdkVerbosity.DEBUG);
50
53
  return this.nativeTrace.finishSpan(spanId, context, timestampMs);
51
54
  }
@@ -55,42 +58,42 @@ class DdRumWrapper implements DdRumType {
55
58
 
56
59
  private nativeRum: DdRumType = NativeModules.DdRum;
57
60
 
58
- startView(key: string, name: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
61
+ startView(key: string, name: string, context: object = {}, timestampMs: number = timeProvider.now()): Promise<void> {
59
62
  InternalLog.log("Starting RUM View “" + name + "” #" + key, SdkVerbosity.DEBUG);
60
63
  return this.nativeRum.startView(key, name, context, timestampMs);
61
64
  }
62
65
 
63
- stopView(key: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
66
+ stopView(key: string, context: object = {}, timestampMs: number = timeProvider.now()): Promise<void> {
64
67
  InternalLog.log("Stopping RUM View #" + key, SdkVerbosity.DEBUG);
65
68
  return this.nativeRum.stopView(key, context, timestampMs);
66
69
  }
67
70
 
68
- startAction(type: string, name: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
71
+ startAction(type: string, name: string, context: object = {}, timestampMs: number = timeProvider.now()): Promise<void> {
69
72
  InternalLog.log("Starting RUM Action “" + name + "” (" + type + ")", SdkVerbosity.DEBUG);
70
73
  return this.nativeRum.startAction(type, name, context, timestampMs);
71
74
  }
72
75
 
73
- stopAction(context: object = {}, timestampMs: number = Date.now()): Promise<void> {
76
+ stopAction(context: object = {}, timestampMs: number = timeProvider.now()): Promise<void> {
74
77
  InternalLog.log("Stopping current RUM Action", SdkVerbosity.DEBUG);
75
78
  return this.nativeRum.stopAction(context, timestampMs);
76
79
  }
77
80
 
78
- addAction(type: string, name: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
81
+ addAction(type: string, name: string, context: object = {}, timestampMs: number = timeProvider.now()): Promise<void> {
79
82
  InternalLog.log("Adding RUM Action “" + name + "” (" + type + ")", SdkVerbosity.DEBUG);
80
83
  return this.nativeRum.addAction(type, name, context, timestampMs);
81
84
  }
82
85
 
83
- startResource(key: string, method: string, url: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
86
+ startResource(key: string, method: string, url: string, context: object = {}, timestampMs: number = timeProvider.now()): Promise<void> {
84
87
  InternalLog.log("Starting RUM Resource #" + key + " " + method + ": " + url, SdkVerbosity.DEBUG);
85
88
  return this.nativeRum.startResource(key, method, url, context, timestampMs);
86
89
  }
87
90
 
88
- stopResource(key: string, statusCode: number, kind: string, size: number = -1, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
91
+ stopResource(key: string, statusCode: number, kind: string, size: number = -1, context: object = {}, timestampMs: number = timeProvider.now()): Promise<void> {
89
92
  InternalLog.log("Stopping RUM Resource #" + key + " status:" + statusCode, SdkVerbosity.DEBUG);
90
93
  return this.nativeRum.stopResource(key, statusCode, kind, size, context, timestampMs);
91
94
  }
92
95
 
93
- addError(message: string, source: string, stacktrace: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
96
+ addError(message: string, source: string, stacktrace: string, context: object = {}, timestampMs: number = timeProvider.now()): Promise<void> {
94
97
  InternalLog.log("Adding RUM Error “" + message + "”", SdkVerbosity.DEBUG);
95
98
  let updatedContext: any = context;
96
99
  updatedContext["_dd.error.source_type"] = "react-native";
package/src/index.tsx CHANGED
@@ -4,12 +4,23 @@
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
6
 
7
- import { DdLogs, DdTrace, DdRum } from './foundation';
8
-
9
7
  import { DdSdkReactNativeConfiguration } from './DdSdkReactNativeConfiguration';
10
8
  import { DdSdkReactNative } from './DdSdkReactNative';
9
+ import { InternalLog } from './InternalLog';
11
10
  import { ProxyConfiguration, ProxyType } from './ProxyConfiguration';
11
+ import { SdkVerbosity } from './SdkVerbosity';
12
12
  import { TrackingConsent } from './TrackingConsent';
13
- import { SdkVerbosity } from './SdkVerbosity'
13
+ import { DdLogs, DdTrace, DdRum } from './foundation';
14
14
 
15
- export { DdLogs, DdTrace, DdRum, DdSdkReactNativeConfiguration, DdSdkReactNative, ProxyConfiguration, ProxyType, TrackingConsent, SdkVerbosity };
15
+ export {
16
+ DdLogs,
17
+ DdTrace,
18
+ DdRum,
19
+ DdSdkReactNativeConfiguration,
20
+ DdSdkReactNative,
21
+ InternalLog,
22
+ ProxyConfiguration,
23
+ ProxyType,
24
+ TrackingConsent,
25
+ SdkVerbosity
26
+ };
@@ -4,71 +4,87 @@
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
6
 
7
- import type EventsInterceptor from './EventsInterceptor'
8
- import { DdRum } from '../../foundation'
7
+ import { InternalLog } from '../../InternalLog';
8
+ import { SdkVerbosity } from '../../SdkVerbosity';
9
+ import { DdRum } from '../../foundation';
9
10
 
10
- export const UNKNOWN_TARGET_NAME = "unknown_target"
11
- const DEBOUNCE_EVENT_THRESHOLD_IN_MS = 10
12
- const HANDLE_EVENT_APP_EXECUTION_TIME_IN_MS = 1
13
- const DD_ACTION_NAME_PROP = "dd-action-name"
11
+ import type EventsInterceptor from './EventsInterceptor';
12
+
13
+ export const UNKNOWN_TARGET_NAME = 'unknown_target';
14
+ const DEBOUNCE_EVENT_THRESHOLD_IN_MS = 10;
15
+ const HANDLE_EVENT_APP_EXECUTION_TIME_IN_MS = 1;
16
+ const DD_ACTION_NAME_PROP = 'dd-action-name';
14
17
 
15
18
  export class DdEventsInterceptor implements EventsInterceptor {
19
+ static ACTION_EVENT_DROPPED_WARN_MESSAGE =
20
+ 'An action event was dropped because either the `onPress` method arguments' +
21
+ ' were undefined or they were missing the target information.';
16
22
 
17
- private debouncingStartedTimestamp = Number.MIN_VALUE
23
+ private debouncingStartedTimestamp = Number.MIN_VALUE;
18
24
 
19
25
  interceptOnPress(...args: any[]): void {
20
26
  if (args.length > 0 && args[0] && args[0]._targetInst) {
21
- const currentTime = Date.now()
22
- const timestampDifference = Math.abs(Date.now() - this.debouncingStartedTimestamp)
27
+ const currentTime = Date.now();
28
+ const timestampDifference = Math.abs(
29
+ Date.now() - this.debouncingStartedTimestamp
30
+ );
23
31
  if (timestampDifference > DEBOUNCE_EVENT_THRESHOLD_IN_MS) {
24
- const targetNode = args[0]._targetInst
25
- this.handleTargetEvent(targetNode)
32
+ const targetNode = args[0]._targetInst;
33
+ this.handleTargetEvent(targetNode);
26
34
  // we add an approximated 1 millisecond for the execution time of the `handleTargetEvent` function
27
- this.debouncingStartedTimestamp = currentTime + HANDLE_EVENT_APP_EXECUTION_TIME_IN_MS
35
+ this.debouncingStartedTimestamp =
36
+ currentTime + HANDLE_EVENT_APP_EXECUTION_TIME_IN_MS;
28
37
  }
38
+ } else {
39
+ InternalLog.log(
40
+ DdEventsInterceptor.ACTION_EVENT_DROPPED_WARN_MESSAGE,
41
+ SdkVerbosity.WARN
42
+ );
29
43
  }
30
44
  }
31
45
 
32
46
  private handleTargetEvent(targetNode: any | null) {
33
47
  if (targetNode) {
34
48
  const resolvedTargetName = this.resolveTargetName(targetNode);
35
- DdRum.addAction(RumActionType.TAP.valueOf(), resolvedTargetName)
49
+ DdRum.addAction(RumActionType.TAP.valueOf(), resolvedTargetName);
36
50
  }
37
51
  }
38
52
 
39
53
  private resolveTargetName(targetNode: any): string {
40
- const closestActionLabel = this.findClosestActionLabel(targetNode)
54
+ const closestActionLabel = this.findClosestActionLabel(targetNode);
41
55
  if (closestActionLabel != null) {
42
- return closestActionLabel
56
+ return closestActionLabel;
43
57
  }
44
- const accessibilityLabel = targetNode.memoizedProps?.accessibilityLabel
58
+ const accessibilityLabel = targetNode.memoizedProps?.accessibilityLabel;
45
59
  if (accessibilityLabel != null) {
46
- return accessibilityLabel
60
+ return accessibilityLabel;
47
61
  }
48
- const elementTypeName = this.resolveElementTypeName(targetNode.elementType)
49
- return elementTypeName ? elementTypeName : UNKNOWN_TARGET_NAME
62
+ const elementTypeName = this.resolveElementTypeName(
63
+ targetNode.elementType
64
+ );
65
+ return elementTypeName || UNKNOWN_TARGET_NAME;
50
66
  }
51
67
 
52
68
  private resolveElementTypeName(elementType: any): string | null {
53
- let elementTypeName = null
54
- if (typeof elementType === "string") {
55
- elementTypeName = elementType
56
- } else if (elementType && typeof elementType.name === "string") {
57
- elementTypeName = elementType.name
69
+ let elementTypeName = null;
70
+ if (typeof elementType === 'string') {
71
+ elementTypeName = elementType;
72
+ } else if (elementType && typeof elementType.name === 'string') {
73
+ elementTypeName = elementType.name;
58
74
  }
59
- return elementTypeName
75
+ return elementTypeName;
60
76
  }
61
77
 
62
78
  private findClosestActionLabel(targetNode: any): string | null {
63
- let currentNode = targetNode
79
+ let currentNode = targetNode;
64
80
  while (currentNode) {
65
- const props = currentNode.memoizedProps
81
+ const props = currentNode.memoizedProps;
66
82
  if (props && props[DD_ACTION_NAME_PROP]) {
67
- return props[DD_ACTION_NAME_PROP]
83
+ return props[DD_ACTION_NAME_PROP];
68
84
  }
69
- currentNode = currentNode.return
85
+ currentNode = currentNode.return;
70
86
  }
71
- return null
87
+ return null;
72
88
  }
73
89
  }
74
90
 
@@ -77,13 +93,13 @@ export class DdEventsInterceptor implements EventsInterceptor {
77
93
  */
78
94
  export enum RumActionType {
79
95
  /** User tapped on a widget. */
80
- TAP = "TAP",
96
+ TAP = 'TAP',
81
97
  /** User scrolled a view. */
82
- SCROLL = "SCROLL",
98
+ SCROLL = 'SCROLL',
83
99
  /** User swiped on a view. */
84
- SWIPE = "SWIPE",
100
+ SWIPE = 'SWIPE',
85
101
  /** User clicked on a widget (not used on Mobile). */
86
- CLICK = "CLICK",
102
+ CLICK = 'CLICK',
87
103
  /** A custom action. */
88
- CUSTOM = "CUSTOM"
104
+ CUSTOM = 'CUSTOM'
89
105
  }
@@ -5,21 +5,20 @@
5
5
  */
6
6
 
7
7
  import type { ErrorHandlerCallback } from 'react-native';
8
- import { DdRum } from '../../foundation';
9
- import {InternalLog} from "../../InternalLog";
10
- import {SdkVerbosity} from "../../SdkVerbosity";
11
8
 
9
+ import { InternalLog } from '../../InternalLog';
10
+ import { SdkVerbosity } from '../../SdkVerbosity';
11
+ import { DdRum } from '../../foundation';
12
12
 
13
- const EMPTY_MESSAGE = "Unknown Error"
14
- const EMPTY_STACK_TRACE = ""
15
- const TYPE_SOURCE = "SOURCE"
16
- const TYPE_CONSOLE = "CONSOLE"
13
+ const EMPTY_MESSAGE = 'Unknown Error';
14
+ const EMPTY_STACK_TRACE = '';
15
+ const TYPE_SOURCE = 'SOURCE';
16
+ const TYPE_CONSOLE = 'CONSOLE';
17
17
 
18
18
  /**
19
- * Provides RUM auto-instrumentation feature to track errors as RUM events.
20
- */
19
+ * Provides RUM auto-instrumentation feature to track errors as RUM events.
20
+ */
21
21
  export class DdRumErrorTracking {
22
-
23
22
  private static isTracking = false;
24
23
 
25
24
  private static isInDefaultErrorHandler = false;
@@ -36,36 +35,41 @@ export class DdRumErrorTracking {
36
35
  static startTracking(): void {
37
36
  // extra safety to avoid wrapping the Error handler twice
38
37
  if (DdRumErrorTracking.isTracking) {
39
- InternalLog.log("Datadog SDK is already tracking errors", SdkVerbosity.WARN);
40
- return
38
+ InternalLog.log(
39
+ 'Datadog SDK is already tracking errors',
40
+ SdkVerbosity.WARN
41
+ );
42
+ return;
41
43
  }
42
44
 
43
45
  if (ErrorUtils) {
44
46
  DdRumErrorTracking.defaultErrorHandler = ErrorUtils.getGlobalHandler();
45
47
  DdRumErrorTracking.defaultConsoleError = console.error;
46
48
 
47
-
48
49
  ErrorUtils.setGlobalHandler(DdRumErrorTracking.onGlobalError);
49
50
  console.error = DdRumErrorTracking.onConsoleError;
50
51
 
51
52
  DdRumErrorTracking.isTracking = true;
52
- InternalLog.log("Datadog SDK is tracking errors", SdkVerbosity.INFO);
53
+ InternalLog.log(
54
+ 'Datadog SDK is tracking errors',
55
+ SdkVerbosity.INFO
56
+ );
53
57
  } else {
54
- InternalLog.log("Datadog SDK cannot track errors, ErrorUtils is not defined", SdkVerbosity.ERROR);
58
+ InternalLog.log(
59
+ 'Datadog SDK cannot track errors, ErrorUtils is not defined',
60
+ SdkVerbosity.ERROR
61
+ );
55
62
  }
56
-
57
63
  }
58
64
 
59
65
  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
60
66
  static onGlobalError(error: any, isFatal?: boolean): void {
61
67
  const message = DdRumErrorTracking.getErrorMessage(error);
62
68
  const stacktrace = DdRumErrorTracking.getErrorStackTrace(error);
63
- DdRum.addError(
64
- message,
65
- TYPE_SOURCE,
66
- stacktrace,
67
- { "_dd.error.is_crash": isFatal, "_dd.error.raw": error }
68
- ).then(() => {
69
+ DdRum.addError(message, TYPE_SOURCE, stacktrace, {
70
+ '_dd.error.is_crash': isFatal,
71
+ '_dd.error.raw': error
72
+ }).then(() => {
69
73
  DdRumErrorTracking.isInDefaultErrorHandler = true;
70
74
  try {
71
75
  DdRumErrorTracking.defaultErrorHandler(error, isFatal);
@@ -76,7 +80,6 @@ export class DdRumErrorTracking {
76
80
  }
77
81
 
78
82
  static onConsoleError(...params: unknown[]): void {
79
-
80
83
  if (DdRumErrorTracking.isInDefaultErrorHandler) {
81
84
  return;
82
85
  }
@@ -85,45 +88,44 @@ export class DdRumErrorTracking {
85
88
  for (let i = 0; i < params.length; i += 1) {
86
89
  const param = params[i];
87
90
  const paramStack = DdRumErrorTracking.getErrorStackTrace(param);
88
- if (paramStack != undefined && paramStack != EMPTY_STACK_TRACE) {
91
+ if (paramStack !== EMPTY_STACK_TRACE) {
89
92
  stack = paramStack;
90
93
  break;
91
94
  }
92
95
  }
93
96
 
94
- const message = params.map((param) => {
95
- if (typeof param === 'string') { return param; }
96
- else { return DdRumErrorTracking.getErrorMessage(param); }
97
- }).join(' ');
98
-
99
-
100
- DdRum.addError(
101
- message,
102
- TYPE_CONSOLE,
103
- stack
104
- ).then(() => {
97
+ const message = params
98
+ .map(param => {
99
+ if (typeof param === 'string') {
100
+ return param;
101
+ } else {
102
+ return DdRumErrorTracking.getErrorMessage(param);
103
+ }
104
+ })
105
+ .join(' ');
106
+
107
+ DdRum.addError(message, TYPE_CONSOLE, stack).then(() => {
105
108
  DdRumErrorTracking.defaultConsoleError.apply(console, params);
106
109
  });
107
-
108
110
  }
109
111
 
110
112
  private static getErrorMessage(error: any | undefined): string {
111
113
  let message = EMPTY_MESSAGE;
112
- if (error == undefined) {
114
+ if (error === undefined || error === null) {
113
115
  message = EMPTY_MESSAGE;
114
- } else if (typeof error == 'object' && "message" in error){
116
+ } else if (typeof error === 'object' && 'message' in error) {
115
117
  message = String(error.message);
116
118
  } else {
117
119
  message = String(error);
118
120
  }
119
121
 
120
- return message
122
+ return message;
121
123
  }
122
124
 
123
125
  private static getErrorStackTrace(error: any | undefined): string {
124
- let stack = EMPTY_STACK_TRACE
126
+ let stack = EMPTY_STACK_TRACE;
125
127
 
126
- if (error == undefined) {
128
+ if (error === undefined || error === null) {
127
129
  stack = EMPTY_STACK_TRACE;
128
130
  } else if (typeof error === 'string') {
129
131
  stack = EMPTY_STACK_TRACE;
@@ -134,12 +136,15 @@ export class DdRumErrorTracking {
134
136
  stack = String(error.stacktrace);
135
137
  } else if ('stack' in error) {
136
138
  stack = String(error.stack);
137
- } else if (('sourceURL' in error) && ('line' in error) && ('column' in error)) {
139
+ } else if (
140
+ 'sourceURL' in error &&
141
+ 'line' in error &&
142
+ 'column' in error
143
+ ) {
138
144
  stack = `at ${error.sourceURL}:${error.line}:${error.column}`;
139
145
  }
140
146
  }
141
147
 
142
- return stack
148
+ return stack;
143
149
  }
144
150
  }
145
-