Skip to content

Commit 11344dd

Browse files
committed
refactor: extract timeout error handling into a separate function for better readability
1 parent dee619a commit 11344dd

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

packages/cli/src/lookup.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,18 @@ export function clearTimeoutSignal(signal?: AbortSignal): void {
177177
}
178178
}
179179

180+
function handleTimeoutError(
181+
spinner: { fail: (text: string) => void },
182+
timeoutSeconds?: number,
183+
url?: string,
184+
): void {
185+
const urlText = url ? ` for: ${colors.red(url)}` : "";
186+
spinner.fail(`Request timed out after ${timeoutSeconds} seconds${urlText}.`);
187+
console.error(
188+
"Try increasing the timeout with --timeout option or check network connectivity.",
189+
);
190+
}
191+
180192
function wrapDocumentLoaderWithTimeout(
181193
loader: DocumentLoader,
182194
timeoutSeconds?: number,
@@ -185,7 +197,9 @@ function wrapDocumentLoaderWithTimeout(
185197

186198
return (url: string, options?) => {
187199
const signal = createTimeoutSignal(timeoutSeconds);
188-
return loader(url, { ...options, signal });
200+
return loader(url, { ...options, signal }).finally(() =>
201+
clearTimeoutSignal(signal)
202+
);
189203
};
190204
}
191205

@@ -345,10 +359,7 @@ export const command = new Command()
345359
});
346360
} catch (error) {
347361
if (error instanceof Error && error.message.includes("timed out")) {
348-
spinner.fail(`Request timed out after ${options.timeout} seconds.`);
349-
console.error(
350-
"Try increasing the timeout with --timeout option or check network connectivity.",
351-
);
362+
handleTimeoutError(spinner, options.timeout, url);
352363
} else {
353364
spinner.fail(`Failed to fetch object: ${colors.red(url)}.`);
354365
if (authLoader == null) {
@@ -395,10 +406,7 @@ export const command = new Command()
395406
} catch (error) {
396407
logger.error("Failed to complete the traversal: {error}", { error });
397408
if (error instanceof Error && error.message.includes("timed out")) {
398-
spinner.fail(`Request timed out after ${options.timeout} seconds.`);
399-
console.error(
400-
"Try increasing the timeout with --timeout option or check network connectivity.",
401-
);
409+
handleTimeoutError(spinner, options.timeout);
402410
} else {
403411
spinner.fail("Failed to complete the traversal.");
404412
if (authLoader == null) {
@@ -432,14 +440,7 @@ export const command = new Command()
432440
},
433441
).catch((error) => {
434442
if (error instanceof Error && error.message.includes("timed out")) {
435-
spinner.fail(
436-
`Request timed out after ${options.timeout} seconds for: ${
437-
colors.red(url)
438-
}.`,
439-
);
440-
console.error(
441-
"Try increasing the timeout with --timeout option or check network connectivity.",
442-
);
443+
handleTimeoutError(spinner, options.timeout, url);
443444
}
444445
throw error;
445446
}),

0 commit comments

Comments
 (0)