Skip to content

Commit d2500e7

Browse files
author
stand4love@163.com
committed
added redis info into error response.
1 parent adddb33 commit d2500e7

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

lib/common/errors.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@
1515
* the base error class
1616
*/
1717
class AppError extends Error {
18-
constructor(status, message) {
18+
constructor(redis, status, message) {
1919
super();
20+
this.redis = redis || {};
21+
delete this.redis.password;
22+
2023
this.status = status;
2124
this.message = message || 'unknown exception';
2225
this.lineNo = -1;
2326
}
2427
}
2528

2629
module.exports = {
27-
newConnectTimeoutError: msg => new AppError(500, msg || 'ConnectTimeout'),
28-
newConnectFailedError: msg => new AppError(500, msg || 'ConnectFailed'),
29-
newReplyError: (msg, lineNo = -1) => new AppError(400, msg, lineNo),
30+
newConnectTimeoutError: (redis, msg) => new AppError(redis, 500, msg || 'ConnectTimeout'),
31+
newConnectFailedError: (redis, msg) => new AppError(redis, 500, msg || 'ConnectFailed'),
32+
newReplyError: (redis, msg, lineNo = -1) => new AppError(redis, 400, msg, lineNo),
3033
};

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module.exports = {
3939
result = await handler.method(req, h);
4040
} catch (e) {
4141
logger.error(e);
42-
result = {code: e.status, message: e.message, line: e.lineNo || -1}
42+
result = {code: e.status, redis: e.redis, message: e.message, line: e.lineNo || -1}
4343
status = e.status || 500;
4444
}
4545
return injectHeader(h.response(result).code(status));

lib/redis.js

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ const DISCONNECTED = 'disconnected';
1818
async function connect(body) {
1919
// already exist
2020
if (redisInstanceCache[body.id]) {
21+
const redisConn = redisInstanceCache[body.id];
22+
if (redisConn.status == DISCONNECTED) {
23+
redisConn.redis.connect();
24+
}
2125
return Promise.resolve(body);
2226
}
2327

@@ -26,22 +30,26 @@ async function connect(body) {
2630
host: body.serverModel.ip, port: body.serverModel.port, db: body.serverModel.db,
2731
password: body.serverModel.password,
2832
showFriendlyErrorStack: true,
33+
maxRetriesPerRequest: 10,
2934
});
35+
36+
body.serverModel.id = body.id;
37+
3038
const timeoutHandler = setTimeout(() => {
3139
redis.disconnect();
32-
reject(errors.newConnectTimeoutError);
40+
reject(errors.newConnectTimeoutError(body.serverModel));
3341
}, 3 * 1000);
3442

3543
redis.on('error', (e) => {
3644
console.error(e);
3745
body.status = DISCONNECTED;
38-
redisInstanceCache[body.id] = null;
39-
4046
});
4147

4248
redis.on('ready', () => {
43-
redisInstanceCache[body.id] = redis;
49+
body.redis = redis;
4450
body.status = CONNECTED;
51+
redisInstanceCache[body.id] = body;
52+
4553
resolve(body);
4654
clearTimeout(timeoutHandler);
4755
});
@@ -53,20 +61,21 @@ async function connect(body) {
5361
*
5462
* @param query the query params
5563
*/
56-
function getRedis(query) {
57-
const redis = redisInstanceCache[query.id];
58-
if (!redis && redis.status !== CONNECTED) {
59-
throw errors.newConnectFailedError;
64+
function getRedisConnection(query) {
65+
const redisConn = redisInstanceCache[query.id];
66+
if (!redisConn && redisConn.status !== CONNECTED) {
67+
throw errors.newConnectFailedError(redisConn ? redisConn.serverModel : {});
6068
}
61-
return redis;
69+
return redisConn;
6270
}
6371

6472
/**
6573
* fetch the redis tree
6674
* @param query the query params
6775
*/
6876
async function fetchTree(query) {
69-
const redis = getRedis(query);
77+
const redisConn = getRedisConnection(query);
78+
const redis = redisConn.redis;
7079

7180
const root = {};
7281
const lencommands = {
@@ -91,7 +100,7 @@ async function fetchTree(query) {
91100
}
92101
}
93102
} catch (e) {
94-
throw errors.newReplyError(e.message);
103+
throw errors.newReplyError(redisConn.serverModel, e.message);
95104
}
96105

97106
const tree = {};
@@ -166,14 +175,15 @@ async function call(query, body) {
166175
if (!lines || lines.length <= 0) {
167176
return [];
168177
}
169-
const redis = getRedis(query);
178+
const redisConn = getRedisConnection(query);
179+
const redis = redisConn.redis;
170180

171181
const results = [];
172182
for (let i = 0; i < lines.length; i++) {
173183
try {
174184
results.push(await redis.call(...lines[i]));
175185
} catch (e) {
176-
throw errors.newReplyError(e.message, i + 1);
186+
throw errors.newReplyError(redisConn.serverModel, e.message, i + 1);
177187
}
178188
}
179189
return results;
@@ -185,12 +195,13 @@ async function call(query, body) {
185195
* @return {Promise<*>}
186196
*/
187197
async function dump(query) {
188-
const redis = getRedis(query);
198+
const redisConn = getRedisConnection(query);
199+
const redis = redisConn.redis;
189200

190201
try {
191202
return await redisDump(query.exportType, redis);
192203
} catch (e) {
193-
throw errors.newReplyError(e.message);
204+
throw errors.newReplyError(redisConn.serverModel, e.message);
194205
}
195206
}
196207

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy