Improve Redis error messages

This commit is contained in:
ShadowNinja 2015-05-06 15:04:14 -04:00
parent 2b44e75442
commit 3f5c2dea4d

@ -47,7 +47,7 @@ Database_Redis::Database_Redis(Settings &conf)
ctx = redisConnect(addr, port); ctx = redisConnect(addr, port);
if (!ctx) { if (!ctx) {
throw FileNotGoodException("Cannot allocate redis context"); throw FileNotGoodException("Cannot allocate redis context");
} else if(ctx->err) { } else if (ctx->err) {
std::string err = std::string("Connection error: ") + ctx->errstr; std::string err = std::string("Connection error: ") + ctx->errstr;
redisFree(ctx); redisFree(ctx);
throw FileNotGoodException(err); throw FileNotGoodException(err);
@ -92,7 +92,7 @@ bool Database_Redis::saveBlock(const v3s16 &pos, const std::string &data)
if (reply->type == REDIS_REPLY_ERROR) { if (reply->type == REDIS_REPLY_ERROR) {
errorstream << "WARNING: saveBlock: saving block " << PP(pos) errorstream << "WARNING: saveBlock: saving block " << PP(pos)
<< "failed" << std::endl; << " failed: " << reply->str << std::endl;
freeReplyObject(reply); freeReplyObject(reply);
return false; return false;
} }
@ -110,13 +110,20 @@ std::string Database_Redis::loadBlock(const v3s16 &pos)
if (!reply) { if (!reply) {
throw FileNotGoodException(std::string( throw FileNotGoodException(std::string(
"Redis command 'HGET %s %s' failed: ") + ctx->errstr); "Redis command 'HGET %s %s' failed: ") + ctx->errstr);
} else if (reply->type != REDIS_REPLY_STRING) {
return "";
} }
switch (reply->type) {
case REDIS_REPLY_STRING: {
std::string str(reply->str, reply->len); std::string str(reply->str, reply->len);
freeReplyObject(reply); // std::string copies the memory so this won't cause any problems // std::string copies the memory so this won't cause any problems
freeReplyObject(reply);
return str; return str;
}
case REDIS_REPLY_ERROR:
errorstream << "WARNING: loadBlock: loading block " << PP(pos)
<< " failed: " << reply->str << std::endl;
}
freeReplyObject(reply);
return "";
} }
bool Database_Redis::deleteBlock(const v3s16 &pos) bool Database_Redis::deleteBlock(const v3s16 &pos)
@ -130,7 +137,7 @@ bool Database_Redis::deleteBlock(const v3s16 &pos)
"Redis command 'HDEL %s %s' failed: ") + ctx->errstr); "Redis command 'HDEL %s %s' failed: ") + ctx->errstr);
} else if (reply->type == REDIS_REPLY_ERROR) { } else if (reply->type == REDIS_REPLY_ERROR) {
errorstream << "WARNING: deleteBlock: deleting block " << PP(pos) errorstream << "WARNING: deleteBlock: deleting block " << PP(pos)
<< " failed" << std::endl; << " failed: " << reply->str << std::endl;
freeReplyObject(reply); freeReplyObject(reply);
return false; return false;
} }
@ -145,13 +152,17 @@ void Database_Redis::listAllLoadableBlocks(std::vector<v3s16> &dst)
if (!reply) { if (!reply) {
throw FileNotGoodException(std::string( throw FileNotGoodException(std::string(
"Redis command 'HKEYS %s' failed: ") + ctx->errstr); "Redis command 'HKEYS %s' failed: ") + ctx->errstr);
} else if (reply->type != REDIS_REPLY_ARRAY) {
throw FileNotGoodException("Failed to get keys from database");
} }
switch (reply->type) {
case REDIS_REPLY_ARRAY:
for (size_t i = 0; i < reply->elements; i++) { for (size_t i = 0; i < reply->elements; i++) {
assert(reply->element[i]->type == REDIS_REPLY_STRING); assert(reply->element[i]->type == REDIS_REPLY_STRING);
dst.push_back(getIntegerAsBlock(stoi64(reply->element[i]->str))); dst.push_back(getIntegerAsBlock(stoi64(reply->element[i]->str)));
} }
case REDIS_REPLY_ERROR:
throw FileNotGoodException(std::string(
"Failed to get keys from database: ") + reply->str);
}
freeReplyObject(reply); freeReplyObject(reply);
} }