Skip to content

Commit

Permalink
Add more information when connect redis fail
Browse files Browse the repository at this point in the history
  • Loading branch information
Junchao-Mellanox committed Jun 11, 2024
1 parent 391e27b commit eadf646
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions common/dbconnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ void RedisContext::initContext(const char *host, int port, const timeval *tv)

if (m_conn->err)
throw system_error(make_error_code(errc::address_not_available),
"Unable to connect to redis");
"Unable to connect to redis - " + std::string(m_conn->errstr) + "(" + std::to_string(m_conn->err) + ")");
}

void RedisContext::initContext(const char *path, const timeval *tv)
Expand All @@ -578,7 +578,7 @@ void RedisContext::initContext(const char *path, const timeval *tv)

if (m_conn->err)
throw system_error(make_error_code(errc::address_not_available),
"Unable to connect to redis (unix-socket)");
"Unable to connect to redis (unix-socket) - " + std::string(m_conn->errstr) + "(" + std::to_string(m_conn->err) + ")");
}

redisContext *RedisContext::getContext() const
Expand Down
29 changes: 29 additions & 0 deletions tests/redis_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1139,3 +1139,32 @@ TEST(Connector, hmset)
// test empty multi hash
db.hmset({});
}

TEST(Connector, connectFail)
{
// connect to an ip which is not a redis server
EXPECT_THROW({
try
{
DBConnector db(0, "1.1.1.1", 6379, 1);
}
catch(const std::system_error& e)
{
EXPECT_THAT(e.what(), HasSubstr("Unable to connect to redis - "));
throw;
}
}, std::system_error);

// connect to an invalid unix socket address
EXPECT_THROW({
try
{
DBConnector db(0, "/tmp/invalid", 1);
}
catch(const std::system_error& e)
{
EXPECT_THAT(e.what() HasSubstr("Unable to connect to redis (unix-socket) - "));
throw;
}
}, std::system_error);
}

0 comments on commit eadf646

Please sign in to comment.