Error service updates

This commit is contained in:
Adina Țeudan 2021-08-27 20:26:06 +03:00
parent 7029f2942d
commit 45f90be54b

View File

@ -2,21 +2,20 @@ import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs'; import { BehaviorSubject, Observable } from 'rxjs';
import { HttpErrorResponse } from '@angular/common/http'; import { HttpErrorResponse } from '@angular/common/http';
import { LoadingService } from '../loading'; import { LoadingService } from '../loading';
import { map } from 'rxjs/operators';
export type Error = HttpErrorResponse | null;
@Injectable({ providedIn: 'root' }) @Injectable({ providedIn: 'root' })
export class ErrorService { export class ErrorService {
readonly hasError$: Observable<Error>; readonly hasError$: Observable<boolean>;
private readonly _errorEvent$ = new BehaviorSubject<Error>(null); private readonly _errorEvent$ = new BehaviorSubject<HttpErrorResponse | undefined>(undefined);
constructor(private readonly _loadingService: LoadingService) { constructor(private readonly _loadingService: LoadingService) {
this.hasError$ = this._errorEvent$.asObservable(); this.hasError$ = this._errorEvent$.asObservable().pipe(map(e => !!e));
} }
private _error: Error = null; private _error?: HttpErrorResponse;
get error(): Error { get error(): HttpErrorResponse | undefined {
return this._error; return this._error;
} }
@ -27,7 +26,7 @@ export class ErrorService {
} }
clear(): void { clear(): void {
this._errorEvent$.next(null); this._errorEvent$.next(undefined);
this._error = null; this._error = undefined;
} }
} }