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 {
24 public:
25 explicit BaseError(std::string message);
26
27 const char* what() const noexcept override;
28
29 private:
30 std::string message_;
31};
32
33/// @brief Error during an RPC
34class RpcError : public BaseError {
35 public:
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 {
43 public:
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 {
49 public:
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 {
56 public:
57 ErrorWithStatus(std::string_view call_name, grpc::Status&& status,
58 std::optional<google::rpc::Status>&& gstatus,
59 std::optional<std::string>&& message);
60
61 const grpc::Status& GetStatus() const noexcept;
62
63 const std::optional<google::rpc::Status>& GetGStatus() const noexcept;
64
65 const std::optional<std::string>& GetGStatusString() const noexcept;
66
67 private:
68 grpc::Status status_;
69 std::optional<google::rpc::Status> gstatus_;
70 std::optional<std::string> gstatus_string_;
71};
72
73/// @brief Concrete errors for all the error codes
74/// @see <grpcpp/impl/codegen/status_code_enum.h> for error code details
75/// @{
76class CancelledError final : public ErrorWithStatus {
77 public:
78 using ErrorWithStatus::ErrorWithStatus;
79};
80
81class UnknownError final : public ErrorWithStatus {
82 public:
83 using ErrorWithStatus::ErrorWithStatus;
84};
85
86class InvalidArgumentError final : public ErrorWithStatus {
87 public:
88 using ErrorWithStatus::ErrorWithStatus;
89};
90
91class DeadlineExceededError final : public ErrorWithStatus {
92 public:
93 using ErrorWithStatus::ErrorWithStatus;
94};
95
96class NotFoundError final : public ErrorWithStatus {
97 public:
98 using ErrorWithStatus::ErrorWithStatus;
99};
100
101class AlreadyExistsError final : public ErrorWithStatus {
102 public:
103 using ErrorWithStatus::ErrorWithStatus;
104};
105
106class PermissionDeniedError final : public ErrorWithStatus {
107 public:
108 using ErrorWithStatus::ErrorWithStatus;
109};
110
111class ResourceExhaustedError final : public ErrorWithStatus {
112 public:
113 using ErrorWithStatus::ErrorWithStatus;
114};
115
116class FailedPreconditionError final : public ErrorWithStatus {
117 public:
118 using ErrorWithStatus::ErrorWithStatus;
119};
120
121class AbortedError final : public ErrorWithStatus {
122 public:
123 using ErrorWithStatus::ErrorWithStatus;
124};
125
126class OutOfRangeError final : public ErrorWithStatus {
127 public:
128 using ErrorWithStatus::ErrorWithStatus;
129};
130
131class UnimplementedError final : public ErrorWithStatus {
132 public:
133 using ErrorWithStatus::ErrorWithStatus;
134};
135
136class InternalError final : public ErrorWithStatus {
137 public:
138 using ErrorWithStatus::ErrorWithStatus;
139};
140
141class UnavailableError final : public ErrorWithStatus {
142 public:
143 using ErrorWithStatus::ErrorWithStatus;
144};
145
146class DataLossError final : public ErrorWithStatus {
147 public:
148 using ErrorWithStatus::ErrorWithStatus;
149};
150
151class UnauthenticatedError final : public ErrorWithStatus {
152 public:
153 using ErrorWithStatus::ErrorWithStatus;
154};
155/// @}
156
157namespace impl {
158
159[[noreturn]] void ThrowErrorWithStatus(
160 std::string_view call_name, grpc::Status&& status,
161 std::optional<google::rpc::Status>&& gstatus,
162 std::optional<std::string>&& gstatus_string);
163
164} // namespace impl
165
166} // namespace ugrpc::client
167
168USERVER_NAMESPACE_END