mirror of
https://github.com/Azure/cosmos-explorer.git
synced 2026-06-08 13:37:29 +01:00
Users/chskelt/debug logging2 (#2503)
* Adding further console logging but also fixed lines with JSON.stringify(error) * Added retry mechanism for GETs for armRequest workflow * Moved stringifyError into its own file as it was causing strict compile issues in ErrorHandlingUtils
This commit is contained in:
@@ -3,6 +3,11 @@
|
||||
*
|
||||
* Usage: await fetchWithTimeout(url, { method: 'GET', headers: {...} }, 10000);
|
||||
*
|
||||
* If `init.signal` is provided, it is combined with the internal timeout: aborting
|
||||
* the caller's signal aborts the fetch (propagating the caller's abort reason), and
|
||||
* the timeout still applies. Pass `timeoutMs: Infinity` to disable the timeout entirely
|
||||
* (useful for long-running operations that should rely solely on caller cancellation).
|
||||
*
|
||||
* A shared helper to remove duplicated inline implementations across the codebase.
|
||||
*/
|
||||
export async function fetchWithTimeout(
|
||||
@@ -10,13 +15,30 @@ export async function fetchWithTimeout(
|
||||
init: RequestInit = {},
|
||||
timeoutMs: number = 5000,
|
||||
): Promise<Response> {
|
||||
const externalSignal = init.signal;
|
||||
if (externalSignal?.aborted) {
|
||||
throw externalSignal.reason ?? new DOMException("The operation was aborted.", "AbortError");
|
||||
}
|
||||
|
||||
const controller = new AbortController();
|
||||
const id = setTimeout(() => controller.abort(), timeoutMs);
|
||||
const hasTimeout = Number.isFinite(timeoutMs);
|
||||
const timeoutId = hasTimeout ? setTimeout(() => controller.abort(), timeoutMs) : undefined;
|
||||
|
||||
let onExternalAbort: (() => void) | undefined;
|
||||
if (externalSignal) {
|
||||
onExternalAbort = () => controller.abort(externalSignal.reason);
|
||||
externalSignal.addEventListener("abort", onExternalAbort, { once: true });
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(url, { ...init, signal: controller.signal });
|
||||
return response;
|
||||
return await fetch(url, { ...init, signal: controller.signal });
|
||||
} finally {
|
||||
clearTimeout(id);
|
||||
if (timeoutId !== undefined) {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
if (externalSignal && onExternalAbort) {
|
||||
externalSignal.removeEventListener("abort", onExternalAbort);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user