userver: userver/ugrpc/rich_status.hpp Source File
Loading...
Searching...
No Matches
rich_status.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <optional>
7
8#include <google/protobuf/any.pb.h>
9#include <google/protobuf/message.h>
10#include <google/rpc/error_details.pb.h>
11#include <google/rpc/status.pb.h>
12#include <grpcpp/support/status.h>
13
14USERVER_NAMESPACE_BEGIN
15
16namespace ugrpc {
17
43class RichStatus final {
44public:
46 RichStatus() = default;
47
52 explicit RichStatus(const grpc::Status& grpc_status);
53
59 template <typename... TDetails>
60 RichStatus(grpc::StatusCode code, std::string message, TDetails&&... details);
61
65 template <typename TDetail>
66 RichStatus& AddDetail(TDetail&& detail);
67
78 template <typename TRichErrorDetail>
79 [[nodiscard]] static std::optional<TRichErrorDetail> TryGetDetail(const google::rpc::Status& status);
80
81 [[nodiscard]] const google::rpc::Status& GetGoogleStatus() const& { return google_status_; }
82 google::rpc::Status GetGoogleStatus() && { return std::move(google_status_); }
83
86 [[nodiscard]] grpc::Status ToGrpcStatus() const;
87
88 [[nodiscard]] explicit operator grpc::Status() const { return ToGrpcStatus(); }
89
90private:
91 google::rpc::Status google_status_;
92};
93
109struct ErrorInfo {
116 std::string reason;
117
119 std::string domain;
120
122 std::unordered_map<std::string, std::string> metadata;
123
124 google::rpc::ErrorInfo ToGoogleErrorDetail() const&;
125 google::rpc::ErrorInfo ToGoogleErrorDetail() &&;
126
127 static std::optional<ErrorInfo> TryUnpack(const google::protobuf::Any& any);
128};
129
137struct RetryInfo {
139 std::chrono::nanoseconds retry_delay;
140
141 google::rpc::RetryInfo ToGoogleErrorDetail() const;
142
143 static std::optional<RetryInfo> TryUnpack(const google::protobuf::Any& any);
144};
145
152struct DebugInfo {
153 std::vector<std::string> stack_entries;
154 std::string detail;
155
156 google::rpc::DebugInfo ToGoogleErrorDetail() const&;
157 google::rpc::DebugInfo ToGoogleErrorDetail() &&;
158
159 static std::optional<DebugInfo> TryUnpack(const google::protobuf::Any& any);
160};
161
169 std::string subject;
170
172 std::string description;
173};
174
183 std::vector<QuotaViolation> violations;
184
185 google::rpc::QuotaFailure ToGoogleErrorDetail() const&;
186 google::rpc::QuotaFailure ToGoogleErrorDetail() &&;
187
188 static std::optional<QuotaFailure> TryUnpack(const google::protobuf::Any& any);
189};
190
199 std::string type;
200
205 std::string subject;
206
211 std::string description;
212};
213
227 std::vector<PreconditionViolation> violations;
228
229 google::rpc::PreconditionFailure ToGoogleErrorDetail() const&;
230 google::rpc::PreconditionFailure ToGoogleErrorDetail() &&;
231
232 static std::optional<PreconditionFailure> TryUnpack(const google::protobuf::Any& any);
233};
234
240 std::string field;
241
243 std::string description;
244};
245
255 std::vector<FieldViolation> field_violations;
256
257 google::rpc::BadRequest ToGoogleErrorDetail() const&;
258 google::rpc::BadRequest ToGoogleErrorDetail() &&;
259
260 static std::optional<BadRequest> TryUnpack(const google::protobuf::Any& any);
261};
262
275 std::string request_id;
276 std::string serving_data;
277
278 google::rpc::RequestInfo ToGoogleErrorDetail() const&;
279 google::rpc::RequestInfo ToGoogleErrorDetail() &&;
280
281 static std::optional<RequestInfo> TryUnpack(const google::protobuf::Any& any);
282};
283
297 std::string resource_type;
298
300 std::string resource_name;
301
303 std::string owner;
304
306 std::string description;
307
308 google::rpc::ResourceInfo ToGoogleErrorDetail() const&;
309 google::rpc::ResourceInfo ToGoogleErrorDetail() &&;
310
311 static std::optional<ResourceInfo> TryUnpack(const google::protobuf::Any& any);
312};
313
317struct HelpLink {
318 std::string description;
319 std::string url;
320};
321
334struct Help {
336 std::vector<HelpLink> links;
337
338 google::rpc::Help ToGoogleErrorDetail() const&;
339 google::rpc::Help ToGoogleErrorDetail() &&;
340
341 static std::optional<Help> TryUnpack(const google::protobuf::Any& any);
342};
343
361 std::string locale;
362
368 std::string message;
369
370 google::rpc::LocalizedMessage ToGoogleErrorDetail() const&;
371 google::rpc::LocalizedMessage ToGoogleErrorDetail() &&;
372
373 static std::optional<LocalizedMessage> TryUnpack(const google::protobuf::Any& any);
374};
375
376template <typename... TDetails>
377RichStatus::RichStatus(grpc::StatusCode code, std::string message, TDetails&&... details) {
378 google_status_.set_code(static_cast<int>(code));
379 google_status_.set_message(std::move(message));
380 (AddDetail(std::forward<TDetails>(details)), ...);
381}
382
383template <typename TDetail>
385 const auto pb_google_detail = std::forward<TDetail>(detail).ToGoogleErrorDetail();
386 google_status_.add_details()->PackFrom(pb_google_detail);
387 return *this;
388}
389
390template <typename TRichErrorDetail>
391std::optional<TRichErrorDetail> RichStatus::TryGetDetail(const google::rpc::Status& status) {
392 for (const auto& detail : status.details()) {
393 const auto rich_error_detail_opt = TRichErrorDetail::TryUnpack(detail);
394 if (rich_error_detail_opt) {
395 return *rich_error_detail_opt;
396 }
397 }
398 return std::nullopt;
399}
400
401} // namespace ugrpc
402
403USERVER_NAMESPACE_END