Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/556/better timeout error messages #557

Merged
merged 2 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading