I am trying to setup ngrx effects for my angular application. I have been successful in developing the effects and actions which are working fine.
I am facing one issue which is when my service returns an unsuccessful status like 400,422 etc. The error is caught by the effect and appropriate action for failure is triggered but the payload I am receiving is unknown.
The service is giving out a proper JSON formatted body incase of error structured as follows:
{ "data": null, "error": true, "errorBody": { "code": 422, "body": "Unprocessable Entity" } }
with HTTP status code of 422
.
This is my service code method which is using AngularFire Functions(123 is deliberately added so that service throws error):
getBySummonerAlias(
summonerName: string,
summonerTag: number
): Observable<Summoner> {
const callable = this.fns.httpsCallable('getSummonerByAlias');
return callable({ 123: summonerName, summonerTag: summonerTag });
}
My actions:
export const loadSummoner = createAction('[Summoner] Load Summoner');
export const loadSummonerSuccess = createAction(
'[Summoner] Load Summoner Success',
props<{ summoner: Summoner }>()
);
export const loadSummonerFailure = createAction(
'[Summoner] Load Summoner Failure',
props<{ error: any }>()
);
My effects:
loadSummoner$ = createEffect(() =>
this.actions$.pipe(
ofType(fromSummonerActions.loadSummoner),
switchMap(() =>
this.summonerService.getBySummonerAlias('HawKEyE', 2311).pipe(
map((summoner: Summoner) =>
fromSummonerActions.loadSummonerSuccess({ summoner })
),
catchError((error) =>
of(fromSummonerActions.loadSummonerFailure({ error }))
)
)
)
)
);
My reducer:
export const reducers = createReducer(
fromSummonerStore.initialState,
on(fromSummonerActions.loadSummonerSuccess, (state, action) => {
return {
summoner: action.summoner,
};
}),
on(fromSummonerActions.loadSummonerFailure, (state, action) => {
return {
summoner: state.summoner,
error: action.error,
};
})
);
export const metaReducers: MetaReducer<
fromSummonerStore.SummonerState
>[] = !environment.production ? [] : [];
This is the Redux state: