Skip to content

Commit

Permalink
refactor(docs-infra): prefix error messages with Angular version info (
Browse files Browse the repository at this point in the history
…#43686)

Prefix error messages with the Angular version currently used in the
angular.io app. This applies to both errors logged to the console and
reported to Google Analytics and makes debugging certain errors easier.

PR Close #43686
  • Loading branch information
gkalpak authored and dylhunn committed Oct 6, 2021
1 parent 72d1dfd commit 81d3919
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
25 changes: 21 additions & 4 deletions aio/src/app/shared/reporting-error-handler.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ErrorHandler, Injector } from '@angular/core';
import { ErrorHandler, Injector, VERSION } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { WindowToken } from 'app/shared/window';
import { AppModule } from 'app/app.module';
Expand Down Expand Up @@ -46,10 +46,27 @@ describe('ReportingErrorHandler service', () => {
expect(onerrorSpy.calls.argsFor(0)[4]).toEqual(jasmine.any(Error));

// Then error from initial exception
expect(onerrorSpy.calls.argsFor(1)[0]).toEqual('initial error');
expect(onerrorSpy.calls.argsFor(1)[0]).toEqual(`[v${VERSION.full}] initial error`);
expect(onerrorSpy.calls.argsFor(1)[4]).toEqual(error);
});

it('should augment an error object with version info', () => {
const originalMessage = 'this is an error message';
const error = new Error(originalMessage);

expect(error.message).toBe(originalMessage);
if (error.stack) {
expect(error.stack).toContain(originalMessage);
}

handler.handleError(error);

expect(error.message).toBe(`[v${VERSION.full}] ${originalMessage}`);
if (error.stack) {
expect(error.stack).toContain(`[v${VERSION.full}] ${originalMessage}`);
}
});

it('should send an error object to window.onerror', () => {
const error = new Error('this is an error message');
handler.handleError(error);
Expand All @@ -72,10 +89,10 @@ describe('ReportingErrorHandler service', () => {
expect(onerrorSpy).toHaveBeenCalledWith('{reason: this is an error message}');
});

it('should send an error string to window.onerror', () => {
it('should send an error string to window.onerror (prefixed with version info)', () => {
const error = 'this is an error message';
handler.handleError(error);
expect(onerrorSpy).toHaveBeenCalledWith(error);
expect(onerrorSpy).toHaveBeenCalledWith(`[v${VERSION.full}] ${error}`);
});

it('should send a non-object, non-string error stringified to window.onerror', () => {
Expand Down
23 changes: 20 additions & 3 deletions aio/src/app/shared/reporting-error-handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ErrorHandler, Inject, Injectable } from '@angular/core';
import { ErrorHandler, Inject, Injectable, VERSION } from '@angular/core';
import { WindowToken } from './window';

/**
Expand All @@ -19,12 +19,29 @@ export class ReportingErrorHandler extends ErrorHandler {
* @param error Information about the error.
*/
handleError(error: any) {
const versionedError = this.prefixErrorWithVersion(error);

try {
super.handleError(error);
super.handleError(versionedError);
} catch (e) {
this.reportError(e);
}
this.reportError(error);
this.reportError(versionedError);
}

private prefixErrorWithVersion<T>(error: T): T {
const prefix = `[v${VERSION.full}] `;

if (error instanceof Error) {
const oldMessage = error.message;
error.message = prefix + oldMessage;
error.stack = error.stack?.replace(oldMessage, error.message);
} else if (typeof error === 'string') {
error = prefix + error as unknown as T;
}
// If it is a different type, omit the version to avoid altering the original `error` object.

return error;
}

private reportError(error: unknown) {
Expand Down
4 changes: 2 additions & 2 deletions goldens/size-tracking/aio-payloads.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"master": {
"uncompressed": {
"runtime": 4454,
"main": 461446,
"main": 461726,
"polyfills": 55658,
"styles": 69935,
"light-theme": 77426,
Expand All @@ -15,7 +15,7 @@
"master": {
"uncompressed": {
"runtime": 4454,
"main": 460460,
"main": 460478,
"polyfills": 55807,
"styles": 69935,
"light-theme": 77426,
Expand Down

0 comments on commit 81d3919

Please sign in to comment.