Skip to content

Commit

Permalink
add support for error cause in transient error checks (smithy-lang#1461)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
jtmthf and kuhe authored Dec 2, 2024
1 parent 0bec374 commit b52b4e8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
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;
};

0 comments on commit b52b4e8

Please sign in to comment.