I am throwing an exception in my service and trying to catch the exception in my effect and return the correct toast on the frontend.
What happens: It looks like i get a success message and then an error is thrown, i see the error in the console but never the "Error" toast on the frontend. I put a breakpoint on the catcherror line in the service. I see the success toast before the breakpoint gets hit.
What should happen: Should show the "Error" toast on the frontend after catching the error.
Can someone see what I am doing wrong?
Service
saveBusiness(business: BusinessModel) {
return from(this.businessCollection.add(JSON.parse(JSON.stringify(business)))).pipe(
catchError(err => throwError(err))
);
}
Effect
AddBusiness$ = createEffect(() =>
this.actions$.pipe(
ofType(BusinessActions.AddBusiness),
mergeMap(({ business }) =>
of(this.businessService.saveBusiness(business)).pipe(
map(() => BusinessActions.CreateBusinessSuccess()),
catchError(() => of(BusinessActions.CreateBusinessFailure()))
)
)
)
);
Action
export const CreateBusinessFailure = createAction(
'[Business] Failed Creating to database'
);
Component Snippet
actions$
.pipe(ofType(BusinessActions.CreateBusinessSuccess))
.subscribe((data) => {
this.notifyService.showSuccess(
'Business was saved successfully',
'Success'
);
});
actions$
.pipe(ofType(BusinessActions.CreateBusinessFailure))
.subscribe((data) => {
this.notifyService.showError(
'Business was not saved. Please check the information and try again',
'Failed creating business'
);
});