Skip to content

add support for error cause in transient error checks #1461

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

Merged
merged 3 commits into from
Dec 2, 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
6 changes: 6 additions & 0 deletions .changeset/good-dots-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@smithy/service-error-classification": patch
"@smithy/types": patch
---

add support for error cause in transient error checks
16 changes: 15 additions & 1 deletion packages/service-error-classification/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ const checkForErrorType = (
name?: string;
httpStatusCode?: number;
$retryable?: RetryableTrait;
cause?: Partial<Error>;
},
errorTypeResult: boolean
) => {
const { name, httpStatusCode, $retryable } = options;
const { name, httpStatusCode, $retryable, cause } = options;
const error = Object.assign(new Error(), {
name,
$metadata: { httpStatusCode },
$retryable,
cause,
});
expect(isErrorTypeFunc(error as SdkError)).toBe(errorTypeResult);
};
Expand Down Expand Up @@ -127,6 +129,18 @@ describe("isTransientError", () => {
break;
}
}

TRANSIENT_ERROR_CODES.forEach((name) => {
it(`should declare error with cause with the name "${name}" to be a Transient error`, () => {
checkForErrorType(isTransientError, { cause: { name } }, true);
});
});

it("should limit recursion to 10 depth", () => {
const error = { cause: null } as SdkError;
error.cause = error;
checkForErrorType(isTransientError, { cause: error }, false);
});
});

describe("isServerError", () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/service-error-classification/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ export const isThrottlingError = (error: SdkError) =>
* cause where the NodeHttpHandler does not decorate the Error with
* the name "TimeoutError" to be checked by the TRANSIENT_ERROR_CODES condition.
*/
export const isTransientError = (error: SdkError) =>
export const isTransientError = (error: SdkError, depth = 0): boolean =>
isClockSkewCorrectedError(error) ||
TRANSIENT_ERROR_CODES.includes(error.name) ||
NODEJS_TIMEOUT_ERROR_CODES.includes((error as { code?: string })?.code || "") ||
TRANSIENT_ERROR_STATUS_CODES.includes(error.$metadata?.httpStatusCode || 0);
TRANSIENT_ERROR_STATUS_CODES.includes(error.$metadata?.httpStatusCode || 0) ||
(error.cause !== undefined && depth <= 10 && isTransientError(error.cause, depth + 1));

export const isServerError = (error: SdkError) => {
if (error.$metadata?.httpStatusCode !== undefined) {
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/shapes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,5 @@ export type SdkError = Error &
*/
readonly clockSkewCorrected?: true;
};
cause?: Error;
};
Loading