-
Notifications
You must be signed in to change notification settings - Fork 129
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
Error response is not captured properly #454
Comments
I don't believe this is a bug. However, if you want to read the response of a fetch error you can do the following: import { FetchError, ofetch } from "ofetch";
try {
const res = await ofetch("https://example.com/123")
} catch (error) {
if(error instanceof FetchError) {
console.log(error.response)
}
} |
@feelixe I think my message misled you. You are absolutely right about the behaviour of the JavaScript. The issue is that this repo's code is written in such a way that it expects the response to have a value. See the code below, annotated with arrows try {
context.response = await fetch( <-- context.response will be null on error
context.request,
context.options as RequestInit
);
} catch (error) {
context.error = error as Error;
if (context.options.onRequestError) {
await callHooks(
context as FetchContext & { error: Error },
context.options.onRequestError
);
}
return await onError(context);
} finally {
if (abortTimeout) {
clearTimeout(abortTimeout);
}
} async function onError(context: FetchContext): Promise<FetchResponse<any>> {
// Is Abort
// If it is an active abort, it will not retry automatically.
// https://developer.mozilla.org/en-US/docs/Web/API/DOMException#error_names
const isAbort =
(context.error &&
context.error.name === "AbortError" &&
!context.options.timeout) ||
false;
// Retry
if (context.options.retry !== false && !isAbort) {
let retries;
if (typeof context.options.retry === "number") {
retries = context.options.retry;
} else {
retries = isPayloadMethod(context.options.method) ? 0 : 1;
}
const responseCode = (context.response && context.response.status) || 500; <--- responseCode=500, which causes the retry to keep executing when retries > 0 (default of retries value is more than zero if I not remember it wrong)
if (
retries > 0 &&
(Array.isArray(context.options.retryStatusCodes)
? context.options.retryStatusCodes.includes(responseCode)
: retryStatusCodes.has(responseCode))
) {
const retryDelay =
typeof context.options.retryDelay === "function"
? context.options.retryDelay(context)
: context.options.retryDelay || 0;
if (retryDelay > 0) {
await new Promise((resolve) => setTimeout(resolve, retryDelay));
}
// Timeout
return $fetchRaw(context.request, {
...context.options,
retry: retries - 1,
});
}
}
``` |
Thanks @kenng for the further explanation. Can you please make an updated reproduction then? (ie, it might be expected that you might want |
Environment
any version, as it is related to behaviour of try...catch
Reproduction
Sample snippet
Describe the bug
The response is not set when there is an error.
ofetch/src/fetch.ts
Line 186 in 6e6bd86
While the retry function relies on the response, this causes the retry function to execute unintentionally.
ofetch/src/fetch.ts
Line 62 in 6e6bd86
Additional context
No response
Logs
No response
The text was updated successfully, but these errors were encountered: