common-ui/src/lib/error/error.service.ts
2021-09-15 23:48:22 +03:00

29 lines
1.0 KiB
TypeScript

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<HttpErrorResponse | undefined>;
private readonly _errorEvent$ = new BehaviorSubject<HttpErrorResponse | undefined>(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);
}
}