Merge pull request #19603 from yang-g/status_code

Qualify the error code with StatusCode::
pull/19588/head
Yang Gao 5 years ago committed by GitHub
commit 9536b2c9b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      src/cpp/client/secure_credentials.cc
  2. 10
      test/cpp/client/credentials_test.cc

@ -23,7 +23,7 @@
#include <grpc/support/log.h>
#include <grpc/support/string_util.h>
#include <grpcpp/channel.h>
#include <grpcpp/impl/codegen/status_code_enum.h>
#include <grpcpp/impl/codegen/status.h>
#include <grpcpp/impl/grpc_library.h>
#include <grpcpp/support/channel_arguments.h>
@ -139,7 +139,8 @@ grpc::Status StsCredentialsOptionsFromJson(const grpc::string& json_string,
void operator()(grpc_json* json) { grpc_json_destroy(json); }
};
if (options == nullptr) {
return grpc::Status(grpc::INVALID_ARGUMENT, "options cannot be nullptr.");
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT,
"options cannot be nullptr.");
}
ClearStsCredentialsOptions(options);
std::vector<char> scratchpad(json_string.c_str(),
@ -147,7 +148,7 @@ grpc::Status StsCredentialsOptionsFromJson(const grpc::string& json_string,
std::unique_ptr<grpc_json, GrpcJsonDeleter> json(
grpc_json_parse_string(&scratchpad[0]));
if (json == nullptr) {
return grpc::Status(grpc::INVALID_ARGUMENT, "Invalid json.");
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "Invalid json.");
}
// Required fields.
@ -155,7 +156,7 @@ grpc::Status StsCredentialsOptionsFromJson(const grpc::string& json_string,
json.get(), "token_exchange_service_uri", nullptr);
if (value == nullptr) {
ClearStsCredentialsOptions(options);
return grpc::Status(grpc::INVALID_ARGUMENT,
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT,
"token_exchange_service_uri must be specified.");
}
options->token_exchange_service_uri.assign(value);
@ -163,7 +164,7 @@ grpc::Status StsCredentialsOptionsFromJson(const grpc::string& json_string,
grpc_json_get_string_property(json.get(), "subject_token_path", nullptr);
if (value == nullptr) {
ClearStsCredentialsOptions(options);
return grpc::Status(grpc::INVALID_ARGUMENT,
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT,
"subject_token_path must be specified.");
}
options->subject_token_path.assign(value);
@ -171,7 +172,7 @@ grpc::Status StsCredentialsOptionsFromJson(const grpc::string& json_string,
grpc_json_get_string_property(json.get(), "subject_token_type", nullptr);
if (value == nullptr) {
ClearStsCredentialsOptions(options);
return grpc::Status(grpc::INVALID_ARGUMENT,
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT,
"subject_token_type must be specified.");
}
options->subject_token_type.assign(value);
@ -199,7 +200,8 @@ grpc::Status StsCredentialsOptionsFromJson(const grpc::string& json_string,
// Builds STS credentials Options from the $STS_CREDENTIALS env var.
grpc::Status StsCredentialsOptionsFromEnv(StsCredentialsOptions* options) {
if (options == nullptr) {
return grpc::Status(grpc::INVALID_ARGUMENT, "options cannot be nullptr.");
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT,
"options cannot be nullptr.");
}
ClearStsCredentialsOptions(options);
grpc_slice json_string = grpc_empty_slice();
@ -214,13 +216,14 @@ grpc::Status StsCredentialsOptionsFromEnv(StsCredentialsOptions* options) {
};
if (sts_creds_path == nullptr) {
status = grpc::Status(grpc::NOT_FOUND,
status = grpc::Status(grpc::StatusCode::NOT_FOUND,
"STS_CREDENTIALS environment variable not set.");
return cleanup();
}
error = grpc_load_file(sts_creds_path, 1, &json_string);
if (error != GRPC_ERROR_NONE) {
status = grpc::Status(grpc::NOT_FOUND, grpc_error_string(error));
status =
grpc::Status(grpc::StatusCode::NOT_FOUND, grpc_error_string(error));
return cleanup();
}
status = StsCredentialsOptionsFromJson(

@ -119,7 +119,7 @@ TEST_F(CredentialsTest, StsCredentialsOptionsJson) {
I'm not a valid JSON.
)";
EXPECT_EQ(
grpc::INVALID_ARGUMENT,
grpc::StatusCode::INVALID_ARGUMENT,
grpc::experimental::StsCredentialsOptionsFromJson(invalid_json, &options)
.error_code());
@ -130,7 +130,7 @@ TEST_F(CredentialsTest, StsCredentialsOptionsJson) {
})";
auto status = grpc::experimental::StsCredentialsOptionsFromJson(
invalid_json_missing_subject_token_type, &options);
EXPECT_EQ(grpc::INVALID_ARGUMENT, status.error_code());
EXPECT_EQ(grpc::StatusCode::INVALID_ARGUMENT, status.error_code());
EXPECT_THAT(status.error_message(),
::testing::HasSubstr("subject_token_type"));
@ -141,7 +141,7 @@ TEST_F(CredentialsTest, StsCredentialsOptionsJson) {
})";
status = grpc::experimental::StsCredentialsOptionsFromJson(
invalid_json_missing_subject_token_path, &options);
EXPECT_EQ(grpc::INVALID_ARGUMENT, status.error_code());
EXPECT_EQ(grpc::StatusCode::INVALID_ARGUMENT, status.error_code());
EXPECT_THAT(status.error_message(),
::testing::HasSubstr("subject_token_path"));
@ -152,7 +152,7 @@ TEST_F(CredentialsTest, StsCredentialsOptionsJson) {
})";
status = grpc::experimental::StsCredentialsOptionsFromJson(
invalid_json_missing_token_exchange_uri, &options);
EXPECT_EQ(grpc::INVALID_ARGUMENT, status.error_code());
EXPECT_EQ(grpc::StatusCode::INVALID_ARGUMENT, status.error_code());
EXPECT_THAT(status.error_message(),
::testing::HasSubstr("token_exchange_service_uri"));
}
@ -162,7 +162,7 @@ TEST_F(CredentialsTest, StsCredentialsOptionsFromEnv) {
gpr_unsetenv("STS_CREDENTIALS");
grpc::experimental::StsCredentialsOptions options;
auto status = grpc::experimental::StsCredentialsOptionsFromEnv(&options);
EXPECT_EQ(grpc::NOT_FOUND, status.error_code());
EXPECT_EQ(grpc::StatusCode::NOT_FOUND, status.error_code());
// Set env and check for success.
const char valid_json[] = R"(

Loading…
Cancel
Save