Skip to content

Commit b52b4e8

Browse files
jtmthfkuhe
andauthored
add support for error cause in transient error checks (#1461)
* add support for error cause in transient error checks * add recursion limit * add return type to isTransientError function --------- Co-authored-by: George Fu <[email protected]>
1 parent 0bec374 commit b52b4e8

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

.changeset/good-dots-run.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@smithy/service-error-classification": patch
3+
"@smithy/types": patch
4+
---
5+
6+
add support for error cause in transient error checks

packages/service-error-classification/src/index.spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@ const checkForErrorType = (
1515
name?: string;
1616
httpStatusCode?: number;
1717
$retryable?: RetryableTrait;
18+
cause?: Partial<Error>;
1819
},
1920
errorTypeResult: boolean
2021
) => {
21-
const { name, httpStatusCode, $retryable } = options;
22+
const { name, httpStatusCode, $retryable, cause } = options;
2223
const error = Object.assign(new Error(), {
2324
name,
2425
$metadata: { httpStatusCode },
2526
$retryable,
27+
cause,
2628
});
2729
expect(isErrorTypeFunc(error as SdkError)).toBe(errorTypeResult);
2830
};
@@ -127,6 +129,18 @@ describe("isTransientError", () => {
127129
break;
128130
}
129131
}
132+
133+
TRANSIENT_ERROR_CODES.forEach((name) => {
134+
it(`should declare error with cause with the name "${name}" to be a Transient error`, () => {
135+
checkForErrorType(isTransientError, { cause: { name } }, true);
136+
});
137+
});
138+
139+
it("should limit recursion to 10 depth", () => {
140+
const error = { cause: null } as SdkError;
141+
error.cause = error;
142+
checkForErrorType(isTransientError, { cause: error }, false);
143+
});
130144
});
131145

132146
describe("isServerError", () => {

packages/service-error-classification/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ export const isThrottlingError = (error: SdkError) =>
3131
* cause where the NodeHttpHandler does not decorate the Error with
3232
* the name "TimeoutError" to be checked by the TRANSIENT_ERROR_CODES condition.
3333
*/
34-
export const isTransientError = (error: SdkError) =>
34+
export const isTransientError = (error: SdkError, depth = 0): boolean =>
3535
isClockSkewCorrectedError(error) ||
3636
TRANSIENT_ERROR_CODES.includes(error.name) ||
3737
NODEJS_TIMEOUT_ERROR_CODES.includes((error as { code?: string })?.code || "") ||
38-
TRANSIENT_ERROR_STATUS_CODES.includes(error.$metadata?.httpStatusCode || 0);
38+
TRANSIENT_ERROR_STATUS_CODES.includes(error.$metadata?.httpStatusCode || 0) ||
39+
(error.cause !== undefined && depth <= 10 && isTransientError(error.cause, depth + 1));
3940

4041
export const isServerError = (error: SdkError) => {
4142
if (error.$metadata?.httpStatusCode !== undefined) {

packages/types/src/shapes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,5 @@ export type SdkError = Error &
9090
*/
9191
readonly clockSkewCorrected?: true;
9292
};
93+
cause?: Error;
9394
};

0 commit comments

Comments
 (0)