angular/aio/src/app/shared/reporting-error-handler.ts
2019-04-10 12:12:16 -07:00

40 lines
1.0 KiB
TypeScript

import { ErrorHandler, Inject, Injectable } from '@angular/core';
import { WindowToken } from './window';
/**
* Extend the default error handling to report errors to an external service - e.g Google Analytics.
*
* Errors outside the Angular application may also be handled by `window.onerror`.
*/
@Injectable()
export class ReportingErrorHandler extends ErrorHandler {
constructor(@Inject(WindowToken) private window: Window) {
super();
}
/**
* Send error info to Google Analytics, in addition to the default handling.
* @param error Information about the error.
*/
handleError(error: string | Error) {
try {
super.handleError(error);
} catch (e) {
this.reportError(e);
}
this.reportError(error);
}
private reportError(error: string | Error) {
if (this.window.onerror) {
if (typeof error === 'string') {
this.window.onerror(error);
} else {
this.window.onerror(error.message, undefined, undefined, undefined, error);
}
}
}
}