Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Handle Redis errors
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Sep 30, 2019
1 parent fc4e2bc commit 0fa8288
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "staart-manager",
"version": "1.1.29",
"version": "1.1.30",
"main": "index.js",
"repository": "[email protected]:AnandChowdhary/staart.git",
"author": "Anand Chowdhary <[email protected]>",
Expand Down Expand Up @@ -77,6 +77,7 @@
"@types/stripe": "^6.32.5",
"@types/validator": "^10.11.3",
"@types/yaml": "^1.0.2",
"chalk": "^2.4.2",
"concurrently": "^4.1.2",
"coveralls": "^3.0.6",
"dotenv": "^8.1.0",
Expand Down Expand Up @@ -144,5 +145,5 @@
"setup"
],
"snyk": true,
"staart-version": "1.1.29"
"staart-version": "1.1.30"
}
11 changes: 11 additions & 0 deletions src/helpers/errors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ErrorCode } from "../interfaces/enum";
import { HTTPError } from "../interfaces/general";
import chalk from "chalk";
import Joi from "@hapi/joi";

/**
Expand Down Expand Up @@ -38,3 +39,13 @@ export const sendError = (error: string) => {
console.log("Backup error", error);
return { status: 500, code: error } as HTTPError;
};

export const logError = (category: string, error: string, level: 1 | 2 = 2) => {
if (level === 1)
return console.log(
`${chalk.bold.red("❌ ERROR")} ${chalk.red(category)}: ${error}`
);
console.log(
`${chalk.bold.yellow("⚠️ WARNING")} ${chalk.yellow(category)}: ${error}`
);
};
20 changes: 19 additions & 1 deletion src/helpers/redis.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import { createHandyClient } from "handy-redis";
import { REDIS_URL } from "../config";
import { logError } from "./errors";

export const redis = createHandyClient({
url: REDIS_URL
url: REDIS_URL,
retry_strategy: options => {
if (options.error && options.error.code === "ECONNREFUSED") {
logError("Redis connection failed", "Server refused the connection");
}

if (options.total_retry_time > 1000 * 60 * 60) {
logError("Redis connection failed", "Total retry time exhausted");
}

if (options.attempt > 10) {
logError("Redis connection failed", "Max number of attempts exceeded");
return 43200;
}

// Reconnect after this time
return Math.min(options.attempt * 100, 3000);
}
});

0 comments on commit 0fa8288

Please sign in to comment.