-
Notifications
You must be signed in to change notification settings - Fork 179
⚗️ Angular- Add error handling integration #4358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6d4d4a2
4dd19d0
a04aaa4
c76b435
bc1dce2
a463281
35cc95d
67fc656
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { initializeAngularPlugin } from '../../../test/initializeAngularPlugin' | ||
| import { addAngularError } from './addAngularError' | ||
|
|
||
| describe('addAngularError', () => { | ||
| it('delegates the error to addError', () => { | ||
| const addErrorSpy = jasmine.createSpy() | ||
| initializeAngularPlugin({ | ||
| addError: addErrorSpy, | ||
| }) | ||
| const originalError = new Error('error message') | ||
|
|
||
| addAngularError(originalError) | ||
|
|
||
| expect(addErrorSpy).toHaveBeenCalledOnceWith({ | ||
| error: originalError, | ||
| handlingStack: jasmine.any(String), | ||
| startClocks: jasmine.any(Object), | ||
| context: { | ||
| framework: 'angular', | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| it('should merge dd_context from the original error with angular error context', () => { | ||
| const addErrorSpy = jasmine.createSpy() | ||
| initializeAngularPlugin({ | ||
| addError: addErrorSpy, | ||
| }) | ||
| const originalError = new Error('error message') | ||
| ;(originalError as any).dd_context = { component: 'UserList', param: 42 } | ||
|
|
||
| addAngularError(originalError) | ||
|
|
||
| expect(addErrorSpy).toHaveBeenCalledWith( | ||
| jasmine.objectContaining({ | ||
| error: originalError, | ||
| context: { | ||
| framework: 'angular', | ||
| component: 'UserList', | ||
| param: 42, | ||
| }, | ||
| }) | ||
| ) | ||
| }) | ||
|
|
||
| it('handles non-Error values', () => { | ||
| const addErrorSpy = jasmine.createSpy() | ||
| initializeAngularPlugin({ | ||
| addError: addErrorSpy, | ||
| }) | ||
|
|
||
| addAngularError('string error') | ||
|
|
||
| expect(addErrorSpy).toHaveBeenCalledOnceWith( | ||
| jasmine.objectContaining({ | ||
| error: 'string error', | ||
| handlingStack: jasmine.any(String), | ||
| startClocks: jasmine.any(Object), | ||
| context: { framework: 'angular' }, | ||
| }) | ||
| ) | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import type { Context } from '@datadog/browser-core' | ||
| import { callMonitored, clocksNow, createHandlingStack } from '@datadog/browser-core' | ||
| import { onRumStart } from '../angularPlugin' | ||
|
|
||
| /** | ||
| * Add an Angular error to the RUM session. | ||
| * | ||
| * This function is used internally by `provideDatadogErrorHandler()`, but can also be called | ||
| * directly to report errors caught by custom error handling logic. | ||
| * | ||
| * @category Error | ||
| * @example | ||
| * ```ts | ||
| * import { addAngularError } from '@datadog/browser-rum-angular' | ||
| * | ||
| * // In a custom ErrorHandler | ||
| * handleError(error: any) { | ||
| * addAngularError(error) | ||
| * // your own error handling... | ||
| * } | ||
| * ``` | ||
| */ | ||
| export function addAngularError(error: unknown) { | ||
| const handlingStack = createHandlingStack('angular error') | ||
| const startClocks = clocksNow() | ||
| onRumStart((addError) => { | ||
| callMonitored(() => { | ||
| addError({ | ||
| error, | ||
| handlingStack, | ||
| startClocks, | ||
| context: { | ||
| ...(typeof error === 'object' && error !== null ? (error as { dd_context?: Context }).dd_context : undefined), | ||
| framework: 'angular', | ||
| }, | ||
| }) | ||
| }) | ||
| }) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { addAngularError } from './addAngularError' | ||
| export { provideDatadogErrorHandler } from './provideDatadogErrorHandler' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import type { EnvironmentInjector } from '@angular/core' | ||
| import { ErrorHandler, Injector, createEnvironmentInjector } from '@angular/core' | ||
| import { initializeAngularPlugin } from '../../../test/initializeAngularPlugin' | ||
| import { provideDatadogErrorHandler } from './provideDatadogErrorHandler' | ||
|
|
||
| function createErrorHandler(): ErrorHandler { | ||
| const injector = createEnvironmentInjector([provideDatadogErrorHandler()], Injector.NULL as EnvironmentInjector) | ||
| return injector.get(ErrorHandler) | ||
| } | ||
|
|
||
| describe('provideDatadogErrorHandler', () => { | ||
| it('provides an ErrorHandler that reports errors to Datadog', () => { | ||
| const addErrorSpy = jasmine.createSpy() | ||
| initializeAngularPlugin({ addError: addErrorSpy }) | ||
|
|
||
| const handler = createErrorHandler() | ||
| handler.handleError(new Error('test error')) | ||
|
|
||
| expect(addErrorSpy).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('still logs the error to the console via default ErrorHandler', () => { | ||
| initializeAngularPlugin() | ||
|
|
||
| const consoleErrorSpy = spyOn(console, 'error') | ||
| const handler = createErrorHandler() | ||
| handler.handleError(new Error('test error')) | ||
|
|
||
| expect(consoleErrorSpy).toHaveBeenCalled() | ||
| }) | ||
| }) | ||
|
rgaignault marked this conversation as resolved.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import type { EnvironmentProviders } from '@angular/core' | ||
| // eslint-disable-next-line local-rules/disallow-side-effects | ||
| import { ErrorHandler, makeEnvironmentProviders } from '@angular/core' | ||
| import { addAngularError } from './addAngularError' | ||
|
|
||
| // eslint-disable-next-line no-restricted-syntax | ||
| class DatadogErrorHandler extends ErrorHandler { | ||
| override handleError(error: unknown): void { | ||
| addAngularError(error) | ||
| super.handleError(error) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Provides a Datadog-instrumented Angular ErrorHandler that reports errors to RUM. | ||
| * | ||
| * @category Error | ||
| * @example | ||
| * ```ts | ||
| * import { provideDatadogErrorHandler } from '@datadog/browser-rum-angular' | ||
| * | ||
| * bootstrapApplication(AppComponent, { | ||
| * providers: [provideDatadogErrorHandler()], | ||
| * }) | ||
| * ``` | ||
| */ | ||
| export function provideDatadogErrorHandler(): EnvironmentProviders { | ||
| return makeEnvironmentProviders([{ provide: ErrorHandler, useClass: DatadogErrorHandler }]) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export { angularPlugin } from '../domain/angularPlugin' | ||
| export type { AngularPluginConfiguration } from '../domain/angularPlugin' | ||
| export { provideDatadogRouter } from '../domain/angularRouter/provideDatadogRouter' | ||
| export { addAngularError, provideDatadogErrorHandler } from '../domain/error' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import type { RumInitConfiguration, RumPublicApi, StartRumResult } from '@datadog/browser-rum-core' | ||
| import { noop } from '@datadog/browser-core' | ||
| import { angularPlugin, resetAngularPlugin } from '../src/domain/angularPlugin' | ||
| import { registerCleanupTask } from '../../core/test' | ||
|
|
||
| export function initializeAngularPlugin({ | ||
| addError = noop, | ||
| }: { | ||
| addError?: StartRumResult['addError'] | ||
| } = {}) { | ||
| resetAngularPlugin() | ||
| const plugin = angularPlugin() | ||
|
|
||
| plugin.onInit!({ | ||
| publicApi: {} as RumPublicApi, | ||
| initConfiguration: {} as RumInitConfiguration, | ||
| }) | ||
| plugin.onRumStart!({ addError }) | ||
|
|
||
| registerCleanupTask(() => { | ||
| resetAngularPlugin() | ||
| }) | ||
| } |
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❓ question: Why do you need this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh interesting :)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure for context this is the gitlab job : https://gitlab.ddbuild.io/DataDog/browser-sdk/-/jobs/1521302268 Angular : https://github.com/angular/angular/blob/main/packages/core/src/util/global.ts
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you have any other idea to handle this ?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hum weird, error comes from afterAll. .withHead(html`
<script>
if (typeof globalThis === 'undefined') {
window.globalThis = window
}
</script>
`) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // Polyfill globalThis for browsers that don't support it (e.g. Chrome < 71) | ||
| // Required because @angular/core uses globalThis internally. | ||
| /* eslint-disable no-undef */ | ||
| if (typeof globalThis === 'undefined') { | ||
| window.globalThis = window | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.