blob: 2613512acbeedd5970d64bb10a07e4cf1bb56c45 [file] [log] [blame]
Craig Tiller27f59af2016-04-28 14:19:48 -07001/*
2 *
Craig Tillera65475c2016-06-02 15:57:44 -07003 * Copyright 2016, Google Inc.
Craig Tiller27f59af2016-04-28 14:19:48 -07004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#ifndef GRPC_CORE_LIB_IOMGR_ERROR_H
35#define GRPC_CORE_LIB_IOMGR_ERROR_H
36
Craig Tiller4f1d0f32016-05-06 17:12:37 -070037#include <stdbool.h>
Craig Tiller27f59af2016-04-28 14:19:48 -070038#include <stdint.h>
39
Mark D. Roth1e35b692016-09-02 13:44:32 -070040#include <grpc/status.h>
Craig Tillerc027e772016-05-03 16:27:00 -070041#include <grpc/support/time.h>
42
Mark D. Roth757e84e2016-10-06 13:07:53 -070043#ifdef __cplusplus
44extern "C" {
45#endif
46
Craig Tillera65475c2016-06-02 15:57:44 -070047/// Opaque representation of an error.
48/// Errors are refcounted objects that represent the result of an operation.
49/// Ownership laws:
50/// if a grpc_error is returned by a function, the caller owns a ref to that
51/// instance
52/// if a grpc_error is passed to a grpc_closure callback function (functions
53/// with the signature:
54/// void (*f)(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error))
David Garcia Quintas4bf00c92016-08-19 12:39:10 -070055/// then those functions do not own a ref to error (but are free to manually
56/// take a reference).
Craig Tillera65475c2016-06-02 15:57:44 -070057/// if a grpc_error is passed to *ANY OTHER FUNCTION* then that function takes
58/// ownership of the error
59/// Errors have:
60/// a set of ints, strings, and timestamps that describe the error
61/// always present are:
62/// GRPC_ERROR_STR_FILE, GRPC_ERROR_INT_FILE_LINE - source location the error
63/// was generated
64/// GRPC_ERROR_STR_DESCRIPTION - a human readable description of the error
65/// GRPC_ERROR_TIME_CREATED - a timestamp indicating when the error happened
66/// an error can also have children; these are other errors that are believed
67/// to have contributed to this one. By accumulating children, we can begin
68/// to root cause high level failures from low level failures, without having
69/// to derive execution paths from log lines
Craig Tiller27f59af2016-04-28 14:19:48 -070070typedef struct grpc_error grpc_error;
71
72typedef enum {
Craig Tillera65475c2016-06-02 15:57:44 -070073 /// 'errno' from the operating system
Craig Tillerc027e772016-05-03 16:27:00 -070074 GRPC_ERROR_INT_ERRNO,
Craig Tillera65475c2016-06-02 15:57:44 -070075 /// __LINE__ from the call site creating the error
Craig Tillerc027e772016-05-03 16:27:00 -070076 GRPC_ERROR_INT_FILE_LINE,
Craig Tillera65475c2016-06-02 15:57:44 -070077 /// stream identifier: for errors that are associated with an individual
78 /// wire stream
Craig Tiller781bab532016-05-05 08:15:28 -070079 GRPC_ERROR_INT_STREAM_ID,
Craig Tillera65475c2016-06-02 15:57:44 -070080 /// grpc status code representing this error
Craig Tiller781bab532016-05-05 08:15:28 -070081 GRPC_ERROR_INT_GRPC_STATUS,
Craig Tillera65475c2016-06-02 15:57:44 -070082 /// offset into some binary blob (usually represented by
83 /// GRPC_ERROR_STR_RAW_BYTES) where the error occurred
Craig Tiller781bab532016-05-05 08:15:28 -070084 GRPC_ERROR_INT_OFFSET,
Craig Tillera65475c2016-06-02 15:57:44 -070085 /// context sensitive index associated with the error
Craig Tiller781bab532016-05-05 08:15:28 -070086 GRPC_ERROR_INT_INDEX,
Craig Tillera65475c2016-06-02 15:57:44 -070087 /// context sensitive size associated with the error
Craig Tiller781bab532016-05-05 08:15:28 -070088 GRPC_ERROR_INT_SIZE,
Craig Tillera65475c2016-06-02 15:57:44 -070089 /// http2 error code associated with the error (see the HTTP2 RFC)
Craig Tiller94e15762016-05-05 08:44:36 -070090 GRPC_ERROR_INT_HTTP2_ERROR,
Craig Tillera65475c2016-06-02 15:57:44 -070091 /// TSI status code associated with the error
Craig Tiller804ff712016-05-05 16:25:40 -070092 GRPC_ERROR_INT_TSI_CODE,
Craig Tillera65475c2016-06-02 15:57:44 -070093 /// grpc_security_status associated with the error
Craig Tiller804ff712016-05-05 16:25:40 -070094 GRPC_ERROR_INT_SECURITY_STATUS,
Craig Tillera65475c2016-06-02 15:57:44 -070095 /// WSAGetLastError() reported when this error occurred
Craig Tillera41ac572016-05-17 16:08:17 -070096 GRPC_ERROR_INT_WSA_ERROR,
Craig Tillera65475c2016-06-02 15:57:44 -070097 /// File descriptor associated with this error
Craig Tiller80384bd2016-05-06 16:12:31 -070098 GRPC_ERROR_INT_FD,
Craig Tiller34b11df2016-06-09 17:17:15 -070099 /// HTTP status (i.e. 404)
100 GRPC_ERROR_INT_HTTP_STATUS,
Craig Tillerf0f70a82016-06-23 13:55:06 -0700101 /// context sensitive limit associated with the error
102 GRPC_ERROR_INT_LIMIT,
Craig Tiller936f1ea2016-10-14 15:15:19 -0700103 /// chttp2: did the error occur while a write was in progress
104 GRPC_ERROR_INT_OCCURRED_DURING_WRITE,
Craig Tiller27f59af2016-04-28 14:19:48 -0700105} grpc_error_ints;
106
107typedef enum {
Craig Tillera65475c2016-06-02 15:57:44 -0700108 /// top-level textual description of this error
Craig Tiller27f59af2016-04-28 14:19:48 -0700109 GRPC_ERROR_STR_DESCRIPTION,
Craig Tillera65475c2016-06-02 15:57:44 -0700110 /// source file in which this error occurred
Craig Tillerc027e772016-05-03 16:27:00 -0700111 GRPC_ERROR_STR_FILE,
Craig Tillera65475c2016-06-02 15:57:44 -0700112 /// operating system description of this error
Craig Tiller27f59af2016-04-28 14:19:48 -0700113 GRPC_ERROR_STR_OS_ERROR,
Craig Tillera65475c2016-06-02 15:57:44 -0700114 /// syscall that generated this error
Craig Tillerc027e772016-05-03 16:27:00 -0700115 GRPC_ERROR_STR_SYSCALL,
Craig Tillera65475c2016-06-02 15:57:44 -0700116 /// peer that we were trying to communicate when this error occurred
Craig Tillerc027e772016-05-03 16:27:00 -0700117 GRPC_ERROR_STR_TARGET_ADDRESS,
Craig Tillera65475c2016-06-02 15:57:44 -0700118 /// grpc status message associated with this error
Craig Tiller781bab532016-05-05 08:15:28 -0700119 GRPC_ERROR_STR_GRPC_MESSAGE,
Craig Tillera65475c2016-06-02 15:57:44 -0700120 /// hex dump (or similar) with the data that generated this error
Craig Tiller781bab532016-05-05 08:15:28 -0700121 GRPC_ERROR_STR_RAW_BYTES,
Craig Tillera65475c2016-06-02 15:57:44 -0700122 /// tsi error string associated with this error
Craig Tiller804ff712016-05-05 16:25:40 -0700123 GRPC_ERROR_STR_TSI_ERROR,
Craig Tillera65475c2016-06-02 15:57:44 -0700124 /// filename that we were trying to read/write when this error occurred
Craig Tiller4727b9b2016-05-17 17:19:19 -0700125 GRPC_ERROR_STR_FILENAME,
Craig Tiller936f1ea2016-10-14 15:15:19 -0700126 /// which data was queued for writing when the error occurred
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800127 GRPC_ERROR_STR_QUEUED_BUFFERS,
128 /// key associated with the error
129 GRPC_ERROR_STR_KEY,
130 /// value associated with the error
131 GRPC_ERROR_STR_VALUE,
Craig Tiller27f59af2016-04-28 14:19:48 -0700132} grpc_error_strs;
133
Craig Tillerc027e772016-05-03 16:27:00 -0700134typedef enum {
Craig Tillera65475c2016-06-02 15:57:44 -0700135 /// timestamp of error creation
Craig Tillerc027e772016-05-03 16:27:00 -0700136 GRPC_ERROR_TIME_CREATED,
137} grpc_error_times;
138
Craig Tiller86037cd02016-09-02 19:58:43 -0700139/// The following "special" errors can be propagated without allocating memory.
Sree Kuchibhotla2fc2b3e2017-02-14 10:05:14 -0800140/// They are always even so that other code (particularly combiner locks,
141/// polling engines) can safely use the lower bit for themselves.
Craig Tiller86037cd02016-09-02 19:58:43 -0700142
Craig Tiller27f59af2016-04-28 14:19:48 -0700143#define GRPC_ERROR_NONE ((grpc_error *)NULL)
Craig Tiller86037cd02016-09-02 19:58:43 -0700144#define GRPC_ERROR_OOM ((grpc_error *)2)
145#define GRPC_ERROR_CANCELLED ((grpc_error *)4)
Craig Tiller27f59af2016-04-28 14:19:48 -0700146
147const char *grpc_error_string(grpc_error *error);
Craig Tiller27f59af2016-04-28 14:19:48 -0700148
Craig Tillera65475c2016-06-02 15:57:44 -0700149/// Create an error - but use GRPC_ERROR_CREATE instead
Craig Tillerc027e772016-05-03 16:27:00 -0700150grpc_error *grpc_error_create(const char *file, int line, const char *desc,
151 grpc_error **referencing, size_t num_referencing);
Craig Tillera65475c2016-06-02 15:57:44 -0700152/// Create an error (this is the preferred way of generating an error that is
153/// not due to a system call - for system calls, use GRPC_OS_ERROR or
154/// GRPC_WSA_ERROR as appropriate)
155/// \a referencing is an array of num_referencing elements indicating one or
156/// more errors that are believed to have contributed to this one
157/// err = grpc_error_create(x, y, z, r, nr) is equivalent to:
158/// err = grpc_error_create(x, y, z, NULL, 0);
159/// for (i=0; i<nr; i++) err = grpc_error_add_child(err, r[i]);
Craig Tillerc027e772016-05-03 16:27:00 -0700160#define GRPC_ERROR_CREATE(desc) \
161 grpc_error_create(__FILE__, __LINE__, desc, NULL, 0)
David Garcia Quintas32ec1332016-05-11 12:22:53 -0700162
163// Create an error that references some other errors. This function adds a
164// reference to each error in errs - it does not consume an existing reference
Craig Tillerc027e772016-05-03 16:27:00 -0700165#define GRPC_ERROR_CREATE_REFERENCING(desc, errs, count) \
166 grpc_error_create(__FILE__, __LINE__, desc, errs, count)
Craig Tillerf707d622016-05-06 14:26:12 -0700167
Craig Tiller065b1392017-01-09 14:05:07 -0800168//#define GRPC_ERROR_REFCOUNT_DEBUG
Craig Tillere0d6c572016-05-13 07:23:36 -0700169#ifdef GRPC_ERROR_REFCOUNT_DEBUG
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700170grpc_error *grpc_error_ref(grpc_error *err, const char *file, int line,
171 const char *func);
172void grpc_error_unref(grpc_error *err, const char *file, int line,
173 const char *func);
174#define GRPC_ERROR_REF(err) grpc_error_ref(err, __FILE__, __LINE__, __func__)
175#define GRPC_ERROR_UNREF(err) \
176 grpc_error_unref(err, __FILE__, __LINE__, __func__)
Craig Tillere0d6c572016-05-13 07:23:36 -0700177#else
178grpc_error *grpc_error_ref(grpc_error *err);
179void grpc_error_unref(grpc_error *err);
180#define GRPC_ERROR_REF(err) grpc_error_ref(err)
181#define GRPC_ERROR_UNREF(err) grpc_error_unref(err)
182#endif
Craig Tillerf707d622016-05-06 14:26:12 -0700183
Craig Tiller27f59af2016-04-28 14:19:48 -0700184grpc_error *grpc_error_set_int(grpc_error *src, grpc_error_ints which,
Craig Tillerf0f70a82016-06-23 13:55:06 -0700185 intptr_t value) GRPC_MUST_USE_RESULT;
Craig Tiller965eab32016-05-07 22:11:37 -0700186bool grpc_error_get_int(grpc_error *error, grpc_error_ints which, intptr_t *p);
Craig Tillerc027e772016-05-03 16:27:00 -0700187grpc_error *grpc_error_set_time(grpc_error *src, grpc_error_times which,
Craig Tillerf0f70a82016-06-23 13:55:06 -0700188 gpr_timespec value) GRPC_MUST_USE_RESULT;
Craig Tiller27f59af2016-04-28 14:19:48 -0700189grpc_error *grpc_error_set_str(grpc_error *src, grpc_error_strs which,
Craig Tillerf0f70a82016-06-23 13:55:06 -0700190 const char *value) GRPC_MUST_USE_RESULT;
Mark D. Roth5d11e432016-06-23 13:14:05 -0700191/// Returns NULL if the specified string is not set.
192/// Caller does NOT own return value.
193const char *grpc_error_get_str(grpc_error *error, grpc_error_strs which);
Mark D. Roth1e35b692016-09-02 13:44:32 -0700194
Craig Tillera65475c2016-06-02 15:57:44 -0700195/// Add a child error: an error that is believed to have contributed to this
196/// error occurring. Allows root causing high level errors from lower level
197/// errors that contributed to them.
Craig Tillerf0f70a82016-06-23 13:55:06 -0700198grpc_error *grpc_error_add_child(grpc_error *src,
199 grpc_error *child) GRPC_MUST_USE_RESULT;
Craig Tillerc027e772016-05-03 16:27:00 -0700200grpc_error *grpc_os_error(const char *file, int line, int err,
Craig Tillerf0f70a82016-06-23 13:55:06 -0700201 const char *call_name) GRPC_MUST_USE_RESULT;
Craig Tillera65475c2016-06-02 15:57:44 -0700202/// create an error associated with errno!=0 (an 'operating system' error)
Craig Tillerc027e772016-05-03 16:27:00 -0700203#define GRPC_OS_ERROR(err, call_name) \
204 grpc_os_error(__FILE__, __LINE__, err, call_name)
Craig Tillera41ac572016-05-17 16:08:17 -0700205grpc_error *grpc_wsa_error(const char *file, int line, int err,
Craig Tillerf0f70a82016-06-23 13:55:06 -0700206 const char *call_name) GRPC_MUST_USE_RESULT;
Craig Tillera65475c2016-06-02 15:57:44 -0700207/// windows only: create an error associated with WSAGetLastError()!=0
Craig Tillera41ac572016-05-17 16:08:17 -0700208#define GRPC_WSA_ERROR(err, call_name) \
209 grpc_wsa_error(__FILE__, __LINE__, err, call_name)
Craig Tiller27f59af2016-04-28 14:19:48 -0700210
Craig Tiller4f1d0f32016-05-06 17:12:37 -0700211bool grpc_log_if_error(const char *what, grpc_error *error, const char *file,
212 int line);
213#define GRPC_LOG_IF_ERROR(what, error) \
214 grpc_log_if_error((what), (error), __FILE__, __LINE__)
215
Mark D. Roth757e84e2016-10-06 13:07:53 -0700216#ifdef __cplusplus
217}
218#endif
219
Craig Tiller27f59af2016-04-28 14:19:48 -0700220#endif /* GRPC_CORE_LIB_IOMGR_ERROR_H */