Skip to content

Commit 52f72fb

Browse files
author
standlove
committed
updated error handling in back-end
1 parent a5ecd31 commit 52f72fb

File tree

3 files changed

+49
-34
lines changed

3 files changed

+49
-34
lines changed

lib/common/errors.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,12 @@ class AppError extends Error {
1919
super();
2020
this.status = status;
2121
this.message = message || 'unknown exception';
22+
this.lineNo = -1;
2223
}
2324
}
2425

2526
module.exports = {
26-
newBadRequestError: msg => new AppError(
27-
400,
28-
msg || 'The request could not be interpreted correctly or some required parameters were missing.',
29-
),
30-
newEntityNotFoundError: msg => new AppError(404, msg || 'The entity does not exist.'),
31-
newAuthError: msg => new AppError(401, msg || 'Auth failed.'),
32-
newPermissionError: msg => new AppError(403, msg || 'The entity does not exist.'),
33-
newConflictError: msg => new AppError(409, msg || 'The entity does not exist.'),
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),
3430
};

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ module.exports = {
3838
try {
3939
result = await handler.method(req, h);
4040
} catch (e) {
41+
logger.error(e);
4142
result = {code: e.status, message: e.message}
4243
status = e.status || 500;
4344
}

lib/redis.js

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const redisDump = require('./tools/redis-dump');
77
* cached redis instance
88
*/
99
const redisInstanceCache = {};
10+
const CONNECTED = 'connected';
11+
const DISCONNECTED = 'disconnected';
1012

1113
/**
1214
* connect redis and save the connection instance
@@ -23,54 +25,74 @@ async function connect(body) {
2325
const redis = new Redis({
2426
host: body.serverModel.ip, port: body.serverModel.port, db: body.serverModel.db,
2527
password: body.serverModel.password,
28+
showFriendlyErrorStack: true,
2629
});
2730
const timeoutHandler = setTimeout(() => {
2831
redis.disconnect();
29-
reject(errors.newBadRequestError("connect timeout!"));
32+
reject(errors.newConnectTimeoutError);
3033
}, 3 * 1000);
3134

3235
redis.on('error', (e) => {
3336
console.error(e);
34-
redisInstanceCache[body.id] = null;
37+
body.status = DISCONNECTED;
38+
if (e instanceof Redis.MaxRetriesPerRequestError) {
39+
redisInstanceCache[body.id] = null;
40+
}
3541
});
3642

3743
redis.on('ready', () => {
3844
redisInstanceCache[body.id] = redis;
39-
body.status = 'connected';
45+
body.status = CONNECTED;
4046
resolve(body);
4147
clearTimeout(timeoutHandler);
4248
});
4349
});
4450
}
4551

4652
/**
47-
* fetch the redis tree
53+
* Get redis connection
54+
*
4855
* @param query the query params
4956
*/
50-
async function fetchTree(query) {
57+
function getRedis(query) {
5158
const redis = redisInstanceCache[query.id];
52-
if (!redis) {
53-
throw errors.newBadRequestError("Redis instance not exist");
59+
if (!redis && redis.status !== CONNECTED) {
60+
throw errors.newConnectFailedError;
5461
}
62+
return redis;
63+
}
64+
65+
/**
66+
* fetch the redis tree
67+
* @param query the query params
68+
*/
69+
async function fetchTree(query) {
70+
const redis = getRedis(query);
5571

5672
const root = {};
57-
const keys = query.keys || (await redis.keys('*'));
5873
const lencommands = {
5974
list: 'llen',
6075
set: 'scard',
6176
zset: 'zcard',
6277
hash: 'hlen',
6378
};
6479

65-
for (let i = 0; i < keys.length; i++) { // process types
66-
const key = keys[i];
67-
const type = await redis.type(key);
68-
root[key] = {type};
69-
if (type !== 'string') {
70-
root[key].len = await redis[lencommands[type]](key);
71-
} else {
72-
root[key].value = await redis.get(key);
80+
let keys;
81+
try {
82+
keys = query.keys || (await redis.keys('*'));
83+
84+
for (let i = 0; i < keys.length; i++) { // process types
85+
const key = keys[i];
86+
const type = await redis.type(key);
87+
root[key] = {type};
88+
if (type !== 'string') {
89+
root[key].len = await redis[lencommands[type]](key);
90+
} else {
91+
root[key].value = await redis.get(key);
92+
}
7393
}
94+
} catch (e) {
95+
throw errors.newReplyError(e.message);
7496
}
7597

7698
const tree = {};
@@ -145,16 +167,14 @@ async function call(query, body) {
145167
if (!lines || lines.length <= 0) {
146168
return [];
147169
}
148-
const redis = redisInstanceCache[query.id];
149-
if (!redis) {
150-
throw errors.newBadRequestError("Redis instance not exist");
151-
}
170+
const redis = getRedis(query);
171+
152172
const results = [];
153173
for (let i = 0; i < lines.length; i++) {
154174
try {
155175
results.push(await redis.call(...lines[i]));
156176
} catch (e) {
157-
results.push(`${e.message}`);
177+
throw errors.newReplyError(e.message, i + 1);
158178
}
159179
}
160180
return results;
@@ -166,14 +186,12 @@ async function call(query, body) {
166186
* @return {Promise<*>}
167187
*/
168188
async function dump(query) {
169-
const redis = redisInstanceCache[query.id];
170-
if (!redis) {
171-
throw errors.newBadRequestError("Redis instance not exist");
172-
}
189+
const redis = getRedis(query);
190+
173191
try {
174192
return await redisDump(query.exportType, redis);
175193
} catch (e) {
176-
throw errors.newBadRequestError("dump redis failed");
194+
throw errors.newReplyError(e.message);
177195
}
178196
}
179197

0 commit comments

Comments
 (0)