-
Notifications
You must be signed in to change notification settings - Fork 375
/
grpc_error_delegate.cc
132 lines (121 loc) · 4.97 KB
/
grpc_error_delegate.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "google/cloud/grpc_error_delegate.h"
#include "google/cloud/internal/status_payload_keys.h"
#include "absl/types/optional.h"
#include <google/protobuf/any.pb.h>
#include <google/protobuf/text_format.h>
#include <google/rpc/error_details.pb.h>
namespace google {
namespace cloud {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace {
google::cloud::StatusCode MapStatusCode(grpc::StatusCode const& code) {
switch (code) {
case grpc::StatusCode::OK:
return google::cloud::StatusCode::kOk;
case grpc::StatusCode::CANCELLED:
return google::cloud::StatusCode::kCancelled;
case grpc::StatusCode::UNKNOWN:
return google::cloud::StatusCode::kUnknown;
case grpc::StatusCode::INVALID_ARGUMENT:
return google::cloud::StatusCode::kInvalidArgument;
case grpc::StatusCode::DEADLINE_EXCEEDED:
return google::cloud::StatusCode::kDeadlineExceeded;
case grpc::StatusCode::NOT_FOUND:
return google::cloud::StatusCode::kNotFound;
case grpc::StatusCode::ALREADY_EXISTS:
return google::cloud::StatusCode::kAlreadyExists;
case grpc::StatusCode::PERMISSION_DENIED:
return google::cloud::StatusCode::kPermissionDenied;
case grpc::StatusCode::UNAUTHENTICATED:
return google::cloud::StatusCode::kUnauthenticated;
case grpc::StatusCode::RESOURCE_EXHAUSTED:
return google::cloud::StatusCode::kResourceExhausted;
case grpc::StatusCode::FAILED_PRECONDITION:
return google::cloud::StatusCode::kFailedPrecondition;
case grpc::StatusCode::ABORTED:
return google::cloud::StatusCode::kAborted;
case grpc::StatusCode::OUT_OF_RANGE:
return google::cloud::StatusCode::kOutOfRange;
case grpc::StatusCode::UNIMPLEMENTED:
return google::cloud::StatusCode::kUnimplemented;
case grpc::StatusCode::INTERNAL:
return google::cloud::StatusCode::kInternal;
case grpc::StatusCode::UNAVAILABLE:
return google::cloud::StatusCode::kUnavailable;
case grpc::StatusCode::DATA_LOSS:
return google::cloud::StatusCode::kDataLoss;
default:
return google::cloud::StatusCode::kUnknown;
}
}
// Unpacks the ErrorInfo from the Status proto, if one exists.
absl::optional<google::rpc::ErrorInfo> GetErrorInfoProto(
google::rpc::Status const& proto) {
for (google::protobuf::Any const& any : proto.details()) {
if (any.Is<google::rpc::ErrorInfo>()) {
google::rpc::ErrorInfo error_info;
if (any.UnpackTo(&error_info)) return error_info;
}
}
return absl::nullopt;
}
void FillErrorInfo(google::rpc::Status const& status, ErrorInfo& error_info) {
auto proto = GetErrorInfoProto(status);
if (!proto) return;
std::unordered_map<std::string, std::string> metadata;
for (auto const& e : proto->metadata()) metadata[e.first] = e.second;
error_info = ErrorInfo{proto->reason(), proto->domain(), std::move(metadata)};
}
} // namespace
google::cloud::Status MakeStatusFromRpcError(grpc::Status const& status) {
// Fast path for "OK" statuses, which cannot have messages or payloads.
if (status.ok()) return Status{};
auto const e = status.error_details();
if (!e.empty()) {
google::rpc::Status proto;
if (!proto.ParseFromString(e)) {
return MakeStatusFromRpcError(
status.error_code(),
status.error_message() + " (discarded invalid error_details)");
}
return MakeStatusFromRpcError(proto);
}
return MakeStatusFromRpcError(status.error_code(), status.error_message());
}
google::cloud::Status MakeStatusFromRpcError(grpc::StatusCode code,
std::string what) {
return google::cloud::Status(MapStatusCode(code), std::move(what));
}
google::cloud::Status MakeStatusFromRpcError(google::rpc::Status const& proto) {
// Fast path for "OK" statuses, which cannot have messages or payloads.
if (proto.code() == static_cast<std::int32_t>(StatusCode::kOk))
return Status{};
StatusCode code = StatusCode::kUnknown;
if (proto.code() >= 0 &&
proto.code() <= static_cast<std::int32_t>(StatusCode::kUnauthenticated)) {
code = static_cast<StatusCode>(proto.code());
}
ErrorInfo error_info;
FillErrorInfo(proto, error_info);
auto status = Status(code, proto.message(), error_info);
google::cloud::internal::SetPayload(
status, google::cloud::internal::kStatusPayloadGrpcProto,
proto.SerializeAsString());
return status;
}
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace cloud
} // namespace google