How to throw stream error in custom operator?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How to throw stream error in custom operator?



I found cstom operator that I want to use.



This is an operator that retries http requests. Code is from Stephen Fluin: https://github.com/StephenFluin/http-operators/blob/master/operators/retryExponentialBackoff.operator.ts.



Problem is that if after all these reties it does not puts error in stream only completes.
I want it to throw an error. How to do it?
I think this part should be modified:


error(err: any) {
if (count <= maxTries) {
subscription.add(scheduler.schedule(subscribe, initialWait * Math.pow(2, count++)));
}
},



Here is whole operator's class


/**
* Repeats underlying observable on a timer
*
* @param maxTries The maximum number of attempts to make, or -1 for unlimited
* @param initialWait Number of seconds to wait for refresh
*/
export const retryExponentialBackoff = (
maxTries = -1,
initialWait = 1,
scheduler: SchedulerLike = asyncScheduler
) => <T>(
source: Observable<T>
) => {
return new Observable<T>(subscriber => {
let count = 1;
const subscription = new Subscription();

const subscribe = () =>
subscription.add(
source.subscribe({
next(value: T) {
count = 1;
subscriber.next(value);
},
error(err: any) {
if (count <= maxTries) {
subscription.add(scheduler.schedule(subscribe, initialWait * Math.pow(2, count++)));
}
},
complete() {
subscriber.complete();
},
})
);

subscribe();

return subscription;
});
};




1 Answer
1



I would try to add the error bubbling to the subscriber like so:


error(err: any) {
if (count <= maxTries) {
subscription.add(scheduler.schedule(subscribe, initialWait * Math.pow(2, count++)));
}
else {
subscriber.error(err);
}
},



So that after your maxTries count have een exhausted the error is emitted downstream.


maxTries






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Better method to check all objects' parameters with a single call

C++ virtual function: Base class function is called instead of derived