Skip to content

Commit 8955ba5

Browse files
jeskewsrchase
authored andcommitted
Add retry middleware
1 parent 4e95d08 commit 8955ba5

File tree

7 files changed

+49
-60
lines changed

7 files changed

+49
-60
lines changed

packages/middleware-content-length/src/index.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,16 @@ import {
33
BuildHandlerArguments,
44
BuildMiddleware,
55
BodyLengthCalculator,
6+
MetadataBearer,
67
} from '@aws/types';
78

8-
export function contentLengthMiddleware<
9-
Input extends object,
10-
Output extends object,
11-
Stream
12-
>(
9+
export function contentLengthMiddleware(
1310
bodyLengthCalculator: BodyLengthCalculator
14-
): BuildMiddleware<Input, Output, Stream> {
15-
return (
16-
next: BuildHandler<Input, Output, Stream>
17-
): BuildHandler<Input, Output, Stream> => async (
18-
args: BuildHandlerArguments<Input, Stream>
11+
): BuildMiddleware<any, any, any> {
12+
return <Output extends MetadataBearer>(
13+
next: BuildHandler<any, Output, any>
14+
): BuildHandler<any, Output, any> => async (
15+
args: BuildHandlerArguments<any, any>
1916
): Promise<Output> => {
2017
const {request} = args;
2118
if (!request) {
@@ -29,7 +26,10 @@ export function contentLengthMiddleware<
2926
.map(str => str.toLowerCase())
3027
.indexOf('content-length') === -1
3128
) {
32-
headers['Content-Length'] = String(bodyLengthCalculator(body));
29+
const length = bodyLengthCalculator(body);
30+
if (length !== undefined) {
31+
headers['Content-Length'] = String(length);
32+
}
3333
}
3434

3535
return next({

packages/service-error-classification/LICENSE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Apache License
1+
Apache License
22
Version 2.0, January 2004
33
http://www.apache.org/licenses/
44

@@ -198,4 +198,4 @@ Apache License
198198
distributed under the License is distributed on an "AS IS" BASIS,
199199
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200200
See the License for the specific language governing permissions and
201-
limitations under the License.
201+
limitations under the License.
Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,42 @@
1+
export type ErrorCodeSet = {[errorCode: string]: true};
2+
13
/**
24
* Errors encountered when the client clock and server clock cannot agree on the
35
* current time.
46
*
57
* These errors are retryable, assuming the SDK has enabled clock skew
68
* correction.
79
*/
8-
export const CLOCK_SKEW_ERROR_CODES = new Set<string>();
10+
export const CLOCK_SKEW_ERROR_CODES: ErrorCodeSet = {
11+
AuthFailure: true,
12+
InvalidSignatureException: true,
13+
RequestExpired: true,
14+
RequestInTheFuture: true,
15+
RequestTimeTooSkewed: true,
16+
SignatureDoesNotMatch: true,
17+
};
918

1019
/**
11-
* Errors encountered when the state an operation presumes is not yet ready.
20+
* Errors encountered when the state presumed by an operation is not yet ready.
1221
*/
13-
export const STILL_PROCESSING_ERROR_CODES = new Set<string>();
22+
export const STILL_PROCESSING_ERROR_CODES: ErrorCodeSet = {
23+
PriorRequestNotComplete: true,
24+
}
1425

1526
/**
1627
* Errors that indicate the SDK is being throttled.
1728
*
1829
* These errors are always retryable.
1930
*/
20-
export const THROTTLING_ERROR_CODES = new Set<string>();
21-
22-
// Populate the error code sets. This is not done in the Set constructor to
23-
// maintain compatibility with IE 11.
24-
25-
[
26-
'PriorRequestNotComplete',
27-
].forEach(code => STILL_PROCESSING_ERROR_CODES.add(code));
28-
29-
[
30-
'AuthFailure',
31-
'InvalidSignatureException',
32-
'RequestExpired',
33-
'RequestInTheFuture',
34-
'RequestTimeTooSkewed',
35-
'SignatureDoesNotMatch',
36-
].forEach(code => CLOCK_SKEW_ERROR_CODES.add(code));
37-
38-
[
39-
'BandwidthLimitExceeded',
40-
'ProvisionedThroughputExceededException',
41-
'RequestLimitExceeded',
42-
'RequestThrottled',
43-
'RequestThrottledException',
44-
'SlowDown',
45-
'ThrottledException',
46-
'Throttling',
47-
'ThrottlingException',
48-
'TooManyRequestsException',
49-
].forEach(code => THROTTLING_ERROR_CODES.add(code));
31+
export const THROTTLING_ERROR_CODES: ErrorCodeSet = {
32+
BandwidthLimitExceeded: true,
33+
ProvisionedThroughputExceededException: true,
34+
RequestLimitExceeded: true,
35+
RequestThrottled: true,
36+
RequestThrottledException: true,
37+
SlowDown: true,
38+
ThrottledException: true,
39+
Throttling: true,
40+
ThrottlingException: true,
41+
TooManyRequestsException: true,
42+
};

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from "./index";
1111

1212
describe('isClockSkewError', () => {
13-
for (const name of CLOCK_SKEW_ERROR_CODES) {
13+
for (const name of Object.keys(CLOCK_SKEW_ERROR_CODES)) {
1414
it(
1515
`should declare errors with the name ${name} to be throttling errors`,
1616
() => {
@@ -23,7 +23,7 @@ describe('isClockSkewError', () => {
2323
});
2424

2525
describe('isStillProcessingError', () => {
26-
for (const name of STILL_PROCESSING_ERROR_CODES) {
26+
for (const name of Object.keys(STILL_PROCESSING_ERROR_CODES)) {
2727
it(
2828
`should declare errors with the name ${name} to be throttling errors`,
2929
() => {
@@ -36,7 +36,7 @@ describe('isStillProcessingError', () => {
3636
});
3737

3838
describe('isThrottlingError', () => {
39-
for (const name of THROTTLING_ERROR_CODES) {
39+
for (const name of Object.keys(THROTTLING_ERROR_CODES)) {
4040
it(
4141
`should declare errors with the name ${name} to be throttling errors`,
4242
() => {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import {
55
} from './constants';
66

77
export function isClockSkewError(error: Error) {
8-
return CLOCK_SKEW_ERROR_CODES.has(error.name);
8+
return error.name in CLOCK_SKEW_ERROR_CODES;
99
}
1010

1111
export function isStillProcessingError(error: Error): boolean {
12-
return STILL_PROCESSING_ERROR_CODES.has(error.name);
12+
return error.name in STILL_PROCESSING_ERROR_CODES;
1313
}
1414

1515
export function isThrottlingError(error: Error): boolean {
16-
return THROTTLING_ERROR_CODES.has(error.name);
16+
return error.name in THROTTLING_ERROR_CODES;
1717
}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
export function calculateBodyLength(body: any): number {
2+
export function calculateBodyLength(body: any): number|undefined {
33
if (typeof body === 'string') {
44
return body.length;
55
} else if (typeof body.byteLength === 'number') {
@@ -9,6 +9,4 @@ export function calculateBodyLength(body: any): number {
99
// handles browser File object
1010
return body.size;
1111
}
12-
13-
throw new Error(`Could not determine size of ${body}`);
14-
}
12+
}

packages/util-body-length-node/src/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {lstatSync} from 'fs';
22

3-
export function calculateBodyLength(body: any): number {
3+
export function calculateBodyLength(body: any): number|undefined {
44
if (!body) {
55
return 0;
66
}
@@ -15,6 +15,4 @@ export function calculateBodyLength(body: any): number {
1515
// handles fs readable streams
1616
return lstatSync(body.path).size;
1717
}
18-
19-
throw new Error(`Could not determine size of ${body}`);
20-
}
18+
}

0 commit comments

Comments
 (0)