I would like to abstract some error management in my application.
The goal is to prepare an object with enough information so the view can adapt its behavior easily.
sandbox() {
// Simulation of a backend query
const stream$ = of([1,2,3]).pipe(
catchError((error) => of(new MyError(error))),
takeUntil(this.destroyed$)
);
this.test$ = this.handleState(stream$);
}
handleState<T>(stream$: Observable<T | MyError>): Observable<UiDataState<T>> {
const errorStream$ = stream$.pipe(
filter((data) => data instanceof MyError),
map(() => null),
);
const noErrorStream$ = stream$.pipe(
startWith(null),
filter((data) => !(data instanceof MyError)),
map((data) => data as T),
);
const isError$ = errorStream$.pipe(
startWith(false),
map(() => true)
);
return combineLatest([
isError$,
noErrorStream$,
]).pipe(
map(([isError, data]) => {
return {
isError: isError,
data: data
} as UiDataState<T>
})
);
}
This is working but I would like to be able to let the programmer decides the type of error (cf. MyError) to use.
Is there a solution with generics? I tried many solutions but the one above is working.
My conclusion is that it's not possible but I wanted to confirm this with more experienced guys that me 🙂