Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exceptions: cleaning up macros #35694

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ ClientSslAuthConfig::ClientSslAuthConfig(
std::chrono::milliseconds(1000)),
tls_(tls.allocateSlot()), stats_(generateStats(scope, config.stat_prefix())) {
auto list_or_error = Network::Address::IpList::create(config.ip_white_list());
THROW_IF_STATUS_NOT_OK(list_or_error, throw);
THROW_IF_NOT_OK_REF(list_or_error.status());
ip_allowlist_ = std::move(list_or_error.value());

if (!cm.clusters().hasCluster(remote_cluster_name_)) {
Expand Down
2 changes: 1 addition & 1 deletion contrib/sip_proxy/filters/network/source/tra/tra_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ ClientPtr traClient(Event::Dispatcher& dispatcher, Server::Configuration::Factor
.clusterManager()
.grpcAsyncClientManager()
.getOrCreateRawAsyncClient(grpc_service, context.scope(), true);
THROW_IF_STATUS_NOT_OK(client_or_error, throw);
THROW_IF_NOT_OK_REF(client_or_error.status());
return std::make_unique<SipProxy::TrafficRoutingAssistant::GrpcClientImpl>(
client_or_error.value(), dispatcher, timeout);
}
Expand Down
13 changes: 3 additions & 10 deletions envoy/common/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,14 @@ class EnvoyException : public std::runtime_error {
} \
} while (0)

// Simple macro to handle bridging functions which return absl::StatusOr, and
// functions which throw errors.
#define THROW_IF_NOT_OK(status_fn) \
do { \
const absl::Status status = (status_fn); \
THROW_IF_NOT_OK_REF(status); \
} while (0)

// Simple macro to handle bridging functions which return absl::StatusOr, and
// functions which throw errors.
//
// The completely unnecessary throw_action argument was just so 'throw' appears
// at the call site, so format checks about use of exceptions would be triggered.
// This didn't work, so the variable is no longer used and is not duplicated in
// the macros above.
#define THROW_IF_STATUS_NOT_OK(variable, throw_action) THROW_IF_NOT_OK_REF(variable.status());

#define RETURN_IF_NOT_OK_REF(variable) \
if (const absl::Status& temp_status = variable; !temp_status.ok()) { \
return temp_status; \
Expand All @@ -69,7 +62,7 @@ class EnvoyException : public std::runtime_error {
}

template <class Type> Type returnOrThrow(absl::StatusOr<Type> type_or_error) {
THROW_IF_STATUS_NOT_OK(type_or_error, throw);
THROW_IF_NOT_OK_REF(type_or_error.status());
return std::move(type_or_error.value());
}

Expand Down
2 changes: 1 addition & 1 deletion source/common/common/logger_delegates.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ FileSinkDelegate::FileSinkDelegate(const std::string& log_path,
: SinkDelegate(log_sink) {
auto file_or_error = log_manager.createAccessLog(
Filesystem::FilePathAndType{Filesystem::DestinationType::File, log_path});
THROW_IF_STATUS_NOT_OK(file_or_error, throw);
THROW_IF_NOT_OK_REF(file_or_error.status());
log_file_ = file_or_error.value();
setDelegate();
}
Expand Down
8 changes: 4 additions & 4 deletions source/common/formatter/http_specific_formatter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -300,23 +300,23 @@ BuiltInHttpCommandParser::getKnownFormatters() {
{CommandSyntaxChecker::PARAMS_REQUIRED | CommandSyntaxChecker::LENGTH_ALLOWED,
[](absl::string_view format, absl::optional<size_t> max_length) {
auto result = SubstitutionFormatUtils::parseSubcommandHeaders(format);
THROW_IF_STATUS_NOT_OK(result, throw);
THROW_IF_NOT_OK_REF(result.status());
return std::make_unique<RequestHeaderFormatter>(result.value().first,
result.value().second, max_length);
}}},
{"RESP",
{CommandSyntaxChecker::PARAMS_REQUIRED | CommandSyntaxChecker::LENGTH_ALLOWED,
[](absl::string_view format, absl::optional<size_t> max_length) {
auto result = SubstitutionFormatUtils::parseSubcommandHeaders(format);
THROW_IF_STATUS_NOT_OK(result, throw);
THROW_IF_NOT_OK_REF(result.status());
return std::make_unique<ResponseHeaderFormatter>(result.value().first,
result.value().second, max_length);
}}},
{"TRAILER",
{CommandSyntaxChecker::PARAMS_REQUIRED | CommandSyntaxChecker::LENGTH_ALLOWED,
[](absl::string_view format, absl::optional<size_t> max_length) {
auto result = SubstitutionFormatUtils::parseSubcommandHeaders(format);
THROW_IF_STATUS_NOT_OK(result, throw);
THROW_IF_NOT_OK_REF(result.status());
return std::make_unique<ResponseTrailerFormatter>(result.value().first,
result.value().second, max_length);
}}},
Expand Down Expand Up @@ -364,7 +364,7 @@ BuiltInHttpCommandParser::getKnownFormatters() {
{CommandSyntaxChecker::PARAMS_REQUIRED | CommandSyntaxChecker::LENGTH_ALLOWED,
[](absl::string_view format, absl::optional<size_t> max_length) {
auto result = SubstitutionFormatUtils::parseSubcommandHeaders(format);
THROW_IF_STATUS_NOT_OK(result, throw);
THROW_IF_NOT_OK_REF(result.status());
return std::make_unique<RequestHeaderFormatter>(result.value().first,
result.value().second, max_length);
}}},
Expand Down
6 changes: 3 additions & 3 deletions source/common/listener_manager/listener_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ std::string listenerStatsScope(const envoy::config::listener::v3::Listener& conf
return absl::StrCat("envoy_internal_", config.name());
}
auto address_or_error = Network::Address::resolveProtoAddress(config.address());
THROW_IF_STATUS_NOT_OK(address_or_error, throw);
THROW_IF_NOT_OK_REF(address_or_error.status());
return address_or_error.value()->asString();
}
} // namespace
Expand Down Expand Up @@ -324,7 +324,7 @@ ListenerImpl::ListenerImpl(const envoy::config::listener::v3::Listener& config,
// All the addresses should be same socket type, so get the first address's socket type is
// enough.
auto address_or_error = Network::Address::resolveProtoAddress(config.address());
THROW_IF_STATUS_NOT_OK(address_or_error, throw);
THROW_IF_NOT_OK_REF(address_or_error.status());
auto address = std::move(address_or_error.value());
checkIpv4CompatAddress(address, config.address());
addresses_.emplace_back(address);
Expand All @@ -340,7 +340,7 @@ ListenerImpl::ListenerImpl(const envoy::config::listener::v3::Listener& config,
}
auto addresses_or_error =
Network::Address::resolveProtoAddress(config.additional_addresses(i).address());
THROW_IF_STATUS_NOT_OK(addresses_or_error, throw);
THROW_IF_NOT_OK_REF(addresses_or_error.status());
auto additional_address = std::move(addresses_or_error.value());
checkIpv4CompatAddress(address, config.additional_addresses(i).address());
addresses_.emplace_back(additional_address);
Expand Down
2 changes: 1 addition & 1 deletion source/common/protobuf/utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ void MessageUtil::loadFromFile(const std::string& path, Protobuf::Message& messa
ProtobufMessage::ValidationVisitor& validation_visitor,
Api::Api& api) {
auto file_or_error = api.fileSystem().fileReadToEnd(path);
THROW_IF_STATUS_NOT_OK(file_or_error, throw);
THROW_IF_NOT_OK_REF(file_or_error.status());
const std::string contents = file_or_error.value();
// If the filename ends with .pb, attempt to parse it as a binary proto.
if (absl::EndsWithIgnoreCase(path, FileExtensions::get().ProtoBinary)) {
Expand Down
2 changes: 1 addition & 1 deletion source/common/secret/sds_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ SdsApi::FileContentMap SdsApi::loadFiles() {
FileContentMap files;
for (auto const& filename : getDataSourceFilenames()) {
auto file_or_error = api_.fileSystem().fileReadToEnd(filename);
THROW_IF_STATUS_NOT_OK(file_or_error, throw);
THROW_IF_NOT_OK_REF(file_or_error.status());
files[filename] = file_or_error.value();
}
return files;
Expand Down
2 changes: 1 addition & 1 deletion source/common/upstream/cluster_manager_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ bool ClusterManagerImpl::addOrUpdateCluster(const envoy::config::cluster::v3::Cl
// before destroy to avoid early initialization complete.
auto status_or_cluster = loadCluster(cluster, new_hash, version_info, /*added_via_api=*/true,
/*required_for_ads=*/false, warming_clusters_);
THROW_IF_STATUS_NOT_OK(status_or_cluster, throw);
THROW_IF_NOT_OK_REF(status_or_cluster.status());
const ClusterDataPtr previous_cluster = std::move(status_or_cluster.value());
auto& cluster_entry = warming_clusters_.at(cluster_name);
cluster_entry->cluster_->info()->configUpdateStats().warming_state_.set(1);
Expand Down
2 changes: 1 addition & 1 deletion source/common/upstream/health_checker_event_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class HealthCheckEventLoggerImpl : public HealthCheckEventLogger {
auto file_or_error = context.serverFactoryContext().accessLogManager().createAccessLog(
Filesystem::FilePathAndType{Filesystem::DestinationType::File,
health_check_config.event_log_path()});
THROW_IF_STATUS_NOT_OK(file_or_error, throw);
THROW_IF_NOT_OK_REF(file_or_error.status());
file_ = file_or_error.value();
}
for (const auto& config : health_check_config.event_logger()) {
Expand Down
6 changes: 3 additions & 3 deletions source/common/upstream/health_discovery_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ HdsCluster::HdsCluster(Server::Configuration::ServerFactoryContext& server_conte
const LocalityEndpointTuple endpoint_key = {locality_endpoints.locality(), host};
// Initialize an endpoint host object.
auto address_or_error = Network::Address::resolveProtoAddress(host.endpoint().address());
THROW_IF_STATUS_NOT_OK(address_or_error, throw);
THROW_IF_NOT_OK_REF(address_or_error.status());
HostSharedPtr endpoint = std::make_shared<HostImpl>(
info_, "", std::move(address_or_error.value()), nullptr, nullptr, 1,
locality_endpoints.locality(), host.endpoint().health_check_config(), 0,
Expand Down Expand Up @@ -487,7 +487,7 @@ void HdsCluster::updateHosts(
// We do not have this endpoint saved, so create a new one.
auto address_or_error =
Network::Address::resolveProtoAddress(endpoint.endpoint().address());
THROW_IF_STATUS_NOT_OK(address_or_error, throw);
THROW_IF_NOT_OK_REF(address_or_error.status());
host = std::make_shared<HostImpl>(info_, "", std::move(address_or_error.value()), nullptr,
nullptr, 1, endpoints.locality(),
endpoint.endpoint().health_check_config(), 0,
Expand Down Expand Up @@ -560,7 +560,7 @@ void HdsCluster::initHealthchecks() {
for (auto& health_check : cluster_.health_checks()) {
auto health_checker_or_error =
Upstream::HealthCheckerFactory::create(health_check, *this, server_context_);
THROW_IF_STATUS_NOT_OK(health_checker_or_error, throw);
THROW_IF_NOT_OK_REF(health_checker_or_error.status());

auto health_checker = health_checker_or_error.value();
health_checkers_.push_back(health_checker);
Expand Down
2 changes: 1 addition & 1 deletion source/common/upstream/outlier_detection_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ class EventLoggerImpl : public EventLogger {
: time_source_(time_source) {
auto file_or_error = log_manager.createAccessLog(
Filesystem::FilePathAndType{Filesystem::DestinationType::File, file_name});
THROW_IF_STATUS_NOT_OK(file_or_error, throw);
THROW_IF_NOT_OK_REF(file_or_error.status());
file_ = file_or_error.value();
}

Expand Down
2 changes: 1 addition & 1 deletion source/common/upstream/upstream_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2546,7 +2546,7 @@ Network::Address::InstanceConstSharedPtr resolveHealthCheckAddress(
const auto& port_value = health_check_config.port_value();
if (health_check_config.has_address()) {
auto address_or_error = Network::Address::resolveProtoAddress(health_check_config.address());
THROW_IF_STATUS_NOT_OK(address_or_error, throw);
THROW_IF_NOT_OK_REF(address_or_error.status());
auto address = address_or_error.value();
health_check_address =
port_value == 0 ? address : Network::Utility::getAddressWithPort(*address, port_value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ FileAccessLog::FileAccessLog(const Filesystem::FilePathAndType& access_log_file_
AccessLog::AccessLogManager& log_manager)
: ImplBase(std::move(filter)), formatter_(std::move(formatter)) {
auto file_or_error = log_manager.createAccessLog(access_log_file_info);
THROW_IF_STATUS_NOT_OK(file_or_error, throw);
THROW_IF_NOT_OK_REF(file_or_error.status());
log_file_ = file_or_error.value();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ GrpcAccessLoggerImpl::SharedPtr GrpcAccessLoggerCacheImpl::createLogger(
// the main thread if necessary.
auto factory_or_error =
async_client_manager_.factoryForGrpcService(config.grpc_service(), scope_, true);
THROW_IF_STATUS_NOT_OK(factory_or_error, throw);
THROW_IF_NOT_OK_REF(factory_or_error.status());
return std::make_shared<GrpcAccessLoggerImpl>(
factory_or_error.value()->createUncachedRawAsyncClient(), config, dispatcher, local_info_,
scope_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ GrpcAccessLoggerImpl::SharedPtr GrpcAccessLoggerCacheImpl::createLogger(
// the main thread if necessary to ensure it does not throw here.
auto factory_or_error = async_client_manager_.factoryForGrpcService(
config.common_config().grpc_service(), scope_, true);
THROW_IF_STATUS_NOT_OK(factory_or_error, throw);
THROW_IF_NOT_OK_REF(factory_or_error.status());
auto client = factory_or_error.value()->createUncachedRawAsyncClient();
return std::make_shared<GrpcAccessLoggerImpl>(std::move(client), config, dispatcher, local_info_,
scope_);
Expand Down
2 changes: 1 addition & 1 deletion source/extensions/common/wasm/remote_async_datasource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ RemoteAsyncDataProvider::RemoteAsyncDataProvider(

auto strategy_or_error = Config::Utility::prepareJitteredExponentialBackOffStrategy(
source, random, RetryInitialDelayMilliseconds, RetryMaxDelayMilliseconds);
THROW_IF_STATUS_NOT_OK(strategy_or_error, throw);
THROW_IF_NOT_OK_REF(strategy_or_error.status());
backoff_strategy_ = std::move(strategy_or_error.value());

retry_timer_ = dispatcher.createTimer([this]() -> void { start(); });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ template <class T, size_t (*deleter)(T*), unsigned (*getDictId)(const T*)> class

void onDictionaryUpdate(unsigned origin_id, const std::string& filename) {
auto file_or_error = api_.fileSystem().fileReadToEnd(filename);
THROW_IF_STATUS_NOT_OK(file_or_error, throw);
THROW_IF_NOT_OK_REF(file_or_error.status());
const auto data = file_or_error.value();
if (!data.empty()) {
auto dictionary = DictionarySharedPtr(builder_(data.data(), data.length()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ SubscriptionPtr DeltaGrpcCollectionConfigSubscriptionFactory::create(
auto strategy_or_error = Utility::prepareJitteredExponentialBackOffStrategy(
api_config_source, data.api_.randomGenerator(), SubscriptionFactory::RetryInitialDelayMs,
SubscriptionFactory::RetryMaxDelayMs);
THROW_IF_STATUS_NOT_OK(strategy_or_error, throw);
THROW_IF_NOT_OK_REF(strategy_or_error.status());
JitteredExponentialBackOffStrategyPtr backoff_strategy = std::move(strategy_or_error.value());

auto factory_primary_or_error = Config::Utility::factoryForGrpcApiConfigSource(
data.cm_.grpcAsyncClientManager(), api_config_source, data.scope_, true, 0);
THROW_IF_STATUS_NOT_OK(factory_primary_or_error, throw);
THROW_IF_NOT_OK_REF(factory_primary_or_error.status());
absl::StatusOr<RateLimitSettings> rate_limit_settings_or_error =
Utility::parseRateLimitSettings(api_config_source);
THROW_IF_STATUS_NOT_OK(rate_limit_settings_or_error, throw);
THROW_IF_NOT_OK_REF(rate_limit_settings_or_error.status());
GrpcMuxContext grpc_mux_context{
factory_primary_or_error.value()->createUncachedRawAsyncClient(),
/*failover_async_client_=*/nullptr,
Expand Down
6 changes: 3 additions & 3 deletions source/extensions/config_subscription/grpc/grpc_mux_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ bool isXdsTpWildcard(const std::string& resource_name) {
std::string convertToWildcard(const std::string& resource_name) {
ASSERT(XdsResourceIdentifier::hasXdsTpScheme(resource_name));
auto resource_or_error = XdsResourceIdentifier::decodeUrn(resource_name);
THROW_IF_STATUS_NOT_OK(resource_or_error, throw);
THROW_IF_NOT_OK_REF(resource_or_error.status());
xds::core::v3::ResourceName xdstp_resource = resource_or_error.value();
const auto pos = xdstp_resource.id().find_last_of('/');
xdstp_resource.set_id(
Expand Down Expand Up @@ -445,7 +445,7 @@ void GrpcMuxImpl::processDiscoveryResources(const std::vector<DecodedResourcePtr
if (XdsResourceIdentifier::hasXdsTpScheme(resource->name())) {
// Sort the context params of an xdstp resource, so we can compare them easily.
auto resource_or_error = XdsResourceIdentifier::decodeUrn(resource->name());
THROW_IF_STATUS_NOT_OK(resource_or_error, throw);
THROW_IF_NOT_OK_REF(resource_or_error.status());
xds::core::v3::ResourceName xdstp_resource = resource_or_error.value();
XdsResourceIdentifier::EncodeOptions options;
options.sort_context_params_ = true;
Expand Down Expand Up @@ -631,7 +631,7 @@ class GrpcMuxFactory : public MuxFactory {
XdsResourcesDelegateOptRef xds_resources_delegate, bool use_eds_resources_cache) override {
absl::StatusOr<RateLimitSettings> rate_limit_settings_or_error =
Utility::parseRateLimitSettings(ads_config);
THROW_IF_STATUS_NOT_OK(rate_limit_settings_or_error, throw);
THROW_IF_NOT_OK_REF(rate_limit_settings_or_error.status());
GrpcMuxContext grpc_mux_context{
/*async_client_=*/std::move(async_client),
/*failover_async_client_=*/std::move(failover_async_client),
Expand Down
2 changes: 1 addition & 1 deletion source/extensions/config_subscription/grpc/grpc_mux_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class GrpcMuxImpl : public GrpcMux,
[this](const std::string& resource_name) -> std::string {
if (XdsResourceIdentifier::hasXdsTpScheme(resource_name)) {
auto xdstp_resource_or_error = XdsResourceIdentifier::decodeUrn(resource_name);
THROW_IF_STATUS_NOT_OK(xdstp_resource_or_error, throw);
THROW_IF_NOT_OK_REF(xdstp_resource_or_error.status());
auto xdstp_resource = xdstp_resource_or_error.value();
if (subscription_options_.add_xdstp_node_context_params_) {
const auto context = XdsContextParams::encodeResource(
Expand Down
Loading