From 37582bbd24aad35570ed7d68ee8fab55c6f1a86d Mon Sep 17 00:00:00 2001 From: Gitlab staging reset job Date: Mon, 21 Feb 2022 20:33:36 +0100 Subject: [PATCH] Add report in RUM browser SDK --- .../error/errorCollection.ts | 2 + .../error/trackReportError.spec.ts | 45 +++++++++++++++++++ .../error/trackReportError.ts | 22 +++++++++ 3 files changed, 69 insertions(+) create mode 100644 packages/rum-core/src/domain/rumEventsCollection/error/trackReportError.spec.ts create mode 100644 packages/rum-core/src/domain/rumEventsCollection/error/trackReportError.ts diff --git a/packages/rum-core/src/domain/rumEventsCollection/error/errorCollection.ts b/packages/rum-core/src/domain/rumEventsCollection/error/errorCollection.ts index e88c7e07fe..fdddf20146 100644 --- a/packages/rum-core/src/domain/rumEventsCollection/error/errorCollection.ts +++ b/packages/rum-core/src/domain/rumEventsCollection/error/errorCollection.ts @@ -14,6 +14,7 @@ import type { LifeCycle, RawRumEventCollectedData } from '../../lifeCycle' import { LifeCycleEventType } from '../../lifeCycle' import type { ForegroundContexts } from '../../foregroundContexts' import { trackConsoleError } from './trackConsoleError' +import { trackReportError } from './trackReportError' export interface ProvidedError { startClocks: ClocksState @@ -27,6 +28,7 @@ export function startErrorCollection(lifeCycle: LifeCycle, foregroundContexts: F trackConsoleError(errorObservable) trackRuntimeError(errorObservable) + trackReportError(errorObservable) errorObservable.subscribe((error) => lifeCycle.notify(LifeCycleEventType.RAW_ERROR_COLLECTED, { error })) diff --git a/packages/rum-core/src/domain/rumEventsCollection/error/trackReportError.spec.ts b/packages/rum-core/src/domain/rumEventsCollection/error/trackReportError.spec.ts new file mode 100644 index 0000000000..6f2576347d --- /dev/null +++ b/packages/rum-core/src/domain/rumEventsCollection/error/trackReportError.spec.ts @@ -0,0 +1,45 @@ +import type { RawError, Subscription } from '@datadog/browser-core' +import { ErrorHandling, ErrorSource, Observable, clocksNow, isChromium } from '@datadog/browser-core' +import type { Clock } from '../../../../../core/test/specHelper' +import { stubReportingObserver, mockClock } from '../../../../../core/test/specHelper' +import { trackReportError } from './trackReportError' + +describe('trackReportError', () => { + let errorObservable: Observable + let subscription: Subscription + let notifyLog: jasmine.Spy + let clock: Clock + let reportingObserverStub: { raiseReport(type: string): void; reset(): void } + + beforeEach(() => { + errorObservable = new Observable() + notifyLog = jasmine.createSpy('notifyLog') + reportingObserverStub = stubReportingObserver() + trackReportError(errorObservable) + subscription = errorObservable.subscribe(notifyLog) + clock = mockClock() + }) + + afterEach(() => { + subscription.unsubscribe() + clock.cleanup() + reportingObserverStub.reset() + }) + + it('should error report', () => { + if (!isChromium()) { + pending('no ReportingObserver support') + } + + reportingObserverStub.raiseReport('intervention') + + expect(notifyLog).toHaveBeenCalledWith({ + startClocks: clocksNow(), + message: jasmine.any(String), + stack: jasmine.any(String), + source: ErrorSource.REPORT, + handling: ErrorHandling.HANDLED, + type: 'intervention', + }) + }) +}) diff --git a/packages/rum-core/src/domain/rumEventsCollection/error/trackReportError.ts b/packages/rum-core/src/domain/rumEventsCollection/error/trackReportError.ts new file mode 100644 index 0000000000..2ba8c93f93 --- /dev/null +++ b/packages/rum-core/src/domain/rumEventsCollection/error/trackReportError.ts @@ -0,0 +1,22 @@ +import type { Observable, RawError } from '@datadog/browser-core' +import { clocksNow, ErrorHandling, ErrorSource, initReportObservable, CustomReportType } from '@datadog/browser-core' + +export function trackReportError(errorObservable: Observable) { + const subscription = initReportObservable([CustomReportType.csp_violation, CustomReportType.intervention]).subscribe( + (reportError) => + errorObservable.notify({ + startClocks: clocksNow(), + message: reportError.message, + stack: reportError.stack, + type: reportError.type, + source: ErrorSource.REPORT, + handling: ErrorHandling.HANDLED, + }) + ) + + return { + stop: () => { + subscription.unsubscribe() + }, + } +}