New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useFetch
Have access to the response body when the status is not ok
#553
Comments
In response to
No need at all for this as the For the other concerns, now the body is parsed even when the status is not ok, it's just not set to response.value = fetchResponse;
statusCode.value = fetchResponse.status;
let responseData = await fetchResponse[config.type](); //See this line, it's already parsing the body
// see: https://www.tjvantoll.com/2015/09/13/fetch-and-errors/
if (!fetchResponse.ok)
throw new Error(fetchResponse.statusText);
if (options.afterFetch)
({ data: responseData } = await options.afterFetch({ data: responseData, response: fetchResponse }));
data.value = responseData; That's why I think, at least for the moment, to throw the error just right after |
I'll try to add only one method for export interface CustomResponseFetchContext<T = any> {
response: Response
data: T | null
error: boolean
errorMessage?: string
} export interface UseFetchOptions {
...
responseHandler?: (ctx: CustomResponseFetchContext) => Promise<CustomResponseFetchContext>
} This will be optional on the configuration, and this will be used for response extraction if present on the configuration and will be used instead the current logic, that will remains as is. |
The logic to be modified is minimal inside |
@isgj how about this? |
see a9adad1 |
Something similar might work. |
Ok I'll add the callback call |
This makes no sense since you are handling the entire response, the after callback is the response handler |
I'll mention on docs |
@isgj can you take a look at https://github.com/vueuse/vueuse/blob/8d1f05fa232e8580186872c468b4b04b1d16cfb4/packages/core/useFetch/index.md go below events entry, I added 3 entries, for |
@isgj also added warning on |
what about we solve the problem like this: @@ -324,12 +324,12 @@ export function useFetch<T>(url: MaybeRef<string>, ...args: any[]): UseFetchRetu
response.value = fetchResponse
statusCode.value = fetchResponse.status
- let responseData = await fetchResponse[config.type]()
-
// see: https://www.tjvantoll.com/2015/09/13/fetch-and-errors/
if (!fetchResponse.ok)
throw new Error(fetchResponse.statusText)
+ let responseData = await fetchResponse[config.type]()
+
if (options.afterFetch)
({ data: responseData } = await options.afterFetch({ data: responseData, response: fetchResponse })) then the user can work with the response, since it wasnt consumed const {onFetchError, data, response} = useFetch('/update').json().put({/*the data*/})
onFetchError(async (error) => {
// response isnt consumed here, so i can do what i want with it
// e.g. check validation errors
if (response.value?.status == 400 || response.value?.status == 422) {
// get the validation errors
const validationErrors = await response.value.json()
console.log(validationErrors)
}
else {
// handle other server errors
}
}) |
Kinda better, at least one can have access to the data (response body). |
I don't know how I missed it, but I'm closing as duplicate of #526 |
At the moment when the status is not
ok
an error is thrown while fetching. In this casedata
will be null and the error will be populated. It will be nice to have the body of the response set todata
. Even if the status code is not in200 ...(ok)
the response still has a body.I think just throwing after setting the
data
will be okThe text was updated successfully, but these errors were encountered: