From 9ecd0c11d72850220f3c66e7abc221df6bc344a0 Mon Sep 17 00:00:00 2001 From: Erik Tiekstra Date: Thu, 9 Dec 2021 07:27:59 +0100 Subject: [PATCH] fix(error-handling): Removed appRef.tick() to avoid unnecessary errors. (TV-944) Merge in TEA/mina-sidor-fa-web from feature/TV-944 to develop Squashed commit of the following: commit f51b753d96030235edb641daac8ecf6b428d1557 Author: Erik Tiekstra Date: Wed Dec 8 09:13:58 2021 +0100 Removed appRef.tick() --- apps/mina-sidor-fa/src/app/app.module.ts | 9 -------- .../toast-list/toast/toast.component.ts | 10 ++++---- .../src/app/shared/services/error.service.ts | 23 +++++-------------- 3 files changed, 11 insertions(+), 31 deletions(-) diff --git a/apps/mina-sidor-fa/src/app/app.module.ts b/apps/mina-sidor-fa/src/app/app.module.ts index 9a7779b..2721c92 100644 --- a/apps/mina-sidor-fa/src/app/app.module.ts +++ b/apps/mina-sidor-fa/src/app/app.module.ts @@ -5,7 +5,6 @@ import localeSe from '@angular/common/locales/sv'; import { ErrorHandler, LOCALE_ID, NgModule, Provider } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { ApmErrorHandler } from '@elastic/apm-rum-angular'; -import { environment } from '@msfa-environment'; import { AuthInterceptor } from '@msfa-interceptors/auth.interceptor'; import { CustomErrorHandler } from '@msfa-interceptors/custom-error-handler'; import { AppRoutingModule } from './app-routing.module'; @@ -22,14 +21,6 @@ const providers: Provider[] = [ { provide: ErrorHandler, useClass: CustomErrorHandler }, ]; -// Skip error handler in Dev until "Uncaught Error: ApplicationRef.tick is called recursively" is fixed -if (environment.production) { - providers.push({ - provide: ErrorHandler, - useClass: CustomErrorHandler, - }); -} - @NgModule({ declarations: [AppComponent], imports: [ diff --git a/apps/mina-sidor-fa/src/app/components/toast-list/toast/toast.component.ts b/apps/mina-sidor-fa/src/app/components/toast-list/toast/toast.component.ts index 70089f2..36ee04e 100644 --- a/apps/mina-sidor-fa/src/app/components/toast-list/toast/toast.component.ts +++ b/apps/mina-sidor-fa/src/app/components/toast-list/toast/toast.component.ts @@ -19,11 +19,11 @@ export class ToastComponent implements AfterViewInit { ErrorSeverity = ErrorSeverity; ngAfterViewInit(): void { - // if (this.error.removeAfter) { - // setTimeout(() => { - // this.closeToast.emit(this.error); - // }, this.error.removeAfter); - // } + if (this.error.removeAfter) { + setTimeout(() => { + this.closeToast.emit(this.error); + }, this.error.removeAfter); + } } get className(): string { diff --git a/apps/mina-sidor-fa/src/app/shared/services/error.service.ts b/apps/mina-sidor-fa/src/app/shared/services/error.service.ts index ea643b0..1515b45 100644 --- a/apps/mina-sidor-fa/src/app/shared/services/error.service.ts +++ b/apps/mina-sidor-fa/src/app/shared/services/error.service.ts @@ -1,4 +1,4 @@ -import { ApplicationRef, Injectable, Injector } from '@angular/core'; +import { Injectable } from '@angular/core'; import { CustomError } from '@msfa-models/error/custom-error'; import { BehaviorSubject, Observable } from 'rxjs'; import { map } from 'rxjs/operators'; @@ -7,29 +7,18 @@ import { map } from 'rxjs/operators'; providedIn: 'root', }) export class ErrorService { - private appRef: ApplicationRef; - private errorQueue$ = new BehaviorSubject([]); + private _errorQueue$ = new BehaviorSubject([]); - public errors$: Observable = this.errorQueue$.pipe( + public errors$: Observable = this._errorQueue$.pipe( map(errors => errors.sort((a, b) => (a.timestamp > b.timestamp ? -1 : 1))) ); - constructor(private injector: Injector) { - // Workaround to fix change-detection when using Error interceptor - // See https://stackoverflow.com/a/37793791 - setTimeout(() => { - this.appRef = this.injector.get(ApplicationRef); - }); - } - public add(error: CustomError): void { - this.errorQueue$.next([...this.errorQueue$.value, error]); - this.appRef.tick(); + this._errorQueue$.next([...this._errorQueue$.value, error]); } public remove(error: CustomError): void { - const newErrorQueue = this.errorQueue$.value.filter(currentError => currentError.id !== error.id); - this.errorQueue$.next(newErrorQueue); - this.appRef.tick(); + const newErrorQueue = this._errorQueue$.value.filter(currentError => currentError.id !== error.id); + this._errorQueue$.next(newErrorQueue); } }