common-ui/src/lib/error/error.service.ts
2021-08-24 00:11:37 +02:00

35 lines
852 B
TypeScript

import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { LoadingService } from '../loading/loading.service';
export type Error = {
name: string;
} | null;
@Injectable({ providedIn: 'root' })
export class ErrorService {
readonly hasError$: Observable<Error>;
private readonly _errorEvent$ = new BehaviorSubject<Error>(null);
constructor(private readonly _loadingService: LoadingService) {
this.hasError$ = this._errorEvent$.asObservable();
}
private _error: Error = null;
get error(): Error {
return this._error;
}
set(error: Error): void {
this._loadingService.stop();
this._error = error;
this._errorEvent$.next(error);
}
clear(): void {
this._errorEvent$.next(null);
this._error = null;
}
}