Skip to content

Commit 6161475

Browse files
committed
(#556) Store intermediate results and errors to provide additional context on timeout
1 parent 67a413a commit 6161475

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

lib/util/timeout.function.ts

+19-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ export function timeout<R>(
88
updateIntervalMs: number,
99
maxDurationMs: number,
1010
action: (...params: any) => Promise<R>,
11-
config?: TimoutConfig
11+
config?: TimoutConfig,
1212
): Promise<R> {
1313
return new Promise<R>((resolve, reject) => {
1414
let interval: NodeJS.Timeout;
1515
let timerCleaned = false;
16+
let lastResult: R | null;
17+
let lastRejectionReason: any | null;
1618

1719
if (config?.signal) {
1820
config.signal.onabort = () => {
@@ -29,12 +31,16 @@ export function timeout<R>(
2931
if (!result && !timerCleaned) {
3032
interval = setTimeout(executeInterval, updateIntervalMs);
3133
} else {
34+
lastResult = result;
35+
lastRejectionReason = null;
3236
cleanupTimer();
3337
resolve(result);
3438
}
3539
}
3640

37-
function handleRejection() {
41+
function handleRejection(reason: any) {
42+
lastRejectionReason = reason;
43+
lastResult = null;
3844
if (!timerCleaned) {
3945
interval = setTimeout(executeInterval, updateIntervalMs);
4046
}
@@ -52,7 +58,17 @@ export function timeout<R>(
5258

5359
const maxTimeout = setTimeout(() => {
5460
cleanupTimer();
55-
reject(`Action timed out after ${maxDurationMs} ms`);
61+
let additionalInformation: string | undefined;
62+
if (lastResult == null && lastRejectionReason != null) {
63+
additionalInformation = `Last rejection reason was: ${lastRejectionReason}.`;
64+
} else if (lastResult == null && lastRejectionReason == null) {
65+
additionalInformation = `Didn't receive a result within timeout.`;
66+
}
67+
reject(
68+
`Action timed out after ${maxDurationMs} ms.${
69+
additionalInformation ? ` ${additionalInformation}` : ""
70+
}`,
71+
);
5672
}, maxDurationMs);
5773

5874
executeInterval();

0 commit comments

Comments
 (0)