fix: provide statusText as error message on retryable network errors #832
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What kind of change does this PR introduce?
This PR fixes a minor, but annoying lack of error message and adds a test case for an uncovered branch.
What is the current behavior?
Network errors (502,503,504) result into
AuthRetryableFetchError
(correct), with"{}"
as anError#message
(embarassing if displayed to the end user).The bug is caused by erroneous usage of
_getErrorMessage(error)
helper in a branch wherelooksLikeFetchResponse(error)
assertion have succeeded anderror.status
is502 | 503 | 504
. I believe_getErrorMessage
was intended to be called on something that has been thrown or returned as an error, not on a resolved fetch response that was notok
.The dreaded
"{}"
comes from the finalJSON.stringify
fallback of_getErrorMessage
.What is the new behavior?
I went for the simplest solution of providing
Response.statusText
property as an error message. Status text is the only human-readable piece of information that is reliably/readily available in this context. It is also reasonable to expect it to be clear and accurate.Additional context
I briefly considered pulling in the
Response.text()
and including it in the message, but decided against it. Proxy errors rarely include any extra info there, but often include HTML formatting and styling which wouldn't be appropriate here. Also waiting forResponse.text()
might fail with another error.This fix should be backwards compatible on a patch level. The
Error
constructor coerces the message to a String (or leaves out an empty string in case ofundefined
) therefore even in case of custom fetch implementations/middlewares that don't providestatusText
ourAuthRetryableFetchError
will still have amessage
field of a string type.