Home | History | Annotate | Download | only in iomgr
      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 
     22 #include <grpc/support/port_platform.h>
     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 
     32 #include "src/core/lib/debug/trace.h"
     33 
     34 /// Opaque representation of an error.
     35 /// See https://github.com/grpc/grpc/blob/master/doc/core/grpc-error.md for a
     36 /// full write up of this object.
     37 
     38 typedef struct grpc_error grpc_error;
     39 
     40 extern grpc_core::DebugOnlyTraceFlag grpc_trace_error_refcount;
     41 
     42 typedef enum {
     43   /// 'errno' from the operating system
     44   GRPC_ERROR_INT_ERRNO,
     45   /// __LINE__ from the call site creating the error
     46   GRPC_ERROR_INT_FILE_LINE,
     47   /// stream identifier: for errors that are associated with an individual
     48   /// wire stream
     49   GRPC_ERROR_INT_STREAM_ID,
     50   /// grpc status code representing this error
     51   GRPC_ERROR_INT_GRPC_STATUS,
     52   /// offset into some binary blob (usually represented by
     53   /// GRPC_ERROR_STR_RAW_BYTES) where the error occurred
     54   GRPC_ERROR_INT_OFFSET,
     55   /// context sensitive index associated with the error
     56   GRPC_ERROR_INT_INDEX,
     57   /// context sensitive size associated with the error
     58   GRPC_ERROR_INT_SIZE,
     59   /// http2 error code associated with the error (see the HTTP2 RFC)
     60   GRPC_ERROR_INT_HTTP2_ERROR,
     61   /// TSI status code associated with the error
     62   GRPC_ERROR_INT_TSI_CODE,
     63   /// grpc_security_status associated with the error
     64   GRPC_ERROR_INT_SECURITY_STATUS,
     65   /// WSAGetLastError() reported when this error occurred
     66   GRPC_ERROR_INT_WSA_ERROR,
     67   /// File descriptor associated with this error
     68   GRPC_ERROR_INT_FD,
     69   /// HTTP status (i.e. 404)
     70   GRPC_ERROR_INT_HTTP_STATUS,
     71   /// context sensitive limit associated with the error
     72   GRPC_ERROR_INT_LIMIT,
     73   /// chttp2: did the error occur while a write was in progress
     74   GRPC_ERROR_INT_OCCURRED_DURING_WRITE,
     75 
     76   /// Must always be last
     77   GRPC_ERROR_INT_MAX,
     78 } grpc_error_ints;
     79 
     80 typedef enum {
     81   /// top-level textual description of this error
     82   GRPC_ERROR_STR_DESCRIPTION,
     83   /// source file in which this error occurred
     84   GRPC_ERROR_STR_FILE,
     85   /// operating system description of this error
     86   GRPC_ERROR_STR_OS_ERROR,
     87   /// syscall that generated this error
     88   GRPC_ERROR_STR_SYSCALL,
     89   /// peer that we were trying to communicate when this error occurred
     90   GRPC_ERROR_STR_TARGET_ADDRESS,
     91   /// grpc status message associated with this error
     92   GRPC_ERROR_STR_GRPC_MESSAGE,
     93   /// hex dump (or similar) with the data that generated this error
     94   GRPC_ERROR_STR_RAW_BYTES,
     95   /// tsi error string associated with this error
     96   GRPC_ERROR_STR_TSI_ERROR,
     97   /// filename that we were trying to read/write when this error occurred
     98   GRPC_ERROR_STR_FILENAME,
     99   /// which data was queued for writing when the error occurred
    100   GRPC_ERROR_STR_QUEUED_BUFFERS,
    101   /// key associated with the error
    102   GRPC_ERROR_STR_KEY,
    103   /// value associated with the error
    104   GRPC_ERROR_STR_VALUE,
    105 
    106   /// Must always be last
    107   GRPC_ERROR_STR_MAX,
    108 } grpc_error_strs;
    109 
    110 typedef enum {
    111   /// timestamp of error creation
    112   GRPC_ERROR_TIME_CREATED,
    113 
    114   /// Must always be last
    115   GRPC_ERROR_TIME_MAX,
    116 } grpc_error_times;
    117 
    118 /// The following "special" errors can be propagated without allocating memory.
    119 /// They are always even so that other code (particularly combiner locks,
    120 /// polling engines) can safely use the lower bit for themselves.
    121 
    122 #define GRPC_ERROR_NONE ((grpc_error*)NULL)
    123 #define GRPC_ERROR_OOM ((grpc_error*)2)
    124 #define GRPC_ERROR_CANCELLED ((grpc_error*)4)
    125 
    126 // debug only toggles that allow for a sanity to check that ensures we will
    127 // never create any errors in the per-RPC hotpath.
    128 void grpc_disable_error_creation();
    129 void grpc_enable_error_creation();
    130 
    131 const char* grpc_error_string(grpc_error* error);
    132 
    133 /// Create an error - but use GRPC_ERROR_CREATE instead
    134 grpc_error* grpc_error_create(const char* file, int line, grpc_slice desc,
    135                               grpc_error** referencing, size_t num_referencing);
    136 /// Create an error (this is the preferred way of generating an error that is
    137 ///   not due to a system call - for system calls, use GRPC_OS_ERROR or
    138 ///   GRPC_WSA_ERROR as appropriate)
    139 /// \a referencing is an array of num_referencing elements indicating one or
    140 /// more errors that are believed to have contributed to this one
    141 /// err = grpc_error_create(x, y, z, r, nr) is equivalent to:
    142 ///   err = grpc_error_create(x, y, z, NULL, 0);
    143 ///   for (i=0; i<nr; i++) err = grpc_error_add_child(err, r[i]);
    144 #define GRPC_ERROR_CREATE_FROM_STATIC_STRING(desc)                           \
    145   grpc_error_create(__FILE__, __LINE__, grpc_slice_from_static_string(desc), \
    146                     NULL, 0)
    147 #define GRPC_ERROR_CREATE_FROM_COPIED_STRING(desc)                           \
    148   grpc_error_create(__FILE__, __LINE__, grpc_slice_from_copied_string(desc), \
    149                     NULL, 0)
    150 
    151 // Create an error that references some other errors. This function adds a
    152 // reference to each error in errs - it does not consume an existing reference
    153 #define GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(desc, errs, count)  \
    154   grpc_error_create(__FILE__, __LINE__, grpc_slice_from_static_string(desc), \
    155                     errs, count)
    156 #define GRPC_ERROR_CREATE_REFERENCING_FROM_COPIED_STRING(desc, errs, count)  \
    157   grpc_error_create(__FILE__, __LINE__, grpc_slice_from_copied_string(desc), \
    158                     errs, count)
    159 
    160 #ifndef NDEBUG
    161 grpc_error* grpc_error_ref(grpc_error* err, const char* file, int line);
    162 void grpc_error_unref(grpc_error* err, const char* file, int line);
    163 #define GRPC_ERROR_REF(err) grpc_error_ref(err, __FILE__, __LINE__)
    164 #define GRPC_ERROR_UNREF(err) grpc_error_unref(err, __FILE__, __LINE__)
    165 #else
    166 grpc_error* grpc_error_ref(grpc_error* err);
    167 void grpc_error_unref(grpc_error* err);
    168 #define GRPC_ERROR_REF(err) grpc_error_ref(err)
    169 #define GRPC_ERROR_UNREF(err) grpc_error_unref(err)
    170 #endif
    171 
    172 grpc_error* grpc_error_set_int(grpc_error* src, grpc_error_ints which,
    173                                intptr_t value) GRPC_MUST_USE_RESULT;
    174 bool grpc_error_get_int(grpc_error* error, grpc_error_ints which, intptr_t* p);
    175 /// This call takes ownership of the slice; the error is responsible for
    176 /// eventually unref-ing it.
    177 grpc_error* grpc_error_set_str(grpc_error* src, grpc_error_strs which,
    178                                grpc_slice str) GRPC_MUST_USE_RESULT;
    179 /// Returns false if the specified string is not set.
    180 /// Caller does NOT own the slice.
    181 bool grpc_error_get_str(grpc_error* error, grpc_error_strs which,
    182                         grpc_slice* s);
    183 
    184 /// Add a child error: an error that is believed to have contributed to this
    185 /// error occurring. Allows root causing high level errors from lower level
    186 /// errors that contributed to them. The src error takes ownership of the
    187 /// child error.
    188 ///
    189 /// Edge Conditions -
    190 /// 1) If either of \a src or \a child is GRPC_ERROR_NONE, returns a reference
    191 /// to the other argument. 2) If both \a src and \a child are GRPC_ERROR_NONE,
    192 /// returns GRPC_ERROR_NONE. 3) If \a src and \a child point to the same error,
    193 /// returns a single reference. (Note that, 2 references should have been
    194 /// received to the error in this case.)
    195 grpc_error* grpc_error_add_child(grpc_error* src,
    196                                  grpc_error* child) GRPC_MUST_USE_RESULT;
    197 
    198 grpc_error* grpc_os_error(const char* file, int line, int err,
    199                           const char* call_name) GRPC_MUST_USE_RESULT;
    200 
    201 inline grpc_error* grpc_assert_never_ok(grpc_error* error) {
    202   GPR_ASSERT(error != GRPC_ERROR_NONE);
    203   return error;
    204 }
    205 
    206 /// create an error associated with errno!=0 (an 'operating system' error)
    207 #define GRPC_OS_ERROR(err, call_name) \
    208   grpc_assert_never_ok(grpc_os_error(__FILE__, __LINE__, err, call_name))
    209 grpc_error* grpc_wsa_error(const char* file, int line, int err,
    210                            const char* call_name) GRPC_MUST_USE_RESULT;
    211 /// windows only: create an error associated with WSAGetLastError()!=0
    212 #define GRPC_WSA_ERROR(err, call_name) \
    213   grpc_wsa_error(__FILE__, __LINE__, err, call_name)
    214 
    215 bool grpc_log_if_error(const char* what, grpc_error* error, const char* file,
    216                        int line);
    217 #define GRPC_LOG_IF_ERROR(what, error) \
    218   grpc_log_if_error((what), (error), __FILE__, __LINE__)
    219 
    220 #endif /* GRPC_CORE_LIB_IOMGR_ERROR_H */
    221