import { ErrorHandler, Injectable, Injector, NgModule } from '@angular/core'; import { NavigationError, Router } from '@angular/router'; import { ApplicationinsightsAngularpluginErrorService } from '@microsoft/applicationinsights-angularplugin-js'; import { filter, map } from 'rxjs'; import { MwMonitorService } from './monitor.service'; @Injectable() export class MwErrorHandler extends ApplicationinsightsAngularpluginErrorService { constructor( private readonly injector: Injector, private readonly router: Router ) { super(); this.router.events .pipe(filter((event: any) => event.error)) .subscribe((err) => { this.handleError(err.error); console.error(err.error); }); } override handleError(error: any): void { try { const monitorService = this.injector.get( MwMonitorService, null ) as MwMonitorService; if (monitorService) { monitorService.logError(new Error(error.message), error.stack); } } catch (err) { super.handleError(error); } super.handleError(error); } } @NgModule({ providers: [ MwMonitorService, { provide: ErrorHandler, useClass: MwErrorHandler }, ], }) export class MwErrorHandlerModule {}