0

Error codes below :-

400 - perform single retry on response arrival without delay

502, 503 and 504 perform eight number of retries with 1000 Sec delay

My code is below :-

private _getData('loginKey', body, headers) {
return this._http.post('url', body, { headers: headers })
  .map((response: Response) => response.json())
  .retryWhen(errors => {
    return errors
      .mergeMap((error) => this.handleError(error))
      .delay(2000)
      .take(8);
  });
}

public handleError(error: Response | any): Observable<any> {
  if (error.status === 400) { // retry 1 time and with no delay
    return Observable.of(error);
  } else { // retry confgured times and configured delay
    return Observable.of(error);
  }
}

My problem is how to change delay and take value according to the above Error codes:

if error.status is 400 then delay value 0 and take value 1

if error.status is 502, 503 and 504 then delay value 2000 and take value 8

3
  • If my response answers your question would you accept it? Commented Jul 24, 2017 at 20:35
  • yes definitely if it will helpful. Commented Jul 25, 2017 at 18:05
  • I have updated my answer Commented Jul 30, 2017 at 20:31

1 Answer 1

1

The easiest way to do this is to define a function in a service that will return the value you want.

example:

let errorStatus;
private _getData('loginKey', body, headers) {
return this._http.post('url', body, { headers: headers })
  .map((response: Response) => response.json())
  .retryWhen(errors => {
    return errors
      .mergeMap((error) => {
          errorStatus = error.status;
          this.handleError(error))
      })
      .delay(ResponseService.getDelay(errorStatus))
      .take(ResponseService.getRetries(errorStatus));
  });
}

in ResponseService.js...

function getDelay(status) {
    let val = 0;
    switch(status) {
        case 400:
            val = 0;
            break;
        ...
    }
    return val;
}

function getRetries(status) {
    let val = 0;
    switch(status) {
        case 400:
            val = 1;
            break;
        ...
    }
    return val
}
Sign up to request clarification or add additional context in comments.

3 Comments

but error.status is not accessible in delay and take any idea about how to access it.
I updated the work. You should be able to capture the value of the error so you can use it outside the scope of the mergeMap
I have try your solution but still not able to access errorstatus inside delay and take because this is calling before the mergeMap inside code run. So any solution on this code or any alternation solution available. Please see this link: reactivex.io/rxjs/class/es6/Observable.js~Observable.html i don't have the complete knowledge of Observable uses I have used one method from this link mergeMap, delay and take so maybe any alternation solution is available on this link. Please guide me what approach i use for this solution if you know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.