Skip to content

Commit e264cf7

Browse files
Handle the exception from the token supplier (#458)
### Motivation When a token supplier is passed to the `AuthToken`, if exceptions are thrown from it, the application will crash immediately. A typical case is the Python wrapper might raise an exception when trying to get token. ### Modifications Catch the exception in `Commands::newConnect` because the token supplier is called in it. Then convert it to `ResultAuthenticationError`. Add `testTokenSupplierException` to verify it.
1 parent ce6c4bc commit e264cf7

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/ClientConnection.cc

+9-2
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,15 @@ void ClientConnection::handleHandshake(const ASIO_ERROR& err) {
528528

529529
bool connectingThroughProxy = logicalAddress_ != physicalAddress_;
530530
Result result = ResultOk;
531-
SharedBuffer buffer = Commands::newConnect(authentication_, logicalAddress_, connectingThroughProxy,
532-
clientVersion_, result);
531+
SharedBuffer buffer;
532+
try {
533+
buffer = Commands::newConnect(authentication_, logicalAddress_, connectingThroughProxy,
534+
clientVersion_, result);
535+
} catch (const std::exception& e) {
536+
LOG_ERROR(cnxString_ << "Failed to create Connect command: " << e.what());
537+
close(ResultAuthenticationError);
538+
return;
539+
}
533540
if (result != ResultOk) {
534541
LOG_ERROR(cnxString_ << "Failed to establish connection: " << result);
535542
close(result);

tests/AuthTokenTest.cc

+10
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,13 @@ TEST(AuthPluginToken, testNoAuthWithHttp) {
200200
result = client.subscribe(topicName, subName, consumer);
201201
ASSERT_EQ(ResultConnectError, result);
202202
}
203+
204+
TEST(AuthPluginToken, testTokenSupplierException) {
205+
ClientConfiguration config;
206+
config.setAuth(
207+
AuthToken::create([]() -> std::string { throw std::runtime_error("failed to generate token"); }));
208+
Client client(serviceUrl, config);
209+
Producer producer;
210+
ASSERT_EQ(ResultAuthenticationError, client.createProducer("topic", producer));
211+
ASSERT_EQ(ResultOk, client.close());
212+
}

0 commit comments

Comments
 (0)