Google Cloud C++ Client  1.32.1
C++ Client Library for Google Cloud Platform
grpc_error_delegate.cc
Go to the documentation of this file.
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "google/cloud/grpc_error_delegate.h"
16 #include <google/protobuf/text_format.h>
17 
18 namespace google {
19 namespace cloud {
20 inline namespace GOOGLE_CLOUD_CPP_NS {
21 namespace {
22 google::cloud::StatusCode MapStatusCode(grpc::StatusCode const& code) {
23  switch (code) {
24  case grpc::StatusCode::OK:
26  case grpc::StatusCode::CANCELLED:
28  case grpc::StatusCode::UNKNOWN:
30  case grpc::StatusCode::INVALID_ARGUMENT:
32  case grpc::StatusCode::DEADLINE_EXCEEDED:
34  case grpc::StatusCode::NOT_FOUND:
36  case grpc::StatusCode::ALREADY_EXISTS:
38  case grpc::StatusCode::PERMISSION_DENIED:
40  case grpc::StatusCode::UNAUTHENTICATED:
42  case grpc::StatusCode::RESOURCE_EXHAUSTED:
44  case grpc::StatusCode::FAILED_PRECONDITION:
46  case grpc::StatusCode::ABORTED:
48  case grpc::StatusCode::OUT_OF_RANGE:
50  case grpc::StatusCode::UNIMPLEMENTED:
52  case grpc::StatusCode::INTERNAL:
54  case grpc::StatusCode::UNAVAILABLE:
56  case grpc::StatusCode::DATA_LOSS:
58  default:
60  }
61 }
62 } // namespace
63 
64 google::cloud::Status MakeStatusFromRpcError(grpc::Status const& status) {
65  return MakeStatusFromRpcError(status.error_code(), status.error_message());
66 }
67 
68 google::cloud::Status MakeStatusFromRpcError(grpc::StatusCode code,
69  std::string what) {
70  // TODO(#1912): Pass along status.error_details() once we have `absl::Status`
71  // or some version that supports binary blobs of data.
72  return google::cloud::Status(MapStatusCode(code), std::move(what));
73 }
74 
76  google::rpc::Status const& status) {
78  if (status.code() >= 0 &&
79  status.code() <=
80  static_cast<std::int32_t>(StatusCode::kUnauthenticated)) {
81  code = static_cast<StatusCode>(status.code());
82  }
83  return Status(code, status.message());
84 }
85 
86 } // namespace GOOGLE_CLOUD_CPP_NS
87 } // namespace cloud
88 } // namespace google