Skip to content

Commit 0848a33

Browse files
committed
feat(waiter-utils): adding reasons for waiter states and exceptions
1 parent 48ab750 commit 0848a33

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

packages/util-waiter/src/createWaiter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,10 @@ export const createWaiter = async <Client, Input>(
3434
if (options.abortController) {
3535
exitConditions.push(abortTimeout(options.abortController.signal));
3636
}
37+
38+
if (options.abortSignal) {
39+
exitConditions.push(abortTimeout(options.abortSignal));
40+
}
41+
3742
return Promise.race(exitConditions);
3843
};

packages/util-waiter/src/poller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const randomInRange = (min: number, max: number) => min + Math.random() * (max -
2121
* @param stateChecker function that checks the acceptor states on each poll.
2222
*/
2323
export const runPolling = async <Client, Input>(
24-
{ minDelay, maxDelay, maxWaitTime, abortController, client }: WaiterOptions<Client>,
24+
{ minDelay, maxDelay, maxWaitTime, abortController, client, abortSignal }: WaiterOptions<Client>,
2525
input: Input,
2626
acceptorChecks: (client: Client, input: Input) => Promise<WaiterResult>
2727
): Promise<WaiterResult> => {
@@ -36,7 +36,7 @@ export const runPolling = async <Client, Input>(
3636
// Pre-compute this number to avoid Number type overflow.
3737
const attemptCeiling = Math.log(maxDelay / minDelay) / Math.log(2) + 1;
3838
while (true) {
39-
if (abortController?.signal?.aborted) {
39+
if (abortController?.signal?.aborted || abortSignal?.aborted) {
4040
return { state: WaiterState.ABORTED };
4141
}
4242
const delay = exponentialBackoffWithJitter(minDelay, maxDelay, attemptCeiling, currentAttempt);

packages/util-waiter/src/waiter.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@ export interface WaiterConfiguration<Client> {
1212
maxWaitTime: number;
1313

1414
/**
15+
* @deprecated Use abortSignal
1516
* Abort controller. Used for ending the waiter early.
1617
*/
1718
abortController?: AbortController;
1819

20+
/**
21+
* Abort Signal. Used for ending the waiter early.
22+
*/
23+
abortSignal?: AbortController["signal"];
24+
1925
/**
2026
* The minimum amount of time to delay between retries in seconds. This is the
2127
* floor of the exponential backoff. This value defaults to service default
@@ -55,4 +61,28 @@ export enum WaiterState {
5561

5662
export type WaiterResult = {
5763
state: WaiterState;
64+
65+
/**
66+
* (optional) Indicates a reason for why a waiter has reached its state.
67+
*/
68+
reason?: any;
69+
};
70+
71+
/**
72+
* Handles and throws exceptions resulting from the waiterResult
73+
* @param result WaiterResult
74+
*/
75+
export const checkExceptions = (result: WaiterResult): WaiterResult => {
76+
if (result.state === WaiterState.ABORTED) {
77+
throw new Error(
78+
`${JSON.stringify({
79+
...result,
80+
name: "AbortError",
81+
reason: "Request was aborted",
82+
})}`
83+
);
84+
} else if (result.state !== WaiterState.SUCCESS) {
85+
throw new Error(`${JSON.stringify({ result })}`);
86+
}
87+
return result;
5888
};

0 commit comments

Comments
 (0)