diff --git a/aio/src/app/shared/reporting-error-handler.spec.ts b/aio/src/app/shared/reporting-error-handler.spec.ts index 39fb790e30..941327b22f 100644 --- a/aio/src/app/shared/reporting-error-handler.spec.ts +++ b/aio/src/app/shared/reporting-error-handler.spec.ts @@ -56,10 +56,40 @@ describe('ReportingErrorHandler service', () => { expect(onerrorSpy).toHaveBeenCalledWith(error.message, undefined, undefined, undefined, error); }); + it('should send a non-error object to window.onerror', () => { + const error = {reason: 'this is an error message'}; + handler.handleError(error); + expect(onerrorSpy).toHaveBeenCalledWith(JSON.stringify(error)); + }); + + it('should send a non-error object with circular references to window.onerror', () => { + const error = { + reason: 'this is an error message', + get self() { return this; }, + toString() { return `{reason: ${this.reason}}`; }, + }; + handler.handleError(error); + expect(onerrorSpy).toHaveBeenCalledWith('{reason: this is an error message}'); + }); + it('should send an error string to window.onerror', () => { const error = 'this is an error message'; handler.handleError(error); expect(onerrorSpy).toHaveBeenCalledWith(error); }); + + it('should send a non-object, non-string error stringified to window.onerror', () => { + handler.handleError(404); + handler.handleError(false); + handler.handleError(null); + handler.handleError(undefined); + + expect(onerrorSpy.calls.allArgs()).toEqual([ + ['404'], + ['false'], + ['null'], + ['undefined'], + ]); + }); }); }); diff --git a/aio/src/app/shared/reporting-error-handler.ts b/aio/src/app/shared/reporting-error-handler.ts index 126580f157..badb88bdff 100644 --- a/aio/src/app/shared/reporting-error-handler.ts +++ b/aio/src/app/shared/reporting-error-handler.ts @@ -17,8 +17,7 @@ export class ReportingErrorHandler extends ErrorHandler { * Send error info to Google Analytics, in addition to the default handling. * @param error Information about the error. */ - handleError(error: string | Error) { - + handleError(error: any) { try { super.handleError(error); } catch (e) { @@ -27,12 +26,19 @@ export class ReportingErrorHandler extends ErrorHandler { this.reportError(error); } - private reportError(error: string | Error) { + private reportError(error: unknown) { if (this.window.onerror) { - if (typeof error === 'string') { - this.window.onerror(error); - } else { + if (error instanceof Error) { this.window.onerror(error.message, undefined, undefined, undefined, error); + } else { + if (typeof error === 'object') { + try { + error = JSON.stringify(error); + } catch { + // Ignore the error and just let it be stringified. + } + } + this.window.onerror(`${error}`); } } } diff --git a/aio/src/index.html b/aio/src/index.html index 2444bf4735..db945765a5 100644 --- a/aio/src/index.html +++ b/aio/src/index.html @@ -69,7 +69,7 @@ function formatError(msg, url, line, col, e) { var stack; msg = msg.replace(/^Error: /, ''); - if (e) { + if (e && e.stack) { stack = e.stack // strip the leading "Error: " from the stack trace .replace(/^Error: /, '')