import { Injectable } from '@angular/core'; import { BehaviorSubject, Observable } from 'rxjs'; import { HttpErrorResponse } from '@angular/common/http'; import { LoadingService } from '../loading'; import { filter } from 'rxjs/operators'; import { NavigationStart, Router } from '@angular/router'; @Injectable({ providedIn: 'root' }) export class ErrorService { readonly error$: Observable; private readonly _errorEvent$ = new BehaviorSubject(undefined); constructor(private readonly _loadingService: LoadingService, private readonly _router: Router) { this.error$ = this._errorEvent$.asObservable(); _router.events.pipe(filter(event => event instanceof NavigationStart)).subscribe(() => { this.clear(); }); } set(error: HttpErrorResponse): void { this._loadingService.stop(); this._errorEvent$.next(error); } clear(): void { this._errorEvent$.next(undefined); } }