mirror of
https://github.com/Mintplex-Labs/nut.js.git
synced 2026-07-19 14:06:17 -04:00
Feature/556/better timeout error messages (#557)
* (#556) Store intermediate results and errors to provide additional context on timeout * (#556) Update testcases
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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(),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 = () => {
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user