blob: b3629486918f9cd72e1c1df1f5c1c04491e2d9f7 [file] [log] [blame]
Craig Tiller27f59af2016-04-28 14:19:48 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2016 gRPC authors.
Craig Tiller27f59af2016-04-28 14:19:48 -07004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
Craig Tiller27f59af2016-04-28 14:19:48 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller27f59af2016-04-28 14:19:48 -070010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
Craig Tiller27f59af2016-04-28 14:19:48 -070016 *
17 */
18
19#ifndef GRPC_CORE_LIB_IOMGR_ERROR_H
20#define GRPC_CORE_LIB_IOMGR_ERROR_H
21
Craig Tiller4f1d0f32016-05-06 17:12:37 -070022#include <stdbool.h>
Craig Tiller27f59af2016-04-28 14:19:48 -070023#include <stdint.h>
24
ncteisenbbb38012017-03-10 14:58:43 -080025#include <grpc/slice.h>
Mark D. Roth1e35b692016-09-02 13:44:32 -070026#include <grpc/status.h>
Craig Tillerc027e772016-05-03 16:27:00 -070027#include <grpc/support/time.h>
28
ncteisenf0c46e32017-06-08 15:57:09 -070029#include "src/core/lib/debug/trace.h"
30
Mark D. Roth757e84e2016-10-06 13:07:53 -070031#ifdef __cplusplus
32extern "C" {
33#endif
34
Craig Tillera65475c2016-06-02 15:57:44 -070035/// Opaque representation of an error.
ncteisen031cfeb2017-03-21 19:07:17 -070036/// See https://github.com/grpc/grpc/blob/master/doc/core/grpc-error.md for a
37/// full write up of this object.
38
Craig Tiller27f59af2016-04-28 14:19:48 -070039typedef struct grpc_error grpc_error;
40
ncteisena1354852017-06-08 16:25:53 -070041#ifndef NDEBUG
ncteisenf0c46e32017-06-08 15:57:09 -070042extern grpc_tracer_flag grpc_trace_error_refcount;
ncteisena1354852017-06-08 16:25:53 -070043#endif
ncteisenf0c46e32017-06-08 15:57:09 -070044
Craig Tiller27f59af2016-04-28 14:19:48 -070045typedef enum {
Craig Tillera65475c2016-06-02 15:57:44 -070046 /// 'errno' from the operating system
Craig Tillerc027e772016-05-03 16:27:00 -070047 GRPC_ERROR_INT_ERRNO,
Craig Tillera65475c2016-06-02 15:57:44 -070048 /// __LINE__ from the call site creating the error
Craig Tillerc027e772016-05-03 16:27:00 -070049 GRPC_ERROR_INT_FILE_LINE,
Craig Tillera65475c2016-06-02 15:57:44 -070050 /// stream identifier: for errors that are associated with an individual
51 /// wire stream
Craig Tiller781bab532016-05-05 08:15:28 -070052 GRPC_ERROR_INT_STREAM_ID,
Craig Tillera65475c2016-06-02 15:57:44 -070053 /// grpc status code representing this error
Craig Tiller781bab532016-05-05 08:15:28 -070054 GRPC_ERROR_INT_GRPC_STATUS,
Craig Tillera65475c2016-06-02 15:57:44 -070055 /// offset into some binary blob (usually represented by
56 /// GRPC_ERROR_STR_RAW_BYTES) where the error occurred
Craig Tiller781bab532016-05-05 08:15:28 -070057 GRPC_ERROR_INT_OFFSET,
Craig Tillera65475c2016-06-02 15:57:44 -070058 /// context sensitive index associated with the error
Craig Tiller781bab532016-05-05 08:15:28 -070059 GRPC_ERROR_INT_INDEX,
Craig Tillera65475c2016-06-02 15:57:44 -070060 /// context sensitive size associated with the error
Craig Tiller781bab532016-05-05 08:15:28 -070061 GRPC_ERROR_INT_SIZE,
Craig Tillera65475c2016-06-02 15:57:44 -070062 /// http2 error code associated with the error (see the HTTP2 RFC)
Craig Tiller94e15762016-05-05 08:44:36 -070063 GRPC_ERROR_INT_HTTP2_ERROR,
Craig Tillera65475c2016-06-02 15:57:44 -070064 /// TSI status code associated with the error
Craig Tiller804ff712016-05-05 16:25:40 -070065 GRPC_ERROR_INT_TSI_CODE,
Craig Tillera65475c2016-06-02 15:57:44 -070066 /// grpc_security_status associated with the error
Craig Tiller804ff712016-05-05 16:25:40 -070067 GRPC_ERROR_INT_SECURITY_STATUS,
Craig Tillera65475c2016-06-02 15:57:44 -070068 /// WSAGetLastError() reported when this error occurred
Craig Tillera41ac572016-05-17 16:08:17 -070069 GRPC_ERROR_INT_WSA_ERROR,
Craig Tillera65475c2016-06-02 15:57:44 -070070 /// File descriptor associated with this error
Craig Tiller80384bd2016-05-06 16:12:31 -070071 GRPC_ERROR_INT_FD,
Craig Tiller34b11df2016-06-09 17:17:15 -070072 /// HTTP status (i.e. 404)
73 GRPC_ERROR_INT_HTTP_STATUS,
Craig Tillerf0f70a82016-06-23 13:55:06 -070074 /// context sensitive limit associated with the error
75 GRPC_ERROR_INT_LIMIT,
Craig Tiller936f1ea2016-10-14 15:15:19 -070076 /// chttp2: did the error occur while a write was in progress
77 GRPC_ERROR_INT_OCCURRED_DURING_WRITE,
ncteisen55337bb2017-03-02 15:12:33 -080078
79 /// Must always be last
80 GRPC_ERROR_INT_MAX,
Craig Tiller27f59af2016-04-28 14:19:48 -070081} grpc_error_ints;
82
83typedef enum {
Craig Tillera65475c2016-06-02 15:57:44 -070084 /// top-level textual description of this error
Craig Tiller27f59af2016-04-28 14:19:48 -070085 GRPC_ERROR_STR_DESCRIPTION,
Craig Tillera65475c2016-06-02 15:57:44 -070086 /// source file in which this error occurred
Craig Tillerc027e772016-05-03 16:27:00 -070087 GRPC_ERROR_STR_FILE,
Craig Tillera65475c2016-06-02 15:57:44 -070088 /// operating system description of this error
Craig Tiller27f59af2016-04-28 14:19:48 -070089 GRPC_ERROR_STR_OS_ERROR,
Craig Tillera65475c2016-06-02 15:57:44 -070090 /// syscall that generated this error
Craig Tillerc027e772016-05-03 16:27:00 -070091 GRPC_ERROR_STR_SYSCALL,
Craig Tillera65475c2016-06-02 15:57:44 -070092 /// peer that we were trying to communicate when this error occurred
Craig Tillerc027e772016-05-03 16:27:00 -070093 GRPC_ERROR_STR_TARGET_ADDRESS,
Craig Tillera65475c2016-06-02 15:57:44 -070094 /// grpc status message associated with this error
Craig Tiller781bab532016-05-05 08:15:28 -070095 GRPC_ERROR_STR_GRPC_MESSAGE,
Craig Tillera65475c2016-06-02 15:57:44 -070096 /// hex dump (or similar) with the data that generated this error
Craig Tiller781bab532016-05-05 08:15:28 -070097 GRPC_ERROR_STR_RAW_BYTES,
Craig Tillera65475c2016-06-02 15:57:44 -070098 /// tsi error string associated with this error
Craig Tiller804ff712016-05-05 16:25:40 -070099 GRPC_ERROR_STR_TSI_ERROR,
Craig Tillera65475c2016-06-02 15:57:44 -0700100 /// filename that we were trying to read/write when this error occurred
Craig Tiller4727b9b2016-05-17 17:19:19 -0700101 GRPC_ERROR_STR_FILENAME,
Craig Tiller936f1ea2016-10-14 15:15:19 -0700102 /// which data was queued for writing when the error occurred
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800103 GRPC_ERROR_STR_QUEUED_BUFFERS,
104 /// key associated with the error
105 GRPC_ERROR_STR_KEY,
106 /// value associated with the error
107 GRPC_ERROR_STR_VALUE,
ncteisen55337bb2017-03-02 15:12:33 -0800108
109 /// Must always be last
110 GRPC_ERROR_STR_MAX,
Craig Tiller27f59af2016-04-28 14:19:48 -0700111} grpc_error_strs;
112
Craig Tillerc027e772016-05-03 16:27:00 -0700113typedef enum {
Craig Tillera65475c2016-06-02 15:57:44 -0700114 /// timestamp of error creation
Craig Tillerc027e772016-05-03 16:27:00 -0700115 GRPC_ERROR_TIME_CREATED,
ncteisen55337bb2017-03-02 15:12:33 -0800116
117 /// Must always be last
118 GRPC_ERROR_TIME_MAX,
Craig Tillerc027e772016-05-03 16:27:00 -0700119} grpc_error_times;
120
Craig Tiller86037cd02016-09-02 19:58:43 -0700121/// The following "special" errors can be propagated without allocating memory.
Sree Kuchibhotla2fc2b3e2017-02-14 10:05:14 -0800122/// They are always even so that other code (particularly combiner locks,
123/// polling engines) can safely use the lower bit for themselves.
Craig Tiller86037cd02016-09-02 19:58:43 -0700124
Craig Tiller27f59af2016-04-28 14:19:48 -0700125#define GRPC_ERROR_NONE ((grpc_error *)NULL)
Craig Tiller86037cd02016-09-02 19:58:43 -0700126#define GRPC_ERROR_OOM ((grpc_error *)2)
127#define GRPC_ERROR_CANCELLED ((grpc_error *)4)
Craig Tiller27f59af2016-04-28 14:19:48 -0700128
129const char *grpc_error_string(grpc_error *error);
Craig Tiller27f59af2016-04-28 14:19:48 -0700130
Craig Tillera65475c2016-06-02 15:57:44 -0700131/// Create an error - but use GRPC_ERROR_CREATE instead
Noah Eisenf88ee4f2017-04-17 18:14:39 -0700132grpc_error *grpc_error_create(const char *file, int line, grpc_slice desc,
Craig Tillerc027e772016-05-03 16:27:00 -0700133 grpc_error **referencing, size_t num_referencing);
Craig Tillera65475c2016-06-02 15:57:44 -0700134/// Create an error (this is the preferred way of generating an error that is
135/// not due to a system call - for system calls, use GRPC_OS_ERROR or
136/// GRPC_WSA_ERROR as appropriate)
137/// \a referencing is an array of num_referencing elements indicating one or
138/// more errors that are believed to have contributed to this one
139/// err = grpc_error_create(x, y, z, r, nr) is equivalent to:
140/// err = grpc_error_create(x, y, z, NULL, 0);
141/// for (i=0; i<nr; i++) err = grpc_error_add_child(err, r[i]);
Noah Eisenf88ee4f2017-04-17 18:14:39 -0700142#define GRPC_ERROR_CREATE_FROM_STATIC_STRING(desc) \
143 grpc_error_create(__FILE__, __LINE__, grpc_slice_from_static_string(desc), \
144 NULL, 0)
145#define GRPC_ERROR_CREATE_FROM_COPIED_STRING(desc) \
146 grpc_error_create(__FILE__, __LINE__, grpc_slice_from_copied_string(desc), \
147 NULL, 0)
ncteisenbbb38012017-03-10 14:58:43 -0800148
David Garcia Quintas32ec1332016-05-11 12:22:53 -0700149// Create an error that references some other errors. This function adds a
150// reference to each error in errs - it does not consume an existing reference
Noah Eisenf88ee4f2017-04-17 18:14:39 -0700151#define GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(desc, errs, count) \
152 grpc_error_create(__FILE__, __LINE__, grpc_slice_from_static_string(desc), \
153 errs, count)
154#define GRPC_ERROR_CREATE_REFERENCING_FROM_COPIED_STRING(desc, errs, count) \
155 grpc_error_create(__FILE__, __LINE__, grpc_slice_from_copied_string(desc), \
156 errs, count)
Craig Tillerf707d622016-05-06 14:26:12 -0700157
ncteisenf0c46e32017-06-08 15:57:09 -0700158#ifndef NDEBUG
ncteisend39010e2017-06-08 17:08:07 -0700159grpc_error *grpc_error_ref(grpc_error *err, const char *file, int line);
160void grpc_error_unref(grpc_error *err, const char *file, int line);
161#define GRPC_ERROR_REF(err) grpc_error_ref(err, __FILE__, __LINE__)
162#define GRPC_ERROR_UNREF(err) grpc_error_unref(err, __FILE__, __LINE__)
Craig Tillere0d6c572016-05-13 07:23:36 -0700163#else
164grpc_error *grpc_error_ref(grpc_error *err);
165void grpc_error_unref(grpc_error *err);
166#define GRPC_ERROR_REF(err) grpc_error_ref(err)
167#define GRPC_ERROR_UNREF(err) grpc_error_unref(err)
168#endif
Craig Tillerf707d622016-05-06 14:26:12 -0700169
Craig Tiller27f59af2016-04-28 14:19:48 -0700170grpc_error *grpc_error_set_int(grpc_error *src, grpc_error_ints which,
Craig Tillerf0f70a82016-06-23 13:55:06 -0700171 intptr_t value) GRPC_MUST_USE_RESULT;
Craig Tiller965eab32016-05-07 22:11:37 -0700172bool grpc_error_get_int(grpc_error *error, grpc_error_ints which, intptr_t *p);
ncteisen4b36a3d2017-03-13 19:08:06 -0700173grpc_error *grpc_error_set_str(grpc_error *src, grpc_error_strs which,
174 grpc_slice str) GRPC_MUST_USE_RESULT;
ncteisenbbb38012017-03-10 14:58:43 -0800175/// Returns false if the specified string is not set.
176/// Caller does NOT own the slice.
ncteisen4b36a3d2017-03-13 19:08:06 -0700177bool grpc_error_get_str(grpc_error *error, grpc_error_strs which,
178 grpc_slice *s);
Mark D. Roth1e35b692016-09-02 13:44:32 -0700179
Craig Tillera65475c2016-06-02 15:57:44 -0700180/// Add a child error: an error that is believed to have contributed to this
181/// error occurring. Allows root causing high level errors from lower level
182/// errors that contributed to them.
Craig Tillerf0f70a82016-06-23 13:55:06 -0700183grpc_error *grpc_error_add_child(grpc_error *src,
184 grpc_error *child) GRPC_MUST_USE_RESULT;
Craig Tillerc027e772016-05-03 16:27:00 -0700185grpc_error *grpc_os_error(const char *file, int line, int err,
Craig Tillerf0f70a82016-06-23 13:55:06 -0700186 const char *call_name) GRPC_MUST_USE_RESULT;
Craig Tillera65475c2016-06-02 15:57:44 -0700187/// create an error associated with errno!=0 (an 'operating system' error)
Craig Tillerc027e772016-05-03 16:27:00 -0700188#define GRPC_OS_ERROR(err, call_name) \
189 grpc_os_error(__FILE__, __LINE__, err, call_name)
Craig Tillera41ac572016-05-17 16:08:17 -0700190grpc_error *grpc_wsa_error(const char *file, int line, int err,
Craig Tillerf0f70a82016-06-23 13:55:06 -0700191 const char *call_name) GRPC_MUST_USE_RESULT;
Craig Tillera65475c2016-06-02 15:57:44 -0700192/// windows only: create an error associated with WSAGetLastError()!=0
Craig Tillera41ac572016-05-17 16:08:17 -0700193#define GRPC_WSA_ERROR(err, call_name) \
194 grpc_wsa_error(__FILE__, __LINE__, err, call_name)
Craig Tiller27f59af2016-04-28 14:19:48 -0700195
Craig Tiller4f1d0f32016-05-06 17:12:37 -0700196bool grpc_log_if_error(const char *what, grpc_error *error, const char *file,
197 int line);
198#define GRPC_LOG_IF_ERROR(what, error) \
199 grpc_log_if_error((what), (error), __FILE__, __LINE__)
200
Mark D. Roth757e84e2016-10-06 13:07:53 -0700201#ifdef __cplusplus
202}
203#endif
204
Craig Tiller27f59af2016-04-28 14:19:48 -0700205#endif /* GRPC_CORE_LIB_IOMGR_ERROR_H */