@factorearth/recordmiddleware-errorhandler 0.0.1-y.24 → 0.0.1-y.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Given the error from the dynamodb operation, dertermines if we can retry it, based on the information from the dynamodb sdk docs
3
+ * {@link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Programming.Errors.html}
4
+ * @author Eric Webb <ewebb@factorearth.com>
5
+ * @param err The error thrown by the dynamodb operation
6
+ * @returns Whether or not we can retry the operation
7
+ */
8
+ export declare function determineRetryableDynamo(err: any): boolean;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Given the error from the dynamodb operation, dertermines if we can retry it, based on the information from the dynamodb sdk docs
3
+ * {@link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Programming.Errors.html}
4
+ * @author Eric Webb <ewebb@factorearth.com>
5
+ * @param err The error thrown by the dynamodb operation
6
+ * @returns Whether or not we can retry the operation
7
+ */
8
+ export function determineRetryableDynamo(err) {
9
+ if (!err?.name)
10
+ return false;
11
+ // The errors from the link I provided that are marked as retryable
12
+ const retryableErrors = new Set(["ItemCollectionSizeLimitExceededException", "LimitExceededException", "ProvisionedThroughputExceededException", "RequestLimitExceeded", "ThrottlingException"]);
13
+ return retryableErrors.has(err.name) || (err.$metadata && err.$metadata.httpStatusCode && err.$metadata.httpStatusCode >= 500);
14
+ }
@@ -0,0 +1,28 @@
1
+ interface BackoffOptions {
2
+ /**
3
+ * The initial delay you want to apply. Default to 100ms
4
+ */
5
+ delay?: number;
6
+ /**
7
+ * The number of times you want to attempt the function. Default 10
8
+ */
9
+ numberOfAttempts?: number;
10
+ /**
11
+ * An optional max delay
12
+ */
13
+ maxDelay?: number;
14
+ /**
15
+ * An initial multiplier to be taken to the power of the attempt number when deciding how long to wait. Default value of 2
16
+ */
17
+ timeMultiple?: number;
18
+ }
19
+ /**
20
+ * Given a task, attempts to execute it a designated number of times, returning the eventual response
21
+ * @author Eric Webb <ewebb@factorearth.com>
22
+ * @param task The task we want to retry potentially
23
+ * @param evaluator A function to evaluate if the task executed successfully, or if it needs to be retried. Needs to return boolean
24
+ * @param options The configurable options for the module
25
+ * @returns The result of the function
26
+ */
27
+ export declare function backoff<TaskResult, TaskArguments extends any[]>(task: (...args: TaskArguments) => Promise<TaskResult>, evaluator: (taskResult: TaskResult) => boolean, options?: BackoffOptions, ...taskArguments: TaskArguments): Promise<TaskResult | void>;
28
+ export {};
@@ -0,0 +1,36 @@
1
+ import { delay } from "./delay";
2
+ // Default values
3
+ const DELAY = 100;
4
+ const NUMBER_OF_ATTEMPTS = 10;
5
+ const MAX_DELAY = Infinity;
6
+ const TIME_MULTIPLE = 2;
7
+ /**
8
+ * Given a task, attempts to execute it a designated number of times, returning the eventual response
9
+ * @author Eric Webb <ewebb@factorearth.com>
10
+ * @param task The task we want to retry potentially
11
+ * @param evaluator A function to evaluate if the task executed successfully, or if it needs to be retried. Needs to return boolean
12
+ * @param options The configurable options for the module
13
+ * @returns The result of the function
14
+ */
15
+ export async function backoff(task, evaluator, options, ...taskArguments) {
16
+ let attemptNumber = 0;
17
+ const maxAttempts = options?.numberOfAttempts || NUMBER_OF_ATTEMPTS;
18
+ const initialDelay = options?.delay || DELAY;
19
+ const maxDelay = options?.maxDelay || MAX_DELAY;
20
+ const timeMultiple = options?.timeMultiple || TIME_MULTIPLE;
21
+ while (attemptNumber < maxAttempts) {
22
+ const calculatedDelay = initialDelay * Math.pow(timeMultiple, attemptNumber);
23
+ const delayTime = Math.min(calculatedDelay, maxDelay);
24
+ await delay(delayTime);
25
+ try {
26
+ const taskResult = await task(...taskArguments);
27
+ const evaluatorResult = evaluator(taskResult);
28
+ if (evaluatorResult || !(attemptNumber + 1 < maxAttempts))
29
+ return taskResult;
30
+ }
31
+ catch (err) {
32
+ // recordSentry({ error: err, msg: "Error executing backoff task" });
33
+ }
34
+ attemptNumber++;
35
+ }
36
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Creates a promise that resolvers after a provided number of ms
3
+ * @author Eric Webb <ewebb@factorearth.com>
4
+ * @param millis The number of ms you want to sleep
5
+ * @returns A void promise
6
+ */
7
+ export declare function delay(millis: number): Promise<any>;
package/dist/delay.js ADDED
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Creates a promise that resolvers after a provided number of ms
3
+ * @author Eric Webb <ewebb@factorearth.com>
4
+ * @param millis The number of ms you want to sleep
5
+ * @returns A void promise
6
+ */
7
+ export function delay(millis) {
8
+ return new Promise((resolve, _reject) => setTimeout(resolve, millis));
9
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Takes a given error, determines if we can bother retrying the operation, and re-attempts the operation
3
+ * @author Eric Webb <ewebb@factorearth.com>
4
+ * @param err The error thrown by the dynamodb sdk
5
+ * @param task The task that we need to try and perform, if it can be retried
6
+ * @param evaluator The function that evaluates the success of the task
7
+ * @param taskArguments The arguments we need to pass into the task
8
+ * @returns The result of the task, or the error that prevented it from executing
9
+ */
10
+ export declare function handler<TaskResult, TaskArguments extends any[]>(err: unknown, task: (...args: TaskArguments[]) => Promise<TaskResult>, evaluator: (result: TaskResult) => boolean, ...taskArguments: TaskArguments): Promise<unknown>;
@@ -0,0 +1,19 @@
1
+ import { determineRetryableDynamo } from "./aws-services/dynamodb/determineRetryableDynamo";
2
+ import { backoff } from "./backOff";
3
+ /**
4
+ * Takes a given error, determines if we can bother retrying the operation, and re-attempts the operation
5
+ * @author Eric Webb <ewebb@factorearth.com>
6
+ * @param err The error thrown by the dynamodb sdk
7
+ * @param task The task that we need to try and perform, if it can be retried
8
+ * @param evaluator The function that evaluates the success of the task
9
+ * @param taskArguments The arguments we need to pass into the task
10
+ * @returns The result of the task, or the error that prevented it from executing
11
+ */
12
+ export async function handler(err, task, evaluator, ...taskArguments) {
13
+ const canBeRetried = determineRetryableDynamo(err);
14
+ if (canBeRetried) {
15
+ const result = await backoff(task, evaluator, undefined, ...taskArguments);
16
+ return result;
17
+ }
18
+ return err;
19
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./aws-services/dynamodb/determineRetryableDynamo";
2
+ export * from "./backOff";
3
+ export * from "./handler";
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./aws-services/dynamodb/determineRetryableDynamo";
2
+ export * from "./backOff";
3
+ export * from "./handler";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@factorearth/recordmiddleware-errorhandler",
3
- "version": "0.0.1-y.24",
3
+ "version": "0.0.1-y.25",
4
4
  "description": "A module for handling CRUD errors in lambda",
5
5
  "author": "madtrx <marlin.makori@gmail.com>",
6
6
  "homepage": "https://github.com/FactorEarth/RecordMiddleware#readme",
@@ -24,5 +24,5 @@
24
24
  "access": "public",
25
25
  "registry": "https://registry.npmjs.org/"
26
26
  },
27
- "gitHead": "49e25c03c2ba02f2613958c90e18e82173d5865a"
27
+ "gitHead": "45082bcb7811baf349b0febc1bf1a568b3d3e418"
28
28
  }