You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When receiving an HTTP error status, there is no possibility to get the response without using an interceptor.
This results from this linked blog post in the code (see below), which states that jQuery handles errors differently.
In my opinion, it would be better if this error handling would be up to the user in order to adhere to the Fetch API.
To get the response body on HTTP error status, one needs to change the error and parse the JSON data string. I couldn't come up with another solution for this.
throwOnFailed could be a useFetchOption, so that the changed context in onFetchError can be always returned. IMHO, this is not a good option.
Additional context
This has been discussed several times for interceptors. But using an interceptor requires parsing the response and is only possible when using with execute(true).
I would like to be able to get the response body on, for example 404 or 409, and use it without calling execute(true).
And when calling execute().then((res: Response) => ...) it always includes the whole response, while .catch((err) => ...) only includes network errors.
Loving VueUse and looking forward to what you think.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Clear and concise description of the problem
When receiving an HTTP error status, there is no possibility to get the response without using an interceptor.
This results from this linked blog post in the code (see below), which states that jQuery handles errors differently.
In my opinion, it would be better if this error handling would be up to the user in order to adhere to the Fetch API.
To get the response body on HTTP error status, one needs to change the error and parse the JSON data string. I couldn't come up with another solution for this.
But it is only possible to catch this error with
{immediate: false}
andexecute(true)
, because otherwise the request resolves withnull
.Suggested solution
The returned
data
anderror
objects ofuseFetch
should be aligned with Fetch, so that it is up to the user what to do on HTTP error status codes.This could be achieved with the following steps.
Remove this code block:
vueuse/packages/core/useFetch/index.ts
Lines 444 to 446 in 01c2820
Maybe replace with an option
throwOnHttpError
or similar, which would populate the error returned fromuseFetch
even on HTTP error status.Reject
fetchError
like the Fetch API only on network failure. Remove the check and just doreject(fetchError)
:vueuse/packages/core/useFetch/index.ts
Lines 460 to 463 in 01c2820
Alternative
throwOnFailed
could be auseFetchOption
, so that the changed context inonFetchError
can be always returned. IMHO, this is not a good option.Additional context
This has been discussed several times for interceptors. But using an interceptor requires parsing the response and is only possible when using with
execute(true)
.I would like to be able to get the response body on, for example
404
or409
, and use it without callingexecute(true)
.And when calling
execute().then((res: Response) => ...)
it always includes the whole response, while.catch((err) => ...)
only includes network errors.Loving VueUse and looking forward to what you think.
Validations
The text was updated successfully, but these errors were encountered: