ngx-resource icon indicating copy to clipboard operation
ngx-resource copied to clipboard

Howto retry request

Open bluewalk opened this issue 4 years ago • 2 comments

First of all, great work, saves a bunch of work on my side 👍

What I'm unable to figure out is how to retry a request. For example I have the following, a class that extends Resource which overrides

  • getHeaders (to implement the bearer token)
  • getUrl (to get the baseUrl from the environment)
  • handleSuccessResponse (to hide the loading bar)
  • handleErrorResponse (to hide the loading bar and on 401 do a refresh of the bearer token and logout upon 403)

After refreshing the bearer token (which returns a boolean) I would like to re-execute the original request so the user doesn't have to click the button twice.

How would I go and achieve that? Is there a way to send the original request back to this.requestHandler? Or is there some other way?

Below the handleErrorResponse method

  $handleErrorResponse(options: IResourceActionInner, resp: IResourceResponse): any {
    this.loader.complete();

    switch (resp.status) {
      case 401:
        if (this._authservice.refresh()) {
          console.log('gotta retry request, but how?');
        }
        break;
      case 403:
        this._authservice.logout();
        break;
    }
    
    if (options.returnData && this.$_canSetInternalData(options)) {
      options.returnData.$resolved = true;
    }
    if (options.actionAttributes.onError) {
      options.actionAttributes.onError(resp);
    }
  }

bluewalk avatar Jan 14 '21 06:01 bluewalk

Hi @bluewalk

Thank you for warm words :) It's a log time I did not develop with the library since I switched to Flutter.

But I think you can try to call return this.$restAction(options) in that case (not sure).

  $handleErrorResponse(options: IResourceActionInner, resp: IResourceResponse): any {
    this.loader.complete();

    switch (resp.status) {
      case 401:
        if (this._authservice.refresh()) {
          console.log('gotta retry request, but how?');
          return this.$restAction(options); // hope it will help you
        }
        break;
      case 403:
        this._authservice.logout();
        break;
    }
    
    if (options.returnData && this.$_canSetInternalData(options)) {
      options.returnData.$resolved = true;
    }
    if (options.actionAttributes.onError) {
      options.actionAttributes.onError(resp);
    }
  }

Let me know if that will help you.

troyanskiy avatar Jan 14 '21 15:01 troyanskiy

I was thinking of something like that as well, unfortunately it doesn't work: Error: Uncaught (in promise): [object Undefined]

The methods are IResourceMethodPromise's

bluewalk avatar Jan 14 '21 16:01 bluewalk