I have found myself working with code from themes written in latest Angular versions and searching the web, and I have found that the most part of devs don't handle the subscription error.
My question is: When do I have to handle the error in an Observable subscription?
Without error handling:
this.myService.observable$.subscribe(
(data) => {
// do stuff with data
}
);
With error handling:
this.myService.observable$.subscribe(
(data) => {
// do stuff with data
},
err => {
// do stuff with error
}
);
I mostly find the first version, but...
Isn't an issue not to handle the errors of a subscription?
Doesn't this make the code less solid, testable and more prone to fail?
why is error handling important click Me
Now Lets see Why error handling is necessary in Observables..
Example:
this.service.send(this.shareData).subscribe(() => {
// Here you are sure that the send has shared the data sucessFully
}, (error) => {
/* Now If you want to handle errors Like Front End Errors and Log this
In your backEnd DB So solve it and fix it */
/* Example below check error type is It from frontEnd and log error through Api */
if(error.type !== 'API') {
this.logService.log({
Level: 2,
Message: 'Failed to setFromDB',
});
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With