I am making request to the server and want to show translated error message in case of failure.
I read that passing anonymous error
function to subscribe
is deprecated.
I come up with this code:
this.myService.editSendReport(params)
.pipe(catchError(() => this.translateService.get('settings.report.error')))
.subscribe((res: MyResponce) => {
// In case of error, we show snack bar with error message.
if (typeof res === 'string' || res instanceof String) {
this.snackBar.open(String(res), undefined, {
duration: 3000
});
return;
}
// Success logic
...
});
But still I find this code kind of weird, because now we must handle showing error message in subscribe
in next
callback. What is the best approach of showing error message now?
this.translateService.get('settings.report.error')
is also Observable
. So if I put inside of error
of Observer
. I will need to subscribe
inside of subscribe
, which is considered an anti-pattern too.