-
Notifications
You must be signed in to change notification settings - Fork 576
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add check for isTransientError (#1222)
- Loading branch information
Showing
8 changed files
with
135 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,58 @@ | ||
import { | ||
isClockSkewError, | ||
isThrottlingError, | ||
isTransientError | ||
} from "@aws-sdk/service-error-classification"; | ||
import { defaultRetryDecider } from "./retryDecider"; | ||
|
||
jest.mock("@aws-sdk/service-error-classification", () => ({ | ||
isClockSkewError: jest.fn().mockReturnValue(false), | ||
isThrottlingError: jest.fn().mockReturnValue(false), | ||
isTransientError: jest.fn().mockReturnValue(false) | ||
})); | ||
|
||
describe("defaultRetryDecider", () => { | ||
const createMockError = () => Object.assign(new Error(), { $metadata: {} }); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should return false when the provided error is falsy", () => { | ||
expect(defaultRetryDecider(null as any)).toBe(false); | ||
expect((isClockSkewError as jest.Mock).mock.calls.length).toBe(0); | ||
expect((isThrottlingError as jest.Mock).mock.calls.length).toBe(0); | ||
expect((isTransientError as jest.Mock).mock.calls.length).toBe(0); | ||
}); | ||
|
||
it("should return true if the error was tagged as a connection error", () => { | ||
const err: Error & { connectionError?: boolean } = new Error(); | ||
err.connectionError = true; | ||
expect(defaultRetryDecider(err)).toBe(true); | ||
it("should return true for ClockSkewError", () => { | ||
(isClockSkewError as jest.Mock).mockReturnValueOnce(true); | ||
expect(defaultRetryDecider(createMockError())).toBe(true); | ||
expect((isClockSkewError as jest.Mock).mock.calls.length).toBe(1); | ||
expect((isThrottlingError as jest.Mock).mock.calls.length).toBe(0); | ||
expect((isTransientError as jest.Mock).mock.calls.length).toBe(0); | ||
}); | ||
|
||
for (const httpStatusCode of [429, 500, 502, 503, 504, 509]) { | ||
it(`should return true if the error represents a service response with an HTTP status code of ${httpStatusCode}`, () => { | ||
const err: any = new Error(); | ||
err.$metadata = { httpStatusCode }; | ||
expect(defaultRetryDecider(err)).toBe(true); | ||
}); | ||
} | ||
|
||
it('should return true if the response represents a "still processing" error', () => { | ||
const err = new Error(); | ||
err.name = "PriorRequestNotComplete"; | ||
expect(defaultRetryDecider(err)).toBe(true); | ||
it("should return true for ThrottlingError", () => { | ||
(isThrottlingError as jest.Mock).mockReturnValueOnce(true); | ||
expect(defaultRetryDecider(createMockError())).toBe(true); | ||
expect((isClockSkewError as jest.Mock).mock.calls.length).toBe(1); | ||
expect((isThrottlingError as jest.Mock).mock.calls.length).toBe(1); | ||
expect((isTransientError as jest.Mock).mock.calls.length).toBe(0); | ||
}); | ||
|
||
it("should return true if the response represents a throttling error", () => { | ||
const err = new Error(); | ||
err.name = "TooManyRequestsException"; | ||
expect(defaultRetryDecider(err)).toBe(true); | ||
it("should return true for TransientError", () => { | ||
(isTransientError as jest.Mock).mockReturnValueOnce(true); | ||
expect(defaultRetryDecider(createMockError())).toBe(true); | ||
expect((isClockSkewError as jest.Mock).mock.calls.length).toBe(1); | ||
expect((isThrottlingError as jest.Mock).mock.calls.length).toBe(1); | ||
expect((isTransientError as jest.Mock).mock.calls.length).toBe(1); | ||
}); | ||
|
||
it("should return false for other errors", () => { | ||
expect(defaultRetryDecider(new Error())).toBe(false); | ||
expect(defaultRetryDecider(createMockError())).toBe(false); | ||
expect((isClockSkewError as jest.Mock).mock.calls.length).toBe(1); | ||
expect((isThrottlingError as jest.Mock).mock.calls.length).toBe(1); | ||
expect((isTransientError as jest.Mock).mock.calls.length).toBe(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,18 @@ | ||
import { RETRYABLE_STATUS_CODES } from "./constants"; | ||
import { | ||
isClockSkewError, | ||
isThrottlingError | ||
isThrottlingError, | ||
isTransientError | ||
} from "@aws-sdk/service-error-classification"; | ||
import { MetadataBearer, SdkError } from "@aws-sdk/types"; | ||
import { SdkError } from "@aws-sdk/types"; | ||
|
||
export const defaultRetryDecider = (error: SdkError) => { | ||
if (!error) { | ||
return false; | ||
} | ||
|
||
if (error.connectionError) { | ||
return true; | ||
} | ||
|
||
if ( | ||
hasMetadata(error) && | ||
error.$metadata.httpStatusCode && | ||
RETRYABLE_STATUS_CODES.has(error.$metadata.httpStatusCode) | ||
) { | ||
return true; | ||
} | ||
|
||
return isThrottlingError(error) || isClockSkewError(error); | ||
return ( | ||
isClockSkewError(error) || | ||
isThrottlingError(error) || | ||
isTransientError(error) | ||
); | ||
}; | ||
|
||
function hasMetadata(error: any): error is MetadataBearer { | ||
return error?.$metadata; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,17 @@ | ||
import { CLOCK_SKEW_ERROR_CODES, THROTTLING_ERROR_CODES } from "./constants"; | ||
import { | ||
CLOCK_SKEW_ERROR_CODES, | ||
THROTTLING_ERROR_CODES, | ||
TRANSIENT_ERROR_CODES, | ||
TRANSIENT_ERROR_STATUS_CODES | ||
} from "./constants"; | ||
import { SdkError } from "@aws-sdk/types"; | ||
|
||
export const isClockSkewError = (error: Error) => | ||
export const isClockSkewError = (error: SdkError) => | ||
CLOCK_SKEW_ERROR_CODES.includes(error.name); | ||
|
||
export const isThrottlingError = (error: Error) => | ||
export const isThrottlingError = (error: SdkError) => | ||
THROTTLING_ERROR_CODES.includes(error.name); | ||
|
||
export const isTransientError = (error: SdkError) => | ||
TRANSIENT_ERROR_CODES.includes(error.name) || | ||
TRANSIENT_ERROR_STATUS_CODES.includes(error.$metadata?.httpStatusCode || 0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters