Browse Source

* better error handling in source/redis & backend/redis

master
Alex 'AdUser' Z 8 years ago
parent
commit
d11adf90e7
  1. 16
      src/backends/redis.c
  2. 9
      src/sources/redis.c

16
src/backends/redis.c

@ -56,7 +56,8 @@ redis_connect(cfg_t *cfg) {
break; break;
} }
if (cfg->password[0]) { if (cfg->password[0]) {
reply = redisCommand(conn, "AUTH %s", cfg->password); if ((reply = redisCommand(conn, "AUTH %s", cfg->password)) == NULL)
break;
if (reply->type == REDIS_REPLY_ERROR) { if (reply->type == REDIS_REPLY_ERROR) {
snprintf(cfg->error, sizeof(cfg->error), "auth error: %s", reply->str); snprintf(cfg->error, sizeof(cfg->error), "auth error: %s", reply->str);
break; break;
@ -64,7 +65,8 @@ redis_connect(cfg_t *cfg) {
freeReplyObject(reply); freeReplyObject(reply);
} }
if (cfg->database) { if (cfg->database) {
reply = redisCommand(conn, "SELECT %d", cfg->database); if ((reply = redisCommand(conn, "SELECT %d", cfg->database)) == NULL)
break;
if (reply->type == REDIS_REPLY_ERROR) { if (reply->type == REDIS_REPLY_ERROR) {
snprintf(cfg->error, sizeof(cfg->error), "auth error: %s", reply->str); snprintf(cfg->error, sizeof(cfg->error), "auth error: %s", reply->str);
break; break;
@ -193,14 +195,16 @@ ban(cfg_t *cfg, const char *ip) {
redisReply *reply; redisReply *reply;
do { do {
reply = redisCommand(cfg->conn, "HINCRBY %s %s %d", cfg->hash, ip, 1); if ((reply = redisCommand(cfg->conn, "HINCRBY %s %s %d", cfg->hash, ip, 1)) == NULL)
if (reply && reply->type == REDIS_REPLY_ERROR) { break;
if (reply->type == REDIS_REPLY_ERROR) {
snprintf(cfg->error, sizeof(cfg->error), "HINCRBY: %s", reply->str); snprintf(cfg->error, sizeof(cfg->error), "HINCRBY: %s", reply->str);
break; break;
} }
freeReplyObject(reply); freeReplyObject(reply);
reply = redisCommand(cfg->conn, "PUBLISH %s %s", cfg->hash, ip); if ((reply = redisCommand(cfg->conn, "PUBLISH %s %s", cfg->hash, ip)) == NULL)
if (reply && reply->type == REDIS_REPLY_ERROR) { break;
if (reply->type == REDIS_REPLY_ERROR) {
snprintf(cfg->error, sizeof(cfg->error), "PUBLISH: %s", reply->str); snprintf(cfg->error, sizeof(cfg->error), "PUBLISH: %s", reply->str);
break; break;
} }

9
src/sources/redis.c

@ -51,7 +51,8 @@ redis_connect(cfg_t *cfg) {
break; break;
} }
if (cfg->password[0]) { if (cfg->password[0]) {
reply = redisCommand(conn, "AUTH %s", cfg->password); if ((reply = redisCommand(conn, "AUTH %s", cfg->password)) == NULL)
break;
if (reply->type == REDIS_REPLY_ERROR) { if (reply->type == REDIS_REPLY_ERROR) {
snprintf(cfg->error, sizeof(cfg->error), "auth error: %s", reply->str); snprintf(cfg->error, sizeof(cfg->error), "auth error: %s", reply->str);
break; break;
@ -59,7 +60,8 @@ redis_connect(cfg_t *cfg) {
freeReplyObject(reply); freeReplyObject(reply);
} }
if (cfg->database) { if (cfg->database) {
reply = redisCommand(conn, "SELECT %d", cfg->database); if ((reply = redisCommand(conn, "SELECT %d", cfg->database)) == NULL)
break;
if (reply->type == REDIS_REPLY_ERROR) { if (reply->type == REDIS_REPLY_ERROR) {
snprintf(cfg->error, sizeof(cfg->error), "auth error: %s", reply->str); snprintf(cfg->error, sizeof(cfg->error), "auth error: %s", reply->str);
break; break;
@ -72,7 +74,8 @@ redis_connect(cfg_t *cfg) {
strlcpy(cfg->error, "can't enable nonblocking mode", sizeof(cfg->error)); strlcpy(cfg->error, "can't enable nonblocking mode", sizeof(cfg->error));
break; break;
} }
reply = redisCommand(conn, "SUBSCRIBE %s", cfg->hash); if ((reply = redisCommand(conn, "SUBSCRIBE %s", cfg->hash)) == NULL)
break;
if (reply->type == REDIS_REPLY_ERROR) { if (reply->type == REDIS_REPLY_ERROR) {
snprintf(cfg->error, sizeof(cfg->error), "can't subscribe: %s", reply->str); snprintf(cfg->error, sizeof(cfg->error), "can't subscribe: %s", reply->str);
break; break;

Loading…
Cancel
Save