I'm working on existing codebase and my task is to implement error handling in my update Chart but not really sure how to implement correctly since I'm using store management so I would be really appreciated if I can get any suggestion or help on how I should implement error handling in store.
I did something like this and tested it by providing to the wrong api route in my ChartService.ts file but I didn't get the error message and it always saying it "has been saved" no matter if I give the right api route or wrong api route for updating chart.
The problem I have right now is, It's not giving me error snackbar message even when there is errors in api or network.
import {Store} from '@ngrx/store';
saveClick(){
try
{
this.store.dispatch(updateChart({chart: this.chart}));
this.snackbar.open("Changes to "" + this.chart.name + "" has been saved", "", { duration: 2500 });
}
catch(error)
{
this.snackbar.open("Error has occurred. Could not update Chart", "", { duration: 2500 });
}
}
Chart.effets.ts
updateChart$ = createEffect(() => this.actions$.pipe(
ofType(ChartActionTypes.UpdateChart),
exhaustMap((props: { chart: Chart }) => this.chartService.updateChart(props.chart)//this.getChart(props)
.pipe(
// tap(c => console.log('TAPPY TAP', c)),
map(chart => {
return chartLoaded({ chart: chart })
}),
catchError(() => EMPTY)
),
)
)
)
HTML
<button color="primary" mat-flat-button (click)="saveClick()" [disabled]="this.chart.isPublished">Save</button>
It's not gonna run on but I uploaded all the code for this component in stackblitz hoping someone can help me by looking at the code. thanks https://stackblitz.com/edit/angular-ivy-ncbmb4
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…