Skip to content

Commit

Permalink
Add report in RUM browser SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
amortemousque committed Feb 25, 2022
1 parent fd69a7d commit 37582bb
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }))

Expand Down
Original file line number Diff line number Diff line change
@@ -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<RawError>
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',
})
})
})
Original file line number Diff line number Diff line change
@@ -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<RawError>) {
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()
},
}
}

0 comments on commit 37582bb

Please sign in to comment.