Skip to content

Commit 1d11480

Browse files
authored
fix(util-waiter): return stringified result object in case of failure (#1314)
1 parent 1239f8d commit 1d11480

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

.changeset/giant-swans-trade.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smithy/util-waiter": patch
3+
---
4+
5+
Return stringified result object in case of failure
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { checkExceptions, WaiterState } from "./waiter";
2+
3+
describe(checkExceptions.name, () => {
4+
const reason = "generic reason";
5+
6+
it(`throw AbortError if state is ${WaiterState.ABORTED}`, () => {
7+
const result = { state: WaiterState.ABORTED, reason };
8+
expect(() => checkExceptions(result)).toThrow({
9+
name: "AbortError",
10+
message: JSON.stringify({ ...result, reason: "Request was aborted" }),
11+
});
12+
});
13+
14+
it(`throw TimeoutError if state is ${WaiterState.TIMEOUT}`, () => {
15+
const result = { state: WaiterState.TIMEOUT, reason };
16+
expect(() => checkExceptions(result)).toThrow({
17+
name: "TimeoutError",
18+
message: JSON.stringify({ ...result, reason: "Waiter has timed out" }),
19+
});
20+
});
21+
22+
it(`throw generic Error if state is ${WaiterState.RETRY}`, () => {
23+
const result = { state: WaiterState.RETRY, reason };
24+
expect(() => checkExceptions(result)).toThrow({
25+
name: "Error",
26+
message: JSON.stringify(result),
27+
});
28+
});
29+
30+
it(`throw generic Error if state is ${WaiterState.FAILURE}`, () => {
31+
const result = { state: WaiterState.FAILURE, reason };
32+
expect(() => checkExceptions(result)).toThrow({
33+
name: "Error",
34+
message: JSON.stringify(result),
35+
});
36+
});
37+
38+
it(`return result if state is ${WaiterState.SUCCESS}`, () => {
39+
const result = { state: WaiterState.SUCCESS };
40+
expect(checkExceptions(result)).toEqual(result);
41+
});
42+
});

packages/util-waiter/src/waiter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export const checkExceptions = (result: WaiterResult): WaiterResult => {
6868
timeoutError.name = "TimeoutError";
6969
throw timeoutError;
7070
} else if (result.state !== WaiterState.SUCCESS) {
71-
throw new Error(`${JSON.stringify({ result })}`);
71+
throw new Error(`${JSON.stringify(result)}`);
7272
}
7373
return result;
7474
};

0 commit comments

Comments
 (0)