2020-09-27

How do I handle errors in a shared observable in typescript?

I have stumbled on a bit of an issue and have trouble finding a person with a similar issue, which probably indicates I am doing something that I shouldn't be doing..

I am doing a http request that looks like this

return this.httpClient.post(`${this.route}/typegroups`, JSON.stringify(typeGroup)).pipe(share());

I am subscribing to the returning observable twice and wants both subscribers to be notified if an error occurs(and if everything went well of course). Right now it's only the first subscriber that gets the error notification

First subscriber:

obs.subscribe((next:any) => {
  //Success Code
},
error => {
  this.notificationService.showError(error.message)
})

The second subscriber

obs.subscribe(next => {
    console.log("EVERYTHING WENT WELL")
  },
  error => {
    console.log("ERROR")
  },
  () => console.log("COMPLETED"));

The first subscriber is getting the error notification and the error method is executed, the other subscriber however is having the next method is executed. Which makes it think everything went ok. Any idea why this is happening? Perhaps there even is a better way of doing this?

Thanks alot in advance, I'm currently racking my brains out tring to find a solution to this..



from Recent Questions - Stack Overflow https://ift.tt/3j8hN5k
https://ift.tt/eA8V8J

No comments:

Post a Comment