vlki / refresh-fetch

Wrapper around fetch capable of graceful authentication token refreshing.
MIT License
84 stars 12 forks source link

Failing request after token refresh throws error from the original request #4

Closed juliencampion closed 5 years ago

juliencampion commented 5 years ago

Thank you for this module ! Unfortunately I've encountered a problem while using it.

Description: We assume an API endpoint that returns an error (e.g. 500, Internal server error) instead of a 2xx response, when processing the request, but only after authentication is done. We also assume that a request must be made to this endpoint, but our token has expired, so it needs to be refreshed beforehand. We also assume that the sample code from the README.md file is used to make this request.

Thus, a call to fetch('/api/data') triggers the following HTTP requests:

Expected behavior: Upon encountering such a scenario, this call to fetch('/api/data') should throw the second exception, the one resulting from the response with 500 HTTP status code.

Actual behavior: The actual exception raised from the call is the first one, the one resulting from the response with 401 HTTP status code, instead of the second one. This means that the developer handling this exception is confused from such an exception, since having expired tokens is expected to be a common occurrence, and furthermore the token was refreshed successfully in this example.

I encountered such a problem earlier. If you need help fixing it or have other priorities, I can submit a pull request to this repository, addressing this issue.

Thank you for your time!

vlki commented 5 years ago

Hello @juliencampion , thanks for taking the time to explain! I did not know about this problem and it makes perfect sense that in your scenario the exception returned should have error code 500, not 401. I would appreciate if you could post a pull request.

vlki commented 5 years ago

Added a test for it and published as version 0.6.2.

juliencampion commented 5 years ago

Thank you! I will be able to use the fix right away then :)