Mocky icon indicating copy to clipboard operation
Mocky copied to clipboard

Parameter to randomly generate an error response

Open moniuch opened this issue 7 years ago • 1 comments

I would welcome a feature when, at the same url which is set to respond with OK status, I could have an error response generated with a random seed.

I am developing an Observable pattern to perform a series of retries to call an endpoint, with increasing delay. The problem is that during testing, I cannot really mock up the condition by randomly switching between two mocky.io urls (error and ok) in TypeScript, because apparently the once drawn url value:

getMocky(){
    const u = Math.random();
    const okUrl = 'http://www.mocky.io/v2/58ffadf71100009b17f60044';
    const erUrl = 'http://www.mocky.io/v2/58ffae7f110000ba17f60046';
    return u > 0.6 ? this.http.get(okUrl) : this.http.get(erUrl);
}

remains sticky throughout the whole Observable chain. Therefore, I either get a single successful call or a series of all unsuccessful ones. There's never a case where the chain would get a successful call before the series of attempts is up.

@Effect()
loadData$: Observable<Action> = this.actions$
  .ofType(ActionTypes.LOAD_DATA)
  .pluck('payload')
  .switchMap(params => {
    return this.myService.getData(params)
      .map(res => new LoadDataCompleteAction(res))
      .let(retryOrThrow(5, 1000))  // a custom operator to make 5 attempts
      .catch(err => Observable.of(new LoadDataFailed()));
  });
   

So maybe some good soul could add a feature, which would be best explained by this simplified UI mockup:

[x] Generate an error with random seed of _______ (0 < n < 1)

  1. The higher the n, the less probability of error. Any error (400+)
  2. The option would only be enabled for status codes <400

moniuch avatar Apr 25 '17 21:04 moniuch

Hello,

Thanks for your proposition.

Unfortunately I havn't the time to implement this feature, but a community contribution will be accepted with pleasure.

Someone could add for example a mocky-error-proba=0.4 parameter.

julien-lafont avatar Oct 21 '17 21:10 julien-lafont