3 * Copyright 2016 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef GRPC_CORE_LIB_IOMGR_ERROR_H
20 #define GRPC_CORE_LIB_IOMGR_ERROR_H
22 #include <grpc/support/port_platform.h>
27 #include <grpc/slice.h>
28 #include <grpc/status.h>
29 #include <grpc/support/log.h>
30 #include <grpc/support/time.h>
32 #include "src/core/lib/debug/trace.h"
33 #include "src/core/lib/gprpp/inlined_vector.h"
35 /// Opaque representation of an error.
36 /// See https://github.com/grpc/grpc/blob/master/doc/core/grpc-error.md for a
37 /// full write up of this object.
39 typedef struct grpc_error grpc_error;
41 extern grpc_core::DebugOnlyTraceFlag grpc_trace_error_refcount;
44 /// 'errno' from the operating system
46 /// __LINE__ from the call site creating the error
47 GRPC_ERROR_INT_FILE_LINE,
48 /// stream identifier: for errors that are associated with an individual
50 GRPC_ERROR_INT_STREAM_ID,
51 /// grpc status code representing this error
52 GRPC_ERROR_INT_GRPC_STATUS,
53 /// offset into some binary blob (usually represented by
54 /// GRPC_ERROR_STR_RAW_BYTES) where the error occurred
55 GRPC_ERROR_INT_OFFSET,
56 /// context sensitive index associated with the error
58 /// context sensitive size associated with the error
60 /// http2 error code associated with the error (see the HTTP2 RFC)
61 GRPC_ERROR_INT_HTTP2_ERROR,
62 /// TSI status code associated with the error
63 GRPC_ERROR_INT_TSI_CODE,
64 /// grpc_security_status associated with the error
65 GRPC_ERROR_INT_SECURITY_STATUS,
66 /// WSAGetLastError() reported when this error occurred
67 GRPC_ERROR_INT_WSA_ERROR,
68 /// File descriptor associated with this error
70 /// HTTP status (i.e. 404)
71 GRPC_ERROR_INT_HTTP_STATUS,
72 /// context sensitive limit associated with the error
74 /// chttp2: did the error occur while a write was in progress
75 GRPC_ERROR_INT_OCCURRED_DURING_WRITE,
76 /// channel connectivity state associated with the error
77 GRPC_ERROR_INT_CHANNEL_CONNECTIVITY_STATE,
79 /// Must always be last
84 /// top-level textual description of this error
85 GRPC_ERROR_STR_DESCRIPTION,
86 /// source file in which this error occurred
88 /// operating system description of this error
89 GRPC_ERROR_STR_OS_ERROR,
90 /// syscall that generated this error
91 GRPC_ERROR_STR_SYSCALL,
92 /// peer that we were trying to communicate when this error occurred
93 GRPC_ERROR_STR_TARGET_ADDRESS,
94 /// grpc status message associated with this error
95 GRPC_ERROR_STR_GRPC_MESSAGE,
96 /// hex dump (or similar) with the data that generated this error
97 GRPC_ERROR_STR_RAW_BYTES,
98 /// tsi error string associated with this error
99 GRPC_ERROR_STR_TSI_ERROR,
100 /// filename that we were trying to read/write when this error occurred
101 GRPC_ERROR_STR_FILENAME,
102 /// which data was queued for writing when the error occurred
103 GRPC_ERROR_STR_QUEUED_BUFFERS,
104 /// key associated with the error
106 /// value associated with the error
107 GRPC_ERROR_STR_VALUE,
109 /// Must always be last
114 /// timestamp of error creation
115 GRPC_ERROR_TIME_CREATED,
117 /// Must always be last
121 /// The following "special" errors can be propagated without allocating memory.
122 /// They are always even so that other code (particularly combiner locks,
123 /// polling engines) can safely use the lower bit for themselves.
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
132 inline bool grpc_error_is_special(struct grpc_error* err) {
133 return err <= GRPC_ERROR_SPECIAL_MAX;
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.
138 void grpc_disable_error_creation();
139 void grpc_enable_error_creation();
141 const char* grpc_error_string(grpc_error* error);
143 /// Create an error - but use GRPC_ERROR_CREATE instead
144 grpc_error* grpc_error_create(const char* file, int line,
145 const grpc_slice& desc, grpc_error** referencing,
146 size_t num_referencing);
147 /// Create an error (this is the preferred way of generating an error that is
148 /// not due to a system call - for system calls, use GRPC_OS_ERROR or
149 /// GRPC_WSA_ERROR as appropriate)
150 /// \a referencing is an array of num_referencing elements indicating one or
151 /// more errors that are believed to have contributed to this one
152 /// err = grpc_error_create(x, y, z, r, nr) is equivalent to:
153 /// err = grpc_error_create(x, y, z, NULL, 0);
154 /// for (i=0; i<nr; i++) err = grpc_error_add_child(err, r[i]);
155 #define GRPC_ERROR_CREATE_FROM_STATIC_STRING(desc) \
156 grpc_error_create(__FILE__, __LINE__, grpc_slice_from_static_string(desc), \
158 #define GRPC_ERROR_CREATE_FROM_COPIED_STRING(desc) \
159 grpc_error_create(__FILE__, __LINE__, grpc_slice_from_copied_string(desc), \
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), \
167 #define GRPC_ERROR_CREATE_REFERENCING_FROM_COPIED_STRING(desc, errs, count) \
168 grpc_error_create(__FILE__, __LINE__, grpc_slice_from_copied_string(desc), \
171 #define GRPC_ERROR_CREATE_FROM_VECTOR(desc, error_list) \
172 grpc_error_create_from_vector(__FILE__, __LINE__, desc, error_list)
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);
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);
185 #define GRPC_ERROR_REF(err) grpc_error_ref(err, __FILE__, __LINE__)
186 #define GRPC_ERROR_UNREF(err) grpc_error_unref(err, __FILE__, __LINE__)
188 grpc_error* grpc_error_do_ref(grpc_error* err);
189 void grpc_error_do_unref(grpc_error* err);
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);
194 inline void grpc_error_unref(grpc_error* err) {
195 if (grpc_error_is_special(err)) return;
196 grpc_error_do_unref(err);
198 #define GRPC_ERROR_REF(err) grpc_error_ref(err)
199 #define GRPC_ERROR_UNREF(err) grpc_error_unref(err)
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.
205 static grpc_error* grpc_error_create_from_vector(
206 const char* file, int line, const char* desc,
207 grpc_core::InlinedVector<grpc_error*, N>* error_list) {
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]);
221 grpc_error* grpc_error_set_int(grpc_error* src, grpc_error_ints which,
222 intptr_t value) GRPC_MUST_USE_RESULT;
223 /// It is an error to pass nullptr as `p`. Caller should allocate a dummy
224 /// intptr_t for `p`, even if the value of `p` is not used.
225 bool grpc_error_get_int(grpc_error* error, grpc_error_ints which, intptr_t* p);
226 /// This call takes ownership of the slice; the error is responsible for
227 /// eventually unref-ing it.
228 grpc_error* grpc_error_set_str(grpc_error* src, grpc_error_strs which,
229 const grpc_slice& str) GRPC_MUST_USE_RESULT;
230 /// Returns false if the specified string is not set.
231 /// Caller does NOT own the slice.
232 bool grpc_error_get_str(grpc_error* error, grpc_error_strs which,
235 /// Add a child error: an error that is believed to have contributed to this
236 /// error occurring. Allows root causing high level errors from lower level
237 /// errors that contributed to them. The src error takes ownership of the
240 /// Edge Conditions -
241 /// 1) If either of \a src or \a child is GRPC_ERROR_NONE, returns a reference
242 /// to the other argument. 2) If both \a src and \a child are GRPC_ERROR_NONE,
243 /// returns GRPC_ERROR_NONE. 3) If \a src and \a child point to the same error,
244 /// returns a single reference. (Note that, 2 references should have been
245 /// received to the error in this case.)
246 grpc_error* grpc_error_add_child(grpc_error* src,
247 grpc_error* child) GRPC_MUST_USE_RESULT;
249 grpc_error* grpc_os_error(const char* file, int line, int err,
250 const char* call_name) GRPC_MUST_USE_RESULT;
252 inline grpc_error* grpc_assert_never_ok(grpc_error* error) {
253 GPR_ASSERT(error != GRPC_ERROR_NONE);
257 /// create an error associated with errno!=0 (an 'operating system' error)
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;
262 /// windows only: create an error associated with WSAGetLastError()!=0
263 #define GRPC_WSA_ERROR(err, call_name) \
264 grpc_wsa_error(__FILE__, __LINE__, err, call_name)
266 bool grpc_log_error(const char* what, grpc_error* error, const char* file,
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);
274 #define GRPC_LOG_IF_ERROR(what, error) \
275 (grpc_log_if_error((what), (error), __FILE__, __LINE__))
277 #endif /* GRPC_CORE_LIB_IOMGR_ERROR_H */