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

cleanup(storage): use Status factory functions #14143

Merged
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 @@ -323,8 +323,8 @@ google::cloud::StatusOr<AggregateDownloadThroughputOptions> SelfTest(
if (!value.empty()) continue;
std::ostringstream os;
os << "The environment variable " << var << " is not set or empty";
return google::cloud::Status(google::cloud::StatusCode::kUnknown,
std::move(os).str());
return google::cloud::internal::UnknownError(std::move(os).str(),
GCP_ERROR_INFO());
}
auto bucket_name =
GetEnv("GOOGLE_CLOUD_CPP_STORAGE_TEST_BUCKET_NAME").value();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,57 @@
#include "google/cloud/storage/benchmarks/aggregate_download_throughput_options.h"
#include "google/cloud/grpc_options.h"
#include "google/cloud/internal/absl_str_join_quiet.h"
#include "google/cloud/internal/make_status.h"
#include <iterator>
#include <sstream>
#include <utility>

namespace google {
namespace cloud {
namespace storage_benchmarks {

namespace gcs = ::google::cloud::storage;
namespace gcs_ex = ::google::cloud::storage_experimental;
using ::google::cloud::internal::ErrorInfoBuilder;
using ::google::cloud::testing_util::OptionDescriptor;

StatusOr<AggregateDownloadThroughputOptions> ValidateOptions(
std::string const& usage, AggregateDownloadThroughputOptions options) {
auto make_status = [](std::ostringstream& os) {
return Status{StatusCode::kInvalidArgument, std::move(os).str()};
auto make_status = [](std::ostringstream& os, ErrorInfoBuilder eib) {
return google::cloud::internal::InvalidArgumentError(std::move(os).str(),
std::move(eib));
};

if (options.bucket_name.empty()) {
std::ostringstream os;
os << "Missing --bucket-name option\n" << usage << "\n";
return make_status(os);
return make_status(os, GCP_ERROR_INFO());
}
if (options.thread_count <= 0) {
std::ostringstream os;
os << "Invalid number of threads (" << options.thread_count
<< "), check your --thread-count option\n";
return make_status(os);
return make_status(os, GCP_ERROR_INFO());
}
if (options.iteration_count <= 0) {
std::ostringstream os;
os << "Invalid number of iterations (" << options.iteration_count
<< "), check your --iteration-count option\n";
return make_status(os);
return make_status(os, GCP_ERROR_INFO());
}
if (options.repeats_per_iteration <= 0) {
std::ostringstream os;
os << "Invalid number of repeats per iteration ("
<< options.repeats_per_iteration
<< "), check your --repeats-per-iteration option\n";
return make_status(os);
return make_status(os, GCP_ERROR_INFO());
}
if (options.client_options.get<GrpcNumChannelsOption>() < 0) {
std::ostringstream os;
os << "Invalid number of gRPC channels ("
<< options.client_options.get<GrpcNumChannelsOption>()
<< "), check your --grpc-channel-count option\n";
return make_status(os);
return make_status(os, GCP_ERROR_INFO());
}

return options;
Expand Down Expand Up @@ -178,7 +182,8 @@ ParseAggregateDownloadThroughputOptions(std::vector<std::string> const& argv,
<< absl::StrJoin(std::next(unparsed.begin()), unparsed.end(), ", ")
<< "\n"
<< usage << "\n";
return Status{StatusCode::kInvalidArgument, std::move(os).str()};
return google::cloud::internal::InvalidArgumentError(std::move(os).str(),
GCP_ERROR_INFO());
}
return ValidateOptions(usage, std::move(options));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "google/cloud/grpc_options.h"
#include "google/cloud/internal/build_info.h"
#include "google/cloud/internal/getenv.h"
#include "google/cloud/internal/make_status.h"
#include "google/cloud/internal/random.h"
#include "google/cloud/log.h"
#include "google/cloud/options.h"
Expand Down Expand Up @@ -370,8 +371,8 @@ google::cloud::StatusOr<AggregateUploadThroughputOptions> SelfTest(
if (!value.empty()) continue;
std::ostringstream os;
os << "The environment variable " << var << " is not set or empty";
return google::cloud::Status(google::cloud::StatusCode::kUnknown,
std::move(os).str());
return google::cloud::internal::UnknownError(std::move(os).str(),
GCP_ERROR_INFO());
}
auto bucket_name =
GetEnv("GOOGLE_CLOUD_CPP_STORAGE_TEST_BUCKET_NAME").value();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "google/cloud/internal/absl_str_join_quiet.h"
#include <iterator>
#include <sstream>
#include <utility>

namespace google {
namespace cloud {
Expand All @@ -25,51 +26,52 @@ namespace {

namespace gcs = ::google::cloud::storage;
namespace gcs_ex = ::google::cloud::storage_experimental;
using ::google::cloud::internal::ErrorInfoBuilder;
using ::google::cloud::testing_util::OptionDescriptor;

StatusOr<AggregateUploadThroughputOptions> ValidateOptions(
std::string const& usage, AggregateUploadThroughputOptions options) {
auto make_status = [](std::ostringstream& os) {
auto const code = google::cloud::StatusCode::kInvalidArgument;
return google::cloud::Status{code, std::move(os).str()};
auto make_status = [](std::ostringstream& os, ErrorInfoBuilder eib) {
return google::cloud::internal::InvalidArgumentError(std::move(os).str(),
std::move(eib));
};

if (options.bucket_name.empty()) {
std::ostringstream os;
os << "Missing --bucket option\n" << usage << "\n";
return make_status(os);
return make_status(os, GCP_ERROR_INFO());
}
if (options.object_count <= 0) {
std::ostringstream os;
os << "Invalid number of objects (" << options.object_count
<< "), check your --object-count option\n";
return make_status(os);
return make_status(os, GCP_ERROR_INFO());
}
if (options.minimum_object_size > options.maximum_object_size) {
std::ostringstream os;
os << "Invalid object size range [" << options.minimum_object_size << ","
<< options.maximum_object_size << "], check your --minimum-object-size"
<< " and --maximum-object-size options";
return make_status(os);
return make_status(os, GCP_ERROR_INFO());
}
if (options.thread_count <= 0) {
std::ostringstream os;
os << "Invalid number of threads (" << options.thread_count
<< "), check your --thread-count option\n";
return make_status(os);
return make_status(os, GCP_ERROR_INFO());
}
if (options.iteration_count <= 0) {
std::ostringstream os;
os << "Invalid number of iterations (" << options.iteration_count
<< "), check your --iteration-count option\n";
return make_status(os);
return make_status(os, GCP_ERROR_INFO());
}
if (options.client_options.get<GrpcNumChannelsOption>() < 0) {
std::ostringstream os;
os << "Invalid number of gRPC channels ("
<< options.client_options.get<GrpcNumChannelsOption>()
<< "), check your --grpc-channel-count option\n";
return make_status(os);
return make_status(os, GCP_ERROR_INFO());
}
return options;
}
Expand Down Expand Up @@ -193,7 +195,8 @@ ParseAggregateUploadThroughputOptions(std::vector<std::string> const& argv,
<< absl::StrJoin(std::next(unparsed.begin()), unparsed.end(), ", ")
<< "\n"
<< usage << "\n";
return Status{StatusCode::kInvalidArgument, std::move(os).str()};
return google::cloud::internal::InvalidArgumentError(std::move(os).str(),
GCP_ERROR_INFO());
}

return ValidateOptions(usage, std::move(options));
Expand Down
12 changes: 7 additions & 5 deletions google/cloud/storage/benchmarks/benchmark_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "google/cloud/internal/absl_str_join_quiet.h"
#include "google/cloud/internal/compute_engine_util.h"
#include "google/cloud/internal/curl_options.h"
#include "google/cloud/internal/make_status.h"
#include "google/cloud/internal/rest_client.h"
#include "google/cloud/internal/throw_delegate.h"
#include "google/cloud/options.h"
Expand Down Expand Up @@ -101,24 +102,25 @@ StatusOr<ApiName> ParseApiName(std::string const& val) {
for (auto a : {ApiName::kApiJson, ApiName::kApiGrpc}) {
if (val == ToString(a)) return a;
}
return Status{StatusCode::kInvalidArgument, "unknown ApiName " + val};
return google::cloud::internal::InvalidArgumentError("unknown ApiName " + val,
GCP_ERROR_INFO());
}

StatusOr<ExperimentLibrary> ParseExperimentLibrary(std::string const& val) {
for (auto v : {ExperimentLibrary::kRaw, ExperimentLibrary::kCppClient}) {
if (val == ToString(v)) return v;
}
return Status{StatusCode::kInvalidArgument,
"unknown ExperimentLibrary " + val};
return google::cloud::internal::InvalidArgumentError(
"unknown ExperimentLibrary " + val, GCP_ERROR_INFO());
}

StatusOr<ExperimentTransport> ParseExperimentTransport(std::string const& val) {
for (auto v : {ExperimentTransport::kDirectPath, ExperimentTransport::kGrpc,
ExperimentTransport::kJson}) {
if (val == ToString(v)) return v;
}
return Status{StatusCode::kInvalidArgument,
"unknown ExperimentTransport " + val};
return google::cloud::internal::InvalidArgumentError(
"unknown ExperimentTransport " + val, GCP_ERROR_INFO());
}

std::string ToString(ExperimentLibrary v) {
Expand Down
22 changes: 12 additions & 10 deletions google/cloud/storage/benchmarks/create_dataset_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
// limitations under the License.

#include "google/cloud/storage/benchmarks/create_dataset_options.h"
#include "google/cloud/internal/make_status.h"
#include <sstream>
#include <utility>

namespace google {
namespace cloud {
Expand Down Expand Up @@ -72,42 +74,42 @@ google::cloud::StatusOr<CreateDatasetOptions> ParseCreateDatasetOptions(
if (unparsed.size() > 2) {
std::ostringstream os;
os << "Unknown arguments or options\n" << usage << "\n";
return google::cloud::Status{google::cloud::StatusCode::kInvalidArgument,
std::move(os).str()};
return google::cloud::internal::InvalidArgumentError(std::move(os).str(),
GCP_ERROR_INFO());
}
if (unparsed.size() == 2) {
options.bucket_name = unparsed[1];
}
if (options.bucket_name.empty()) {
std::ostringstream os;
os << "Missing value for --bucket_name option" << usage << "\n";
return google::cloud::Status{google::cloud::StatusCode::kInvalidArgument,
std::move(os).str()};
return google::cloud::internal::InvalidArgumentError(std::move(os).str(),
GCP_ERROR_INFO());
}

if (options.minimum_object_size > options.maximum_object_size) {
std::ostringstream os;
os << "Invalid object size range [" << options.minimum_object_size << ","
<< options.maximum_object_size << "), check your --minimum-object-size"
<< " and --maximum-object-size options";
return google::cloud::Status{google::cloud::StatusCode::kInvalidArgument,
std::move(os).str()};
return google::cloud::internal::InvalidArgumentError(std::move(os).str(),
GCP_ERROR_INFO());
}

if (options.object_count <= 0) {
std::ostringstream os;
os << "Invalid object count (" << options.object_count
<< "), check your --object-count option";
return google::cloud::Status{google::cloud::StatusCode::kInvalidArgument,
std::move(os).str()};
return google::cloud::internal::InvalidArgumentError(std::move(os).str(),
GCP_ERROR_INFO());
}

if (options.thread_count <= 0) {
std::ostringstream os;
os << "Invalid thread count (" << options.thread_count
<< "), check your --thread-count option";
return google::cloud::Status{google::cloud::StatusCode::kInvalidArgument,
std::move(os).str()};
return google::cloud::internal::InvalidArgumentError(std::move(os).str(),
GCP_ERROR_INFO());
}

return options;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
#include "google/cloud/internal/build_info.h"
#include "google/cloud/internal/format_time_point.h"
#include "google/cloud/internal/getenv.h"
#include "google/cloud/internal/make_status.h"
#include "google/cloud/internal/random.h"
#include <fstream>
#include <future>
#include <iomanip>
#include <sstream>
#include <utility>

namespace {
namespace gcs = ::google::cloud::storage;
Expand Down Expand Up @@ -247,17 +249,17 @@ google::cloud::StatusOr<Options> ParseArgsDefault(
if (unparsed.size() > 2) {
std::ostringstream os;
os << "Unknown arguments or options\n" << usage << "\n";
return google::cloud::Status{google::cloud::StatusCode::kInvalidArgument,
std::move(os).str()};
return google::cloud::internal::InvalidArgumentError(std::move(os).str(),
GCP_ERROR_INFO());
}
if (unparsed.size() == 2) {
options.region = unparsed[1];
}
if (options.region.empty()) {
std::ostringstream os;
os << "Missing value for --region option" << usage << "\n";
return google::cloud::Status{google::cloud::StatusCode::kInvalidArgument,
std::move(os).str()};
return google::cloud::internal::InvalidArgumentError(std::move(os).str(),
GCP_ERROR_INFO());
}

return options;
Expand All @@ -267,8 +269,8 @@ google::cloud::StatusOr<Options> SelfTest() {
using ::google::cloud::internal::GetEnv;
using ::google::cloud::internal::Sample;

google::cloud::Status const self_test_error(
google::cloud::StatusCode::kUnknown, "self-test failure");
auto const self_test_error = google::cloud::internal::UnknownError(
"self-test failure", GCP_ERROR_INFO());

{
auto options = ParseArgsDefault(
Expand All @@ -292,8 +294,8 @@ google::cloud::StatusOr<Options> SelfTest() {
if (!value.empty()) continue;
std::ostringstream os;
os << "The environment variable " << var << " is not set or empty";
return google::cloud::Status(google::cloud::StatusCode::kUnknown,
std::move(os).str());
return google::cloud::internal::UnknownError(std::move(os).str(),
GCP_ERROR_INFO());
}
return ParseArgsDefault({
"self-test",
Expand Down
Loading