GRPC C++  1.26.0
error.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2016 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_IOMGR_ERROR_H
20 #define GRPC_CORE_LIB_IOMGR_ERROR_H
21 
23 
24 #include <inttypes.h>
25 #include <stdbool.h>
26 
27 #include <grpc/slice.h>
28 #include <grpc/status.h>
29 #include <grpc/support/log.h>
30 #include <grpc/support/time.h>
31 
34 
38 
39 typedef struct grpc_error grpc_error;
40 
42 
43 typedef enum {
78 
82 
83 typedef enum {
108 
112 
113 typedef enum {
116 
120 
124 
125 #define GRPC_ERROR_NONE ((grpc_error*)NULL)
126 #define GRPC_ERROR_RESERVED_1 ((grpc_error*)1)
127 #define GRPC_ERROR_OOM ((grpc_error*)2)
128 #define GRPC_ERROR_RESERVED_2 ((grpc_error*)3)
129 #define GRPC_ERROR_CANCELLED ((grpc_error*)4)
130 #define GRPC_ERROR_SPECIAL_MAX GRPC_ERROR_CANCELLED
131 
132 inline bool grpc_error_is_special(struct grpc_error* err) {
133  return err <= GRPC_ERROR_SPECIAL_MAX;
134 }
135 
136 // debug only toggles that allow for a sanity to check that ensures we will
137 // never create any errors in the per-RPC hotpath.
140 
141 const char* grpc_error_string(grpc_error* error);
142 
144 grpc_error* grpc_error_create(const char* file, int line,
145  const grpc_slice& desc, grpc_error** referencing,
146  size_t num_referencing);
155 #define GRPC_ERROR_CREATE_FROM_STATIC_STRING(desc) \
156  grpc_error_create(__FILE__, __LINE__, grpc_slice_from_static_string(desc), \
157  NULL, 0)
158 #define GRPC_ERROR_CREATE_FROM_COPIED_STRING(desc) \
159  grpc_error_create(__FILE__, __LINE__, grpc_slice_from_copied_string(desc), \
160  NULL, 0)
161 
162 // Create an error that references some other errors. This function adds a
163 // reference to each error in errs - it does not consume an existing reference
164 #define GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(desc, errs, count) \
165  grpc_error_create(__FILE__, __LINE__, grpc_slice_from_static_string(desc), \
166  errs, count)
167 #define GRPC_ERROR_CREATE_REFERENCING_FROM_COPIED_STRING(desc, errs, count) \
168  grpc_error_create(__FILE__, __LINE__, grpc_slice_from_copied_string(desc), \
169  errs, count)
170 
171 #define GRPC_ERROR_CREATE_FROM_VECTOR(desc, error_list) \
172  grpc_error_create_from_vector(__FILE__, __LINE__, desc, error_list)
173 
174 #ifndef NDEBUG
175 grpc_error* grpc_error_do_ref(grpc_error* err, const char* file, int line);
176 void grpc_error_do_unref(grpc_error* err, const char* file, int line);
177 inline grpc_error* grpc_error_ref(grpc_error* err, const char* file, int line) {
178  if (grpc_error_is_special(err)) return err;
179  return grpc_error_do_ref(err, file, line);
180 }
181 inline void grpc_error_unref(grpc_error* err, const char* file, int line) {
182  if (grpc_error_is_special(err)) return;
183  grpc_error_do_unref(err, file, line);
184 }
185 #define GRPC_ERROR_REF(err) grpc_error_ref(err, __FILE__, __LINE__)
186 #define GRPC_ERROR_UNREF(err) grpc_error_unref(err, __FILE__, __LINE__)
187 #else
190 inline grpc_error* grpc_error_ref(grpc_error* err) {
191  if (grpc_error_is_special(err)) return err;
192  return grpc_error_do_ref(err);
193 }
194 inline void grpc_error_unref(grpc_error* err) {
195  if (grpc_error_is_special(err)) return;
196  grpc_error_do_unref(err);
197 }
198 #define GRPC_ERROR_REF(err) grpc_error_ref(err)
199 #define GRPC_ERROR_UNREF(err) grpc_error_unref(err)
200 #endif
201 
202 // Consumes all the errors in the vector and forms a referencing error from
203 // them. If the vector is empty, return GRPC_ERROR_NONE.
204 template <size_t N>
205 static grpc_error* grpc_error_create_from_vector(
206  const char* file, int line, const char* desc,
208  grpc_error* error = GRPC_ERROR_NONE;
209  if (error_list->size() != 0) {
210  error = grpc_error_create(file, line, grpc_slice_from_static_string(desc),
211  error_list->data(), error_list->size());
212  // Remove refs to all errors in error_list.
213  for (size_t i = 0; i < error_list->size(); i++) {
214  GRPC_ERROR_UNREF((*error_list)[i]);
215  }
216  error_list->clear();
217  }
218  return error;
219 }
220 
222  intptr_t value) GRPC_MUST_USE_RESULT;
225 bool grpc_error_get_int(grpc_error* error, grpc_error_ints which, intptr_t* p);
229  const grpc_slice& str) GRPC_MUST_USE_RESULT;
233  grpc_slice* s);
234 
248 
249 grpc_error* grpc_os_error(const char* file, int line, int err,
250  const char* call_name) GRPC_MUST_USE_RESULT;
251 
253  GPR_ASSERT(error != GRPC_ERROR_NONE);
254  return error;
255 }
256 
258 #define GRPC_OS_ERROR(err, call_name) \
259  grpc_assert_never_ok(grpc_os_error(__FILE__, __LINE__, err, call_name))
260 grpc_error* grpc_wsa_error(const char* file, int line, int err,
261  const char* call_name) GRPC_MUST_USE_RESULT;
263 #define GRPC_WSA_ERROR(err, call_name) \
264  grpc_wsa_error(__FILE__, __LINE__, err, call_name)
265 
266 bool grpc_log_error(const char* what, grpc_error* error, const char* file,
267  int line);
268 inline bool grpc_log_if_error(const char* what, grpc_error* error,
269  const char* file, int line) {
270  return error == GRPC_ERROR_NONE ? true
271  : grpc_log_error(what, error, file, line);
272 }
273 
274 #define GRPC_LOG_IF_ERROR(what, error) \
275  (grpc_log_if_error((what), (error), __FILE__, __LINE__))
276 
277 #endif /* GRPC_CORE_LIB_IOMGR_ERROR_H */
Definition: inlined_vector.h:60
T * data()
Definition: inlined_vector.h:93
size_t size() const
Definition: inlined_vector.h:165
void clear()
Definition: inlined_vector.h:170
Definition: trace.h:61
bool grpc_log_error(const char *what, grpc_error *error, const char *file, int line)
grpc_error_times
Definition: error.h:113
@ GRPC_ERROR_TIME_MAX
Must always be last.
Definition: error.h:118
@ GRPC_ERROR_TIME_CREATED
timestamp of error creation
Definition: error.h:115
grpc_error * grpc_error_set_int(grpc_error *src, grpc_error_ints which, intptr_t value) GRPC_MUST_USE_RESULT
grpc_error * grpc_error_set_str(grpc_error *src, grpc_error_strs which, const grpc_slice &str) GRPC_MUST_USE_RESULT
This call takes ownership of the slice; the error is responsible for eventually unref-ing it.
grpc_error_ints
Definition: error.h:43
@ GRPC_ERROR_INT_HTTP_STATUS
HTTP status (i.e. 404)
Definition: error.h:71
@ GRPC_ERROR_INT_INDEX
context sensitive index associated with the error
Definition: error.h:57
@ GRPC_ERROR_INT_FD
File descriptor associated with this error.
Definition: error.h:69
@ GRPC_ERROR_INT_STREAM_ID
stream identifier: for errors that are associated with an individual wire stream
Definition: error.h:50
@ GRPC_ERROR_INT_HTTP2_ERROR
http2 error code associated with the error (see the HTTP2 RFC)
Definition: error.h:61
@ GRPC_ERROR_INT_CHANNEL_CONNECTIVITY_STATE
channel connectivity state associated with the error
Definition: error.h:77
@ GRPC_ERROR_INT_WSA_ERROR
WSAGetLastError() reported when this error occurred.
Definition: error.h:67
@ GRPC_ERROR_INT_MAX
Must always be last.
Definition: error.h:80
@ GRPC_ERROR_INT_GRPC_STATUS
grpc status code representing this error
Definition: error.h:52
@ GRPC_ERROR_INT_FILE_LINE
LINE from the call site creating the error
Definition: error.h:47
@ GRPC_ERROR_INT_LIMIT
context sensitive limit associated with the error
Definition: error.h:73
@ GRPC_ERROR_INT_TSI_CODE
TSI status code associated with the error.
Definition: error.h:63
@ GRPC_ERROR_INT_SECURITY_STATUS
grpc_security_status associated with the error
Definition: error.h:65
@ GRPC_ERROR_INT_SIZE
context sensitive size associated with the error
Definition: error.h:59
@ GRPC_ERROR_INT_OFFSET
offset into some binary blob (usually represented by GRPC_ERROR_STR_RAW_BYTES) where the error occurr...
Definition: error.h:55
@ GRPC_ERROR_INT_OCCURRED_DURING_WRITE
chttp2: did the error occur while a write was in progress
Definition: error.h:75
@ GRPC_ERROR_INT_ERRNO
'errno' from the operating system
Definition: error.h:45
void grpc_error_do_unref(grpc_error *err, const char *file, int line)
grpc_error * grpc_error_add_child(grpc_error *src, grpc_error *child) GRPC_MUST_USE_RESULT
Add a child error: an error that is believed to have contributed to this error occurring.
grpc_error * grpc_os_error(const char *file, int line, int err, const char *call_name) GRPC_MUST_USE_RESULT
#define GRPC_ERROR_NONE
The following "special" errors can be propagated without allocating memory.
Definition: error.h:125
const char * grpc_error_string(grpc_error *error)
bool grpc_error_is_special(struct grpc_error *err)
Definition: error.h:132
grpc_error * grpc_assert_never_ok(grpc_error *error)
Definition: error.h:252
grpc_error * grpc_error_do_ref(grpc_error *err, const char *file, int line)
grpc_error * grpc_error_create(const char *file, int line, const grpc_slice &desc, grpc_error **referencing, size_t num_referencing)
Create an error - but use GRPC_ERROR_CREATE instead.
void grpc_enable_error_creation()
bool grpc_error_get_int(grpc_error *error, grpc_error_ints which, intptr_t *p)
It is an error to pass nullptr as p.
grpc_error_strs
Definition: error.h:83
@ GRPC_ERROR_STR_TSI_ERROR
tsi error string associated with this error
Definition: error.h:99
@ GRPC_ERROR_STR_DESCRIPTION
top-level textual description of this error
Definition: error.h:85
@ GRPC_ERROR_STR_RAW_BYTES
hex dump (or similar) with the data that generated this error
Definition: error.h:97
@ GRPC_ERROR_STR_TARGET_ADDRESS
peer that we were trying to communicate when this error occurred
Definition: error.h:93
@ GRPC_ERROR_STR_GRPC_MESSAGE
grpc status message associated with this error
Definition: error.h:95
@ GRPC_ERROR_STR_OS_ERROR
operating system description of this error
Definition: error.h:89
@ GRPC_ERROR_STR_FILENAME
filename that we were trying to read/write when this error occurred
Definition: error.h:101
@ GRPC_ERROR_STR_MAX
Must always be last.
Definition: error.h:110
@ GRPC_ERROR_STR_FILE
source file in which this error occurred
Definition: error.h:87
@ GRPC_ERROR_STR_KEY
key associated with the error
Definition: error.h:105
@ GRPC_ERROR_STR_SYSCALL
syscall that generated this error
Definition: error.h:91
@ GRPC_ERROR_STR_VALUE
value associated with the error
Definition: error.h:107
@ GRPC_ERROR_STR_QUEUED_BUFFERS
which data was queued for writing when the error occurred
Definition: error.h:103
bool grpc_error_get_str(grpc_error *error, grpc_error_strs which, grpc_slice *s)
Returns false if the specified string is not set.
#define GRPC_ERROR_UNREF(err)
Definition: error.h:186
grpc_core::DebugOnlyTraceFlag grpc_trace_error_refcount
void grpc_error_unref(grpc_error *err, const char *file, int line)
Definition: error.h:181
bool grpc_log_if_error(const char *what, grpc_error *error, const char *file, int line)
Definition: error.h:268
#define GRPC_ERROR_SPECIAL_MAX
Definition: error.h:130
void grpc_disable_error_creation()
grpc_error * grpc_wsa_error(const char *file, int line, int err, const char *call_name) GRPC_MUST_USE_RESULT
grpc_error * grpc_error_ref(grpc_error *err, const char *file, int line)
Definition: error.h:177
GPRAPI grpc_slice grpc_slice_from_static_string(const char *source)
Create a slice pointing to constant memory.
#define GPR_ASSERT(x)
abort() the process if x is zero, having written a line to the log.
Definition: log.h:94
#define GRPC_MUST_USE_RESULT
Definition: port_platform.h:570
Definition: error_internal.h:39
A grpc_slice s, if initialized, represents the byte range s.bytes[0..s.length-1].
Definition: slice.h:60