• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SRC_CORE_LIB_GPRPP_STATUS_HELPER_H
20 #define GRPC_SRC_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(absl::StatusCode code, absl::string_view msg,
120                           const DebugLocation& location,
121                           std::vector<absl::Status> children);
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 GRPC_MUST_USE_RESULT
128 absl::optional<intptr_t> StatusGetInt(const absl::Status& status,
129                                       StatusIntProperty key);
130 
131 /// Sets the str property to the status
132 void StatusSetStr(absl::Status* status, StatusStrProperty key,
133                   absl::string_view value);
134 
135 /// Gets the str property from the status
136 GRPC_MUST_USE_RESULT absl::optional<std::string> StatusGetStr(
137     const absl::Status& status, StatusStrProperty key);
138 
139 /// Sets the time property to the status
140 void StatusSetTime(absl::Status* status, StatusTimeProperty key,
141                    absl::Time time);
142 
143 /// Gets the time property from the status
144 GRPC_MUST_USE_RESULT absl::optional<absl::Time> StatusGetTime(
145     const absl::Status& status, StatusTimeProperty key);
146 
147 /// Adds a child status to status
148 void StatusAddChild(absl::Status* status, absl::Status child);
149 
150 /// Returns all children status from a status
151 GRPC_MUST_USE_RESULT std::vector<absl::Status> StatusGetChildren(
152     absl::Status status);
153 
154 /// Returns a string representation from status
155 /// Error status will be like
156 ///   STATUS[:MESSAGE] [{PAYLOADS[, children:[CHILDREN-STATUS-LISTS]]}]
157 /// e.g.
158 ///   CANCELLATION:SampleMessage {errno:'2021', line:'54', children:[ABORTED]}
159 GRPC_MUST_USE_RESULT std::string StatusToString(const absl::Status& status);
160 
161 namespace internal {
162 
163 /// Builds a upb message, google_rpc_Status from a status
164 /// This is for internal implementation & test only
165 GRPC_MUST_USE_RESULT google_rpc_Status* StatusToProto(
166     const absl::Status& status, upb_Arena* arena);
167 
168 /// Builds a status from a upb message, google_rpc_Status
169 /// This is for internal implementation & test only
170 absl::Status StatusFromProto(google_rpc_Status* msg);
171 
172 /// Returns ptr that is allocated in the heap memory and the given status is
173 /// copied into. This ptr can be used to get Status later and should be
174 /// freed by StatusFreeHeapPtr. This can be 0 in case of OkStatus.
175 uintptr_t StatusAllocHeapPtr(absl::Status s);
176 
177 /// Frees the allocated status at heap ptr.
178 void StatusFreeHeapPtr(uintptr_t ptr);
179 
180 /// Get the status from a heap ptr.
181 absl::Status StatusGetFromHeapPtr(uintptr_t ptr);
182 
183 /// Move the status from a heap ptr. (GetFrom & FreeHeap)
184 absl::Status StatusMoveFromHeapPtr(uintptr_t ptr);
185 
186 }  // namespace internal
187 
188 }  // namespace grpc_core
189 
190 #endif  // GRPC_SRC_CORE_LIB_GPRPP_STATUS_HELPER_H
191