Skip to content

Commit

Permalink
✨ Add error handling stack to addError
Browse files Browse the repository at this point in the history
  • Loading branch information
amortemousque committed Jun 14, 2021
1 parent 4b318de commit c7ec309
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 19 deletions.
11 changes: 11 additions & 0 deletions packages/rum-core/src/boot/rumPublicApi.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ErrorSource, ONE_SECOND, RelativeTime, getTimeStamp, display, TimeStamp } from '@datadog/browser-core'
import { toStackTraceString } from 'packages/core/src/tools/error'
import { setup, TestSetupBuilder } from '../../test/specHelper'
import { ActionType } from '../rawRumEvent.types'
import { makeRumPublicApi, RumPublicApi, RumUserConfiguration, StartRum } from './rumPublicApi'
Expand Down Expand Up @@ -257,6 +258,7 @@ describe('rum public api', () => {
{
context: undefined,
error: new Error('foo'),
handlingStack: jasmine.any(Object),
source: ErrorSource.CUSTOM,
startClocks: jasmine.any(Object),
},
Expand All @@ -278,6 +280,15 @@ describe('rum public api', () => {
expect(displaySpy).toHaveBeenCalledWith("DD_RUM.addError: Invalid source 'invalid'")
})

it('should generate an stacktrace at the upmost position of RUM call stack', () => {
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
rumPublicApi.addError(new Error('message'))

expect(addErrorSpy).toHaveBeenCalledTimes(1)
const stacktrace = toStackTraceString(addErrorSpy.calls.argsFor(0)[0].handlingStack)
expect(stacktrace).not.toContain('addError')
})

describe('save context when capturing an error', () => {
it('saves the date', () => {
const { clock } = setupBuilder.withFakeClock().build()
Expand Down
34 changes: 20 additions & 14 deletions packages/rum-core/src/boot/rumPublicApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
commonInit,
Configuration,
InternalMonitoring,
callMonitored,
createHandlingStackTrace,
} from '@datadog/browser-core'
import { ProvidedSource } from '../domain/rumEventsCollection/error/errorCollection'
import { CommonContext, User, ActionType, RumEventDomainContext } from '../rawRumEvent.types'
Expand Down Expand Up @@ -152,21 +154,25 @@ export function makeRumPublicApi<C extends RumUserConfiguration>(startRumImpl: S
rumPublicApi.addAction(name, context as Context)
},

addError: monitor((error: unknown, context?: object, source: ProvidedSource = ErrorSource.CUSTOM) => {
let checkedSource: ProvidedSource
if (source === ErrorSource.CUSTOM || source === ErrorSource.NETWORK || source === ErrorSource.SOURCE) {
checkedSource = source
} else {
display.error(`DD_RUM.addError: Invalid source '${source as string}'`)
checkedSource = ErrorSource.CUSTOM
}
addErrorStrategy({
error,
context: deepClone(context as Context),
source: checkedSource,
startClocks: clocksNow(),
addError: (error: unknown, context?: object, source: ProvidedSource = ErrorSource.CUSTOM) => {
const handlingStack = createHandlingStackTrace()
callMonitored(() => {
let checkedSource: ProvidedSource
if (source === ErrorSource.CUSTOM || source === ErrorSource.NETWORK || source === ErrorSource.SOURCE) {
checkedSource = source
} else {
display.error(`DD_RUM.addError: Invalid source '${source as string}'`)
checkedSource = ErrorSource.CUSTOM
}
addErrorStrategy({
error,
handlingStack,
context: deepClone(context as Context),
source: checkedSource,
startClocks: clocksNow(),
})
})
}),
},

addTiming: monitor((name: string) => {
addTimingStrategy(name)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ErrorHandling, ErrorSource, RelativeTime, TimeStamp } from '@datadog/browser-core'
import { createHandlingStackTrace, ErrorHandling, ErrorSource, RelativeTime, TimeStamp } from '@datadog/browser-core'
import { setup, TestSetupBuilder } from '../../../../test/specHelper'
import { RumEventType } from '../../../rawRumEvent.types'
import { LifeCycleEventType } from '../../lifeCycle'
Expand Down Expand Up @@ -29,8 +29,10 @@ describe('error collection', () => {
it('notifies a raw rum error event', () => {
const { rawRumEvents } = setupBuilder.build()
const error = new Error('foo')

addError({
error,
handlingStack: createHandlingStackTrace(),
source: ErrorSource.CUSTOM,
startClocks: { relative: 1234 as RelativeTime, timeStamp: 123456789 as TimeStamp },
})
Expand All @@ -46,6 +48,7 @@ describe('error collection', () => {
resource: undefined,
source: ErrorSource.CUSTOM,
stack: jasmine.stringMatching('Error: foo'),
handling_stack: jasmine.any(String),
type: 'Error',
handling: ErrorHandling.HANDLED,
},
Expand All @@ -65,6 +68,7 @@ describe('error collection', () => {
addError({
context: { foo: 'bar' },
error: new Error('foo'),
handlingStack: createHandlingStackTrace(),
source: ErrorSource.CUSTOM,
startClocks: { relative: 1234 as RelativeTime, timeStamp: 123456789 as TimeStamp },
})
Expand All @@ -78,6 +82,7 @@ describe('error collection', () => {
addError(
{
error: new Error('foo'),
handlingStack: createHandlingStackTrace(),
source: ErrorSource.CUSTOM,
startClocks: { relative: 1234 as RelativeTime, timeStamp: 123456789 as TimeStamp },
},
Expand All @@ -93,6 +98,7 @@ describe('error collection', () => {
addError(
{
error: new Error('foo'),
handlingStack: createHandlingStackTrace(),
source: ErrorSource.CUSTOM,
startClocks: { relative: 1234 as RelativeTime, timeStamp: 123456789 as TimeStamp },
},
Expand All @@ -108,6 +114,7 @@ describe('error collection', () => {
addError({
error: { foo: 'bar' },
source: ErrorSource.CUSTOM,
handlingStack: createHandlingStackTrace(),
startClocks: { relative: 1234 as RelativeTime, timeStamp: 123456789 as TimeStamp },
})
expect(rawRumEvents[0].domainContext).toEqual({
Expand Down Expand Up @@ -149,6 +156,7 @@ describe('error collection', () => {
},
source: ErrorSource.NETWORK,
stack: 'bar',
handling_stack: undefined,
type: 'foo',
handling: undefined,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ClocksState,
generateUUID,
ErrorHandling,
StackTrace,
} from '@datadog/browser-core'
import { CommonContext, RawRumErrorEvent, RumEventType } from '../../../rawRumEvent.types'
import { LifeCycle, LifeCycleEventType, RawRumEventCollectedData } from '../../lifeCycle'
Expand All @@ -18,6 +19,7 @@ export interface ProvidedError {
error: unknown
context?: Context
source: ProvidedSource
handlingStack: StackTrace
}

export type ProvidedSource = 'custom' | 'network' | 'source'
Expand Down Expand Up @@ -45,10 +47,10 @@ export function doStartErrorCollection(lifeCycle: LifeCycle, foregroundContexts:

return {
addError: (
{ error, startClocks, context: customerContext, source }: ProvidedError,
{ error, handlingStack, startClocks, context: customerContext, source }: ProvidedError,
savedCommonContext?: CommonContext
) => {
const rawError = computeRawError(error, startClocks, source)
const rawError = computeRawError(error, handlingStack, startClocks, source)
lifeCycle.notify(LifeCycleEventType.RAW_ERROR_COLLECTED, {
customerContext,
savedCommonContext,
Expand All @@ -58,13 +60,18 @@ export function doStartErrorCollection(lifeCycle: LifeCycle, foregroundContexts:
}
}

function computeRawError(error: unknown, startClocks: ClocksState, source: ProvidedSource): RawError {
function computeRawError(
error: unknown,
handlingStack: StackTrace,
startClocks: ClocksState,
source: ProvidedSource
): RawError {
const stackTrace = error instanceof Error ? computeStackTrace(error) : undefined
return {
startClocks,
source,
originalError: error,
...formatUnknownError(stackTrace, error, 'Provided'),
...formatUnknownError(stackTrace, error, 'Provided', handlingStack),
handling: ErrorHandling.HANDLED,
}
}
Expand All @@ -87,6 +94,7 @@ function processError(
: undefined,
source: error.source,
stack: error.stack,
handling_stack: error.handlingStack,
type: error.type,
handling: error.handling,
},
Expand Down
1 change: 1 addition & 0 deletions packages/rum-core/src/rawRumEvent.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export interface RawRumErrorEvent {
id: string
type?: string
stack?: string
handling_stack?: string
source: ErrorSource
message: string
handling?: ErrorHandling
Expand Down

0 comments on commit c7ec309

Please sign in to comment.