Debian Codegraph @

src/core/lib/gprpp/status_helper.h

raw
1 //
2 //
3 // Copyright 2021 the gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #ifndef GRPC_CORE_LIB_GPRPP_STATUS_HELPER_H
20 #define GRPC_CORE_LIB_GPRPP_STATUS_HELPER_H
21
22 #include <grpc/support/port_platform.h>
23
24 #include <stdint.h>
25
26 #include <string>
27 #include <vector>
28
29 #include "absl/status/status.h"
30 #include "absl/strings/string_view.h"
31 #include "absl/time/time.h"
32 #include "absl/types/optional.h"
33
34 #include "src/core/lib/gprpp/debug_location.h"
35
36 extern "C" {
37 struct google_rpc_Status;
38 struct upb_Arena;
39 }
40
41 #define GRPC_RETURN_IF_ERROR(expr) \
42 do { \
43 const absl::Status status = (expr); \
44 if (!status.ok()) return status; \
45 } while (0)
46
47 namespace grpc_core {
48
49 /// This enum should have the same value of grpc_error_ints
50 enum class StatusIntProperty {
51 /// 'errno' from the operating system
52 kErrorNo,
53 /// __LINE__ from the call site creating the error
54 kFileLine,
55 /// stream identifier: for errors that are associated with an individual
56 /// wire stream
57 kStreamId,
58 /// grpc status code representing this error
59 // TODO(veblush): Remove this after grpc_error is replaced with absl::Status
60 kRpcStatus,
61 /// offset into some binary blob (usually represented by
62 /// RAW_BYTES) where the error occurred
63 kOffset,
64 /// context sensitive index associated with the error
65 kIndex,
66 /// context sensitive size associated with the error
67 kSize,
68 /// http2 error code associated with the error (see the HTTP2 RFC)
69 kHttp2Error,
70 /// TSI status code associated with the error
71 kTsiCode,
72 /// WSAGetLastError() reported when this error occurred
73 kWsaError,
74 /// File descriptor associated with this error
75 kFd,
76 /// HTTP status (i.e. 404)
77 kHttpStatus,
78 /// chttp2: did the error occur while a write was in progress
79 kOccurredDuringWrite,
80 /// channel connectivity state associated with the error
81 ChannelConnectivityState,
82 /// LB policy drop
83 kLbPolicyDrop,
84 };
85
86 /// This enum should have the same value of grpc_error_strs
87 enum class StatusStrProperty {
88 /// top-level textual description of this error
89 kDescription,
90 /// source file in which this error occurred
91 kFile,
92 /// operating system description of this error
93 kOsError,
94 /// syscall that generated this error
95 kSyscall,
96 /// peer that we were trying to communicate when this error occurred
97 kTargetAddress,
98 /// grpc status message associated with this error
99 kGrpcMessage,
100 /// hex dump (or similar) with the data that generated this error
101 kRawBytes,
102 /// tsi error string associated with this error
103 kTsiError,
104 /// filename that we were trying to read/write when this error occurred
105 kFilename,
106 /// key associated with the error
107 kKey,
108 /// value associated with the error
109 kValue,
110 };
111
112 /// This enum should have the same value of grpc_error_times
113 enum class StatusTimeProperty {
114 /// timestamp of error creation
115 kCreated,
116 };
117
118 /// Creates a status with given additional information
119 absl::Status StatusCreate(
120 absl::StatusCode code, absl::string_view msg, const DebugLocation& location,
121 std::vector<absl::Status> children) GRPC_MUST_USE_RESULT;
122
123 /// Sets the int property to the status
124 void StatusSetInt(absl::Status* status, StatusIntProperty key, intptr_t value);
125
126 /// Gets the int property from the status
127 absl::optional<intptr_t> StatusGetInt(
128 const absl::Status& status, StatusIntProperty key) GRPC_MUST_USE_RESULT;
129
130 /// Sets the str property to the status
131 void StatusSetStr(absl::Status* status, StatusStrProperty key,
132 absl::string_view value);
133
134 /// Gets the str property from the status
135 absl::optional<std::string> StatusGetStr(
136 const absl::Status& status, StatusStrProperty key) GRPC_MUST_USE_RESULT;
137
138 /// Sets the time property to the status
139 void StatusSetTime(absl::Status* status, StatusTimeProperty key,
140 absl::Time time);
141
142 /// Gets the time property from the status
143 absl::optional<absl::Time> StatusGetTime(
144 const absl::Status& status, StatusTimeProperty key) GRPC_MUST_USE_RESULT;
145
146 /// Adds a child status to status
147 void StatusAddChild(absl::Status* status, absl::Status child);
148
149 /// Returns all children status from a status
150 std::vector<absl::Status> StatusGetChildren(absl::Status status)
151 GRPC_MUST_USE_RESULT;
152
153 /// Returns a string representation from status
154 /// Error status will be like
155 /// STATUS[:MESSAGE] [{PAYLOADS[, children:[CHILDREN-STATUS-LISTS]]}]
156 /// e.g.
157 /// CANCELLATION:SampleMessage {errno:'2021', line:'54', children:[ABORTED]}
158 std::string StatusToString(const absl::Status& status) GRPC_MUST_USE_RESULT;
159
160 namespace internal {
161
162 /// Builds a upb message, google_rpc_Status from a status
163 /// This is for internal implementation & test only
164 google_rpc_Status* StatusToProto(const absl::Status& status,
165 upb_Arena* arena) GRPC_MUST_USE_RESULT;
166
167 /// Builds a status from a upb message, google_rpc_Status
168 /// This is for internal implementation & test only
169 absl::Status StatusFromProto(google_rpc_Status* msg) GRPC_MUST_USE_RESULT;
170
171 /// Returns ptr that is allocated in the heap memory and the given status is
172 /// copied into. This ptr can be used to get Status later and should be
173 /// freed by StatusFreeHeapPtr. This can be 0 in case of OkStatus.
174 uintptr_t StatusAllocHeapPtr(absl::Status s);
175
176 /// Frees the allocated status at heap ptr.
177 void StatusFreeHeapPtr(uintptr_t ptr);
178
179 /// Get the status from a heap ptr.
180 absl::Status StatusGetFromHeapPtr(uintptr_t ptr);
181
182 /// Move the status from a heap ptr. (GetFrom & FreeHeap)
183 absl::Status StatusMoveFromHeapPtr(uintptr_t ptr);
184
185 } // namespace internal
186
187 } // namespace grpc_core
188
189 #endif // GRPC_CORE_LIB_GPRPP_STATUS_HELPER_H