Skip to content

Commit 4895ac7

Browse files
authored
chore: simplify error codes to be an array of strings (#1203)
1 parent 1074b06 commit 4895ac7

File tree

3 files changed

+76
-49
lines changed

3 files changed

+76
-49
lines changed
+21-25
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,38 @@
1-
export type ErrorCodeSet = { [errorCode: string]: true };
2-
31
/**
42
* Errors encountered when the client clock and server clock cannot agree on the
53
* current time.
64
*
75
* These errors are retryable, assuming the SDK has enabled clock skew
86
* correction.
97
*/
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-
};
8+
export const CLOCK_SKEW_ERROR_CODES = [
9+
"AuthFailure",
10+
"InvalidSignatureException",
11+
"RequestExpired",
12+
"RequestInTheFuture",
13+
"RequestTimeTooSkewed",
14+
"SignatureDoesNotMatch"
15+
];
1816

1917
/**
2018
* Errors encountered when the state presumed by an operation is not yet ready.
2119
*/
22-
export const STILL_PROCESSING_ERROR_CODES: ErrorCodeSet = {
23-
PriorRequestNotComplete: true
24-
};
20+
export const STILL_PROCESSING_ERROR_CODES = ["PriorRequestNotComplete"];
2521

2622
/**
2723
* Errors that indicate the SDK is being throttled.
2824
*
2925
* These errors are always retryable.
3026
*/
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-
};
27+
export const THROTTLING_ERROR_CODES = [
28+
"BandwidthLimitExceeded",
29+
"ProvisionedThroughputExceededException",
30+
"RequestLimitExceeded",
31+
"RequestThrottled",
32+
"RequestThrottledException",
33+
"SlowDown",
34+
"ThrottledException",
35+
"Throttling",
36+
"ThrottlingException",
37+
"TooManyRequestsException"
38+
];

Diff for: packages/service-error-classification/src/index.spec.ts

+49-15
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,66 @@ import {
99
isThrottlingError
1010
} from "./index";
1111

12+
const checkForErrorType = (
13+
isErrorTypeFunc: (error: Error) => boolean,
14+
errorName: string,
15+
errorTypeResult: boolean
16+
) => {
17+
const error = new Error();
18+
error.name = errorName;
19+
expect(isErrorTypeFunc(error)).toBe(errorTypeResult);
20+
};
21+
1222
describe("isClockSkewError", () => {
13-
for (const name of Object.keys(CLOCK_SKEW_ERROR_CODES)) {
14-
it(`should declare errors with the name ${name} to be throttling errors`, () => {
15-
const error = new Error();
16-
error.name = name;
17-
expect(isClockSkewError(error)).toBe(true);
23+
CLOCK_SKEW_ERROR_CODES.forEach(name => {
24+
it(`should declare error with the name "${name}" to be a ClockSkew error`, () => {
25+
checkForErrorType(isClockSkewError, name, true);
1826
});
27+
});
28+
29+
while (true) {
30+
const name = Math.random().toString(36).substring(2);
31+
if (!CLOCK_SKEW_ERROR_CODES.includes(name)) {
32+
it(`should not declare error with the name "${name}" to be a ClockSkew error`, () => {
33+
checkForErrorType(isClockSkewError, name, false);
34+
});
35+
break;
36+
}
1937
}
2038
});
2139

2240
describe("isStillProcessingError", () => {
23-
for (const name of Object.keys(STILL_PROCESSING_ERROR_CODES)) {
24-
it(`should declare errors with the name ${name} to be throttling errors`, () => {
25-
const error = new Error();
26-
error.name = name;
27-
expect(isStillProcessingError(error)).toBe(true);
41+
STILL_PROCESSING_ERROR_CODES.forEach(name => {
42+
it(`should declare error with the name "${name}" to be a StillProcessing error`, () => {
43+
checkForErrorType(isStillProcessingError, name, true);
2844
});
45+
});
46+
47+
while (true) {
48+
const name = Math.random().toString(36).substring(2);
49+
if (!STILL_PROCESSING_ERROR_CODES.includes(name)) {
50+
it(`should not declare error with the name "${name}" to be a StillProcessing error`, () => {
51+
checkForErrorType(isStillProcessingError, name, false);
52+
});
53+
break;
54+
}
2955
}
3056
});
3157

3258
describe("isThrottlingError", () => {
33-
for (const name of Object.keys(THROTTLING_ERROR_CODES)) {
34-
it(`should declare errors with the name ${name} to be throttling errors`, () => {
35-
const error = new Error();
36-
error.name = name;
37-
expect(isThrottlingError(error)).toBe(true);
59+
THROTTLING_ERROR_CODES.forEach(name => {
60+
it(`should declare error with the name "${name}" to be a Throttling error`, () => {
61+
checkForErrorType(isThrottlingError, name, true);
3862
});
63+
});
64+
65+
while (true) {
66+
const name = Math.random().toString(36).substring(2);
67+
if (!THROTTLING_ERROR_CODES.includes(name)) {
68+
it(`should not declare error with the name "${name}" to be a Throttling error`, () => {
69+
checkForErrorType(isThrottlingError, name, false);
70+
});
71+
break;
72+
}
3973
}
4074
});

Diff for: packages/service-error-classification/src/index.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@ import {
44
THROTTLING_ERROR_CODES
55
} from "./constants";
66

7-
export function isClockSkewError(error: Error) {
8-
return error.name in CLOCK_SKEW_ERROR_CODES;
9-
}
7+
export const isClockSkewError = (error: Error) =>
8+
CLOCK_SKEW_ERROR_CODES.includes(error.name);
109

11-
export function isStillProcessingError(error: Error): boolean {
12-
return error.name in STILL_PROCESSING_ERROR_CODES;
13-
}
10+
export const isStillProcessingError = (error: Error) =>
11+
STILL_PROCESSING_ERROR_CODES.includes(error.name);
1412

15-
export function isThrottlingError(error: Error): boolean {
16-
return error.name in THROTTLING_ERROR_CODES;
17-
}
13+
export const isThrottlingError = (error: Error) =>
14+
THROTTLING_ERROR_CODES.includes(error.name);

0 commit comments

Comments
 (0)