Skip to content
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
46 changes: 21 additions & 25 deletions packages/service-error-classification/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@
export type ErrorCodeSet = { [errorCode: string]: true };

/**
* Errors encountered when the client clock and server clock cannot agree on the
* current time.
*
* These errors are retryable, assuming the SDK has enabled clock skew
* correction.
*/
export const CLOCK_SKEW_ERROR_CODES: ErrorCodeSet = {
AuthFailure: true,
InvalidSignatureException: true,
RequestExpired: true,
RequestInTheFuture: true,
RequestTimeTooSkewed: true,
SignatureDoesNotMatch: true
};
export const CLOCK_SKEW_ERROR_CODES = [
"AuthFailure",
"InvalidSignatureException",
"RequestExpired",
"RequestInTheFuture",
"RequestTimeTooSkewed",
"SignatureDoesNotMatch"
];

/**
* Errors encountered when the state presumed by an operation is not yet ready.
*/
export const STILL_PROCESSING_ERROR_CODES: ErrorCodeSet = {
PriorRequestNotComplete: true
};
export const STILL_PROCESSING_ERROR_CODES = ["PriorRequestNotComplete"];

/**
* Errors that indicate the SDK is being throttled.
*
* These errors are always retryable.
*/
export const THROTTLING_ERROR_CODES: ErrorCodeSet = {
BandwidthLimitExceeded: true,
ProvisionedThroughputExceededException: true,
RequestLimitExceeded: true,
RequestThrottled: true,
RequestThrottledException: true,
SlowDown: true,
ThrottledException: true,
Throttling: true,
ThrottlingException: true,
TooManyRequestsException: true
};
export const THROTTLING_ERROR_CODES = [
"BandwidthLimitExceeded",
"ProvisionedThroughputExceededException",
"RequestLimitExceeded",
"RequestThrottled",
"RequestThrottledException",
"SlowDown",
"ThrottledException",
"Throttling",
"ThrottlingException",
"TooManyRequestsException"
];
64 changes: 49 additions & 15 deletions packages/service-error-classification/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,66 @@ import {
isThrottlingError
} from "./index";

const checkForErrorType = (
isErrorTypeFunc: (error: Error) => boolean,
errorName: string,
errorTypeResult: boolean
) => {
const error = new Error();
error.name = errorName;
expect(isErrorTypeFunc(error)).toBe(errorTypeResult);
};

describe("isClockSkewError", () => {
for (const name of Object.keys(CLOCK_SKEW_ERROR_CODES)) {
it(`should declare errors with the name ${name} to be throttling errors`, () => {
const error = new Error();
error.name = name;
expect(isClockSkewError(error)).toBe(true);
CLOCK_SKEW_ERROR_CODES.forEach(name => {
it(`should declare error with the name "${name}" to be a ClockSkew error`, () => {
checkForErrorType(isClockSkewError, name, true);
});
});

while (true) {
const name = Math.random().toString(36).substring(2);
if (!CLOCK_SKEW_ERROR_CODES.includes(name)) {
it(`should not declare error with the name "${name}" to be a ClockSkew error`, () => {
checkForErrorType(isClockSkewError, name, false);
});
break;
}
}
});

describe("isStillProcessingError", () => {
for (const name of Object.keys(STILL_PROCESSING_ERROR_CODES)) {
it(`should declare errors with the name ${name} to be throttling errors`, () => {
const error = new Error();
error.name = name;
expect(isStillProcessingError(error)).toBe(true);
STILL_PROCESSING_ERROR_CODES.forEach(name => {
it(`should declare error with the name "${name}" to be a StillProcessing error`, () => {
checkForErrorType(isStillProcessingError, name, true);
});
});

while (true) {
const name = Math.random().toString(36).substring(2);
if (!STILL_PROCESSING_ERROR_CODES.includes(name)) {
it(`should not declare error with the name "${name}" to be a StillProcessing error`, () => {
checkForErrorType(isStillProcessingError, name, false);
});
break;
}
}
});

describe("isThrottlingError", () => {
for (const name of Object.keys(THROTTLING_ERROR_CODES)) {
it(`should declare errors with the name ${name} to be throttling errors`, () => {
const error = new Error();
error.name = name;
expect(isThrottlingError(error)).toBe(true);
THROTTLING_ERROR_CODES.forEach(name => {
it(`should declare error with the name "${name}" to be a Throttling error`, () => {
checkForErrorType(isThrottlingError, name, true);
});
});

while (true) {
const name = Math.random().toString(36).substring(2);
if (!THROTTLING_ERROR_CODES.includes(name)) {
it(`should not declare error with the name "${name}" to be a Throttling error`, () => {
checkForErrorType(isThrottlingError, name, false);
});
break;
}
}
});
15 changes: 6 additions & 9 deletions packages/service-error-classification/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ import {
THROTTLING_ERROR_CODES
} from "./constants";

export function isClockSkewError(error: Error) {
return error.name in CLOCK_SKEW_ERROR_CODES;
}
export const isClockSkewError = (error: Error) =>
CLOCK_SKEW_ERROR_CODES.includes(error.name);

export function isStillProcessingError(error: Error): boolean {
return error.name in STILL_PROCESSING_ERROR_CODES;
}
export const isStillProcessingError = (error: Error) =>
STILL_PROCESSING_ERROR_CODES.includes(error.name);

export function isThrottlingError(error: Error): boolean {
return error.name in THROTTLING_ERROR_CODES;
}
export const isThrottlingError = (error: Error) =>
THROTTLING_ERROR_CODES.includes(error.name);