userver: userver/ugrpc/client/exceptions.hpp Source File
Loading...
Searching...
No Matches
exceptions.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/ugrpc/client/exceptions.hpp
4/// @brief Exceptions thrown by gRPC client streams
5
6#include <exception>
7#include <optional>
8#include <string>
9#include <string_view>
10
11#include <google/rpc/status.pb.h>
12#include <grpcpp/support/status.h>
13
14USERVER_NAMESPACE_BEGIN
15
16/// Asynchronous gRPC driver
17namespace ugrpc {}
18
19/// Client-side utilities
20namespace ugrpc::client {
21
22/// @brief Base exception for all the client errors
23class BaseError : public std::exception {
24public:
25 explicit BaseError(std::string message);
26
27 const char* what() const noexcept override;
28
29private:
30 std::string message_;
31};
32
33/// @brief Error during an RPC
34class RpcError : public BaseError {
35public:
36 RpcError(std::string_view call_name, std::string_view additional_info);
37};
38
39/// @brief RPC failed without a status. This means that either the call got
40/// cancelled using `TryCancel`, the deadline has expired, or the channel is
41/// broken.
42class RpcInterruptedError final : public RpcError {
43public:
44 RpcInterruptedError(std::string_view call_name, std::string_view stage);
45};
46
47/// @brief RPC failed due to task cancellation
48class RpcCancelledError final : public RpcError {
49public:
50 RpcCancelledError(std::string_view call_name, std::string_view stage);
51};
52
53/// @brief Error with grpc::Status details
54/// @see <grpcpp/impl/codegen/status_code_enum.h> for error code details
55class ErrorWithStatus : public RpcError {
56public:
57 ErrorWithStatus(
58 std::string_view call_name,
59 grpc::Status&& status,
60 std::optional<google::rpc::Status>&& gstatus,
61 std::optional<std::string>&& message
62 );
63
64 const grpc::Status& GetStatus() const noexcept;
65
66 const std::optional<google::rpc::Status>& GetGStatus() const noexcept;
67
68 const std::optional<std::string>& GetGStatusString() const noexcept;
69
70private:
71 grpc::Status status_;
72 std::optional<google::rpc::Status> gstatus_;
73 std::optional<std::string> gstatus_string_;
74};
75
76/// @brief Concrete errors for all the error codes
77/// @see <grpcpp/impl/codegen/status_code_enum.h> for error code details
78/// @{
79class CancelledError final : public ErrorWithStatus {
80public:
81 using ErrorWithStatus::ErrorWithStatus;
82};
83
84class UnknownError final : public ErrorWithStatus {
85public:
86 using ErrorWithStatus::ErrorWithStatus;
87};
88
89class InvalidArgumentError final : public ErrorWithStatus {
90public:
91 using ErrorWithStatus::ErrorWithStatus;
92};
93
94class DeadlineExceededError final : public ErrorWithStatus {
95public:
96 using ErrorWithStatus::ErrorWithStatus;
97};
98
99class NotFoundError final : public ErrorWithStatus {
100public:
101 using ErrorWithStatus::ErrorWithStatus;
102};
103
104class AlreadyExistsError final : public ErrorWithStatus {
105public:
106 using ErrorWithStatus::ErrorWithStatus;
107};
108
109class PermissionDeniedError final : public ErrorWithStatus {
110public:
111 using ErrorWithStatus::ErrorWithStatus;
112};
113
114class ResourceExhaustedError final : public ErrorWithStatus {
115public:
116 using ErrorWithStatus::ErrorWithStatus;
117};
118
119class FailedPreconditionError final : public ErrorWithStatus {
120public:
121 using ErrorWithStatus::ErrorWithStatus;
122};
123
124class AbortedError final : public ErrorWithStatus {
125public:
126 using ErrorWithStatus::ErrorWithStatus;
127};
128
129class OutOfRangeError final : public ErrorWithStatus {
130public:
131 using ErrorWithStatus::ErrorWithStatus;
132};
133
134class UnimplementedError final : public ErrorWithStatus {
135public:
136 using ErrorWithStatus::ErrorWithStatus;
137};
138
139class InternalError final : public ErrorWithStatus {
140public:
141 using ErrorWithStatus::ErrorWithStatus;
142};
143
144class UnavailableError final : public ErrorWithStatus {
145public:
146 using ErrorWithStatus::ErrorWithStatus;
147};
148
149class DataLossError final : public ErrorWithStatus {
150public:
151 using ErrorWithStatus::ErrorWithStatus;
152};
153
154class UnauthenticatedError final : public ErrorWithStatus {
155public:
156 using ErrorWithStatus::ErrorWithStatus;
157};
158/// @}
159
160namespace impl {
161
162[[noreturn]] void ThrowErrorWithStatus(
163 std::string_view call_name,
164 grpc::Status&& status,
165 std::optional<google::rpc::Status>&& gstatus,
166 std::optional<std::string>&& gstatus_string
167);
168
169} // namespace impl
170
171} // namespace ugrpc::client
172
173USERVER_NAMESPACE_END