MCPcopy
hub / github.com/unjs/ofetch / onError

Function onError

src/fetch.ts:39–87  ·  view source on GitHub ↗
(context: FetchContext)

Source from the content-addressed store, hash-verified

37 const { fetch = globalThis.fetch } = globalOptions;
38
39 async function onError(context: FetchContext): Promise<FetchResponse<any>> {
40 // Is Abort
41 // If it is an active abort, it will not retry automatically.
42 // https://developer.mozilla.org/en-US/docs/Web/API/DOMException#error_names
43 const isAbort =
44 (context.error &&
45 context.error.name === "AbortError" &&
46 !context.options.timeout) ||
47 false;
48 // Retry
49 if (context.options.retry !== false && !isAbort) {
50 let retries;
51 if (typeof context.options.retry === "number") {
52 retries = context.options.retry;
53 } else {
54 retries = isPayloadMethod(context.options.method) ? 0 : 1;
55 }
56
57 const responseCode = (context.response && context.response.status) || 500;
58 if (
59 retries > 0 &&
60 (Array.isArray(context.options.retryStatusCodes)
61 ? context.options.retryStatusCodes.includes(responseCode)
62 : retryStatusCodes.has(responseCode))
63 ) {
64 const retryDelay =
65 typeof context.options.retryDelay === "function"
66 ? context.options.retryDelay(context)
67 : context.options.retryDelay || 0;
68 if (retryDelay > 0) {
69 await new Promise((resolve) => setTimeout(resolve, retryDelay));
70 }
71 // Timeout
72 return $fetchRaw(context.request, {
73 ...context.options,
74 retry: retries - 1,
75 });
76 }
77 }
78
79 // Throw normalized error
80 const error = createFetchError(context);
81
82 // Only available on V8 based runtimes (https://v8.dev/docs/stack-trace-api)
83 if (Error.captureStackTrace) {
84 Error.captureStackTrace(error, $fetchRaw);
85 }
86 throw error;
87 }
88
89 const $fetchRaw: $Fetch["raw"] = async function $fetchRaw<
90 T = any,

Callers 1

$fetchRawFunction · 0.85

Calls 3

isPayloadMethodFunction · 0.90
createFetchErrorFunction · 0.90
$fetchRawFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…