Skip to content

Commit

Permalink
cares: handling no-exception build (envoyproxy#34113)
Browse files Browse the repository at this point in the history
Signed-off-by: Alyssa Wilk <[email protected]>
  • Loading branch information
alyssawilk authored May 13, 2024
1 parent 27c05b6 commit 22853cd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
24 changes: 11 additions & 13 deletions source/extensions/network/dns_resolver/cares/dns_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ absl::optional<std::string> DnsResolverImpl::maybeBuildResolversCsv(
for (const auto& resolver : resolvers) {
// This should be an IP address (i.e. not a pipe).
if (resolver->ip() == nullptr) {
throw EnvoyException(
throwEnvoyExceptionOrPanic(
fmt::format("DNS resolver '{}' is not an IP address", resolver->asString()));
}
// Note that the ip()->port() may be zero if the port is not fully specified by the
Expand Down Expand Up @@ -309,18 +309,16 @@ void DnsResolverImpl::PendingResolution::finishResolve() {
callback_(pending_response_.status_, std::move(pending_response_.address_list_));
}
END_TRY
catch (const EnvoyException& e) {
ENVOY_LOG(critical, "EnvoyException in c-ares callback: {}", e.what());
dispatcher_.post([s = std::string(e.what())] { throw EnvoyException(s); });
}
catch (const std::exception& e) {
ENVOY_LOG(critical, "std::exception in c-ares callback: {}", e.what());
dispatcher_.post([s = std::string(e.what())] { throw EnvoyException(s); });
}
catch (...) {
ENVOY_LOG(critical, "Unknown exception in c-ares callback");
dispatcher_.post([] { throw EnvoyException("unknown"); });
}
MULTI_CATCH(
const EnvoyException& e,
{
ENVOY_LOG(critical, "EnvoyException in c-ares callback: {}", e.what());
dispatcher_.post([s = std::string(e.what())] { throwEnvoyExceptionOrPanic(s); });
},
{
ENVOY_LOG(critical, "Unknown exception in c-ares callback");
dispatcher_.post([] { throwEnvoyExceptionOrPanic("unknown"); });
});
} else {
ENVOY_LOG_EVENT(debug, "cares_dns_callback_cancelled",
"dns resolution callback for {} not issued. Cancelled with reason={}",
Expand Down
9 changes: 1 addition & 8 deletions test/extensions/network/dns_resolver/cares/dns_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1095,18 +1095,11 @@ TEST_P(DnsImplTest, CallbackException) {
checkStats(1 /*resolve_total*/, 0 /*pending_resolutions*/, 0 /*not_found*/,
0 /*get_addr_failure*/, 0 /*timeouts*/);

EXPECT_EQ(nullptr, resolveWithException<std::runtime_error>("1.2.3.4", DnsLookupFamily::V4Only,
std::runtime_error("runtime error")));
EXPECT_THROW_WITH_MESSAGE(dispatcher_->run(Event::Dispatcher::RunType::Block), EnvoyException,
"runtime error");
checkStats(2 /*resolve_total*/, 0 /*pending_resolutions*/, 0 /*not_found*/,
0 /*get_addr_failure*/, 0 /*timeouts*/);

EXPECT_EQ(nullptr,
resolveWithException<std::string>("1.2.3.4", DnsLookupFamily::V4Only, std::string()));
EXPECT_THROW_WITH_MESSAGE(dispatcher_->run(Event::Dispatcher::RunType::Block), EnvoyException,
"unknown");
checkStats(3 /*resolve_total*/, 0 /*pending_resolutions*/, 0 /*not_found*/,
checkStats(2 /*resolve_total*/, 0 /*pending_resolutions*/, 0 /*not_found*/,
0 /*get_addr_failure*/, 0 /*timeouts*/);
}

Expand Down

0 comments on commit 22853cd

Please sign in to comment.