diff --git a/packages/angular/src/errorhandler.ts b/packages/angular/src/errorhandler.ts index 4ea44c2ffc63..28c06e1e6bfd 100644 --- a/packages/angular/src/errorhandler.ts +++ b/packages/angular/src/errorhandler.ts @@ -39,7 +39,8 @@ function extractHttpModuleError(error: HttpErrorResponse): string | Error { } // ... or an`ErrorEvent`, which can provide us with the message but no stack... - if (error.error instanceof ErrorEvent && error.error.message) { + // guarding `ErrorEvent` against `undefined` as it's not defined in Node environments + if (typeof ErrorEvent !== 'undefined' && error.error instanceof ErrorEvent && error.error.message) { return error.error.message; } diff --git a/packages/angular/test/errorhandler.test.ts b/packages/angular/test/errorhandler.test.ts index 3c2b2f994446..c30a7a87efc9 100644 --- a/packages/angular/test/errorhandler.test.ts +++ b/packages/angular/test/errorhandler.test.ts @@ -45,6 +45,11 @@ describe('SentryErrorHandler', () => { }); describe('handleError method', () => { + const originalErrorEvent = globalThis.ErrorEvent; + afterEach(() => { + globalThis.ErrorEvent = originalErrorEvent; + }); + it('extracts `null` error', () => { createErrorHandler().handleError(null); @@ -223,6 +228,18 @@ describe('SentryErrorHandler', () => { expect(captureExceptionSpy).toHaveBeenCalledWith('Handled unknown error', captureExceptionEventHint); }); + it('handles ErrorEvent being undefined', () => { + const httpErr = new ErrorEvent('http', { message: 'sentry-http-test' }); + const err = new HttpErrorResponse({ error: httpErr }); + + // @ts-expect-error - this is fine in this test + delete globalThis.ErrorEvent; + + expect(() => { + createErrorHandler().handleError(err); + }).not.toThrow(); + }); + it('extracts an Error with `ngOriginalError`', () => { const ngErr = new Error('sentry-ng-test'); const err = {