I have guard and now I need to catch error in it from a side-effect which calls an http service and redirects the user to an error state. I tried to add catchError
, but I don't even go there if I get an error from server side.
guard.ts
canActivate(): Observable<boolean> {
return this.store
.select(fromDataStore.getDatasLoaded)
.pipe(
withLatestFrom(this.store.select(fromDataStore.getDataLoading)),
map(([loaded, loading]) => {
if (!loaded && !loading) {
this.store.dispatch(new fromDataStore.LoadData());
}
return loaded;
}),
filter(loaded => loaded),
take(1),
catchError(error => {
// redirect user to error state
})
);
}
effect.ts
@Effect() loadData$: Observable<Action> = this.actions$.ofType(DataActionTypes.LoadData)
.pipe(
mergeMap(() =>
this.dataApiService.loadData()
.pipe(
map(data => ({
type: DataActionTypes.LoadDataSuccess,
payload: data
})),
catchError(() => of({ type: DataActionTypes.LoadDataFailure }))
)
)
);
reducer.ts
case DataActionTypes.LoadData: {
return {
...state,
data: {
...state.data,
isLoading: true
}
};
}
case DataActionTypes.LoadDataSuccess: {
return {
...state,
data: {
...dataAdapter.addMany(action.payload, state.data),
isLoaded: true,
isLoading: false
}
};
}
case DataActionTypes.LoadDataFailure: {
return {
...state,
data: {
...state.data,
isLoaded: true,
isLoading: false
}
};
}
default: {
return state;
}