Custom Observable events RxJS


#1

Does anyone know a way to customize an observable.

I would like to do a http.get and send my data into an observable Obs.next(resp.json()).
In case there’s an error then retry but trigger a custom event on my observable so that i can notify the user it will retry. Obs.custom()
And after 5 times trigger Obs.error(err) and stop

Events would solve my issue but i dont want it system wide.

Observable side

Obs.next(mydate);
Obs.customEvent(y)
Obs.error(err);

Consuming side

foo.subscribe(
(x)=>{ Handle my data },
(y)=>{ console.log('custom stuff');},
(error=>{ handle error} 
);

Thanks,


#2

Since I have to call a not-so-well-designed API, I have the observable (or promise in my case) return a custom object, containing a status code, the data and message in case of error. This way I can transform HTTP status codes and API error codes into a consistent object to process. Hope this will help you.

Obs.next({
    code: 200, // success
    data: mydate,
});

Obs.next({
    code: 500, // error
    message: 'Internal server error'
});