Skip to content

Commit

Permalink
Feature/556/better timeout error messages (#557)
Browse files Browse the repository at this point in the history
* (#556) Store intermediate results and errors to provide additional context on timeout

* (#556) Update testcases
  • Loading branch information
s1hofmann authored Feb 3, 2024
1 parent 67a413a commit 2bc3c60
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
22 changes: 16 additions & 6 deletions lib/util/timeout.function.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ describe("timeout", () => {
try {
await timeout(updateInterval, maxDuration, action);
} catch (e) {
expect(e).toBe(`Action timed out after ${maxDuration} ms`);
expect(e).toBe(
`Action timed out after ${maxDuration} ms. Last rejection reason was: false.`,
);
}
const end = Date.now();

Expand All @@ -37,7 +39,9 @@ describe("timeout", () => {
try {
await timeout(updateInterval, maxDuration, action);
} catch (e) {
expect(e).toEqual(`Action timed out after ${maxDuration} ms`);
expect(e).toEqual(
`Action timed out after ${maxDuration} ms. Didn't receive a result within timeout.`,
);
}
const end = Date.now();

Expand Down Expand Up @@ -90,7 +94,7 @@ describe("timeout", () => {
const action = jest.fn(() => {
const interval = Date.now() - start;
return new Promise<boolean>((resolve, reject) =>
interval > delay ? resolve(true) : reject()
interval > delay ? resolve(true) : reject(),
);
});

Expand All @@ -114,7 +118,9 @@ describe("timeout", () => {
try {
await timeout(updateInterval, maxDuration, action);
} catch (e) {
expect(e).toEqual(`Action timed out after ${maxDuration} ms`);
expect(e).toEqual(
`Action timed out after ${maxDuration} ms. Didn't receive a result within timeout.`,
);
}
const end = Date.now();

Expand All @@ -137,7 +143,9 @@ describe("timeout", () => {
const SUT = () => timeout(updateInterval, maxDuration, action);

// THEN
await expect(SUT).rejects.toBe(`Action timed out after ${maxDuration} ms`);
await expect(SUT).rejects.toBe(
`Action timed out after ${maxDuration} ms. Didn't receive a result within timeout.`,
);
expect(action).toBeCalledTimes(1);
});

Expand All @@ -157,7 +165,9 @@ describe("timeout", () => {
const SUT = () => timeout(updateInterval, maxDuration, action);

// THEN
await expect(SUT).rejects.toBe(`Action timed out after ${maxDuration} ms`);
await expect(SUT).rejects.toBe(
`Action timed out after ${maxDuration} ms. Didn't receive a result within timeout.`,
);
expect(action).toBeCalledTimes(1);
await sleep(500);
expect(action).toBeCalledTimes(1);
Expand Down
22 changes: 19 additions & 3 deletions lib/util/timeout.function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ export function timeout<R>(
updateIntervalMs: number,
maxDurationMs: number,
action: (...params: any) => Promise<R>,
config?: TimoutConfig
config?: TimoutConfig,
): Promise<R> {
return new Promise<R>((resolve, reject) => {
let interval: NodeJS.Timeout;
let timerCleaned = false;
let lastResult: R | null;
let lastRejectionReason: any | null;

if (config?.signal) {
config.signal.onabort = () => {
Expand All @@ -29,12 +31,16 @@ export function timeout<R>(
if (!result && !timerCleaned) {
interval = setTimeout(executeInterval, updateIntervalMs);
} else {
lastResult = result;
lastRejectionReason = null;
cleanupTimer();
resolve(result);
}
}

function handleRejection() {
function handleRejection(reason: any) {
lastRejectionReason = reason;
lastResult = null;
if (!timerCleaned) {
interval = setTimeout(executeInterval, updateIntervalMs);
}
Expand All @@ -52,7 +58,17 @@ export function timeout<R>(

const maxTimeout = setTimeout(() => {
cleanupTimer();
reject(`Action timed out after ${maxDurationMs} ms`);
let additionalInformation: string | undefined;
if (lastResult == null && lastRejectionReason != null) {
additionalInformation = `Last rejection reason was: ${lastRejectionReason}.`;
} else if (lastResult == null && lastRejectionReason == null) {
additionalInformation = `Didn't receive a result within timeout.`;
}
reject(
`Action timed out after ${maxDurationMs} ms.${
additionalInformation ? ` ${additionalInformation}` : ""
}`,
);
}, maxDurationMs);

executeInterval();
Expand Down

0 comments on commit 2bc3c60

Please sign in to comment.