blob: 13f898e31ad73402f0d75363db1a2451617ad383 [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
Craig Tillerc027e772016-05-03 16:27:00 -070040#include <grpc/support/time.h>
41
Craig Tillera65475c2016-06-02 15:57:44 -070042/// Opaque representation of an error.
43/// Errors are refcounted objects that represent the result of an operation.
44/// Ownership laws:
45/// if a grpc_error is returned by a function, the caller owns a ref to that
46/// instance
47/// if a grpc_error is passed to a grpc_closure callback function (functions
48/// with the signature:
49/// void (*f)(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error))
50/// then those functions do not automatically own a ref to error
51/// if a grpc_error is passed to *ANY OTHER FUNCTION* then that function takes
52/// ownership of the error
53/// Errors have:
54/// a set of ints, strings, and timestamps that describe the error
55/// always present are:
56/// GRPC_ERROR_STR_FILE, GRPC_ERROR_INT_FILE_LINE - source location the error
57/// was generated
58/// GRPC_ERROR_STR_DESCRIPTION - a human readable description of the error
59/// GRPC_ERROR_TIME_CREATED - a timestamp indicating when the error happened
60/// an error can also have children; these are other errors that are believed
61/// to have contributed to this one. By accumulating children, we can begin
62/// to root cause high level failures from low level failures, without having
63/// to derive execution paths from log lines
Craig Tiller27f59af2016-04-28 14:19:48 -070064typedef struct grpc_error grpc_error;
65
66typedef enum {
Craig Tillera65475c2016-06-02 15:57:44 -070067 /// 'errno' from the operating system
Craig Tillerc027e772016-05-03 16:27:00 -070068 GRPC_ERROR_INT_ERRNO,
Craig Tillera65475c2016-06-02 15:57:44 -070069 /// __LINE__ from the call site creating the error
Craig Tillerc027e772016-05-03 16:27:00 -070070 GRPC_ERROR_INT_FILE_LINE,
Craig Tillera65475c2016-06-02 15:57:44 -070071 /// stream identifier: for errors that are associated with an individual
72 /// wire stream
Craig Tiller781bab532016-05-05 08:15:28 -070073 GRPC_ERROR_INT_STREAM_ID,
Craig Tillera65475c2016-06-02 15:57:44 -070074 /// grpc status code representing this error
Craig Tiller781bab532016-05-05 08:15:28 -070075 GRPC_ERROR_INT_GRPC_STATUS,
Craig Tillera65475c2016-06-02 15:57:44 -070076 /// offset into some binary blob (usually represented by
77 /// GRPC_ERROR_STR_RAW_BYTES) where the error occurred
Craig Tiller781bab532016-05-05 08:15:28 -070078 GRPC_ERROR_INT_OFFSET,
Craig Tillera65475c2016-06-02 15:57:44 -070079 /// context sensitive index associated with the error
Craig Tiller781bab532016-05-05 08:15:28 -070080 GRPC_ERROR_INT_INDEX,
Craig Tillera65475c2016-06-02 15:57:44 -070081 /// context sensitive size associated with the error
Craig Tiller781bab532016-05-05 08:15:28 -070082 GRPC_ERROR_INT_SIZE,
Craig Tillera65475c2016-06-02 15:57:44 -070083 /// http2 error code associated with the error (see the HTTP2 RFC)
Craig Tiller94e15762016-05-05 08:44:36 -070084 GRPC_ERROR_INT_HTTP2_ERROR,
Craig Tillera65475c2016-06-02 15:57:44 -070085 /// TSI status code associated with the error
Craig Tiller804ff712016-05-05 16:25:40 -070086 GRPC_ERROR_INT_TSI_CODE,
Craig Tillera65475c2016-06-02 15:57:44 -070087 /// grpc_security_status associated with the error
Craig Tiller804ff712016-05-05 16:25:40 -070088 GRPC_ERROR_INT_SECURITY_STATUS,
Craig Tillera65475c2016-06-02 15:57:44 -070089 /// WSAGetLastError() reported when this error occurred
Craig Tillera41ac572016-05-17 16:08:17 -070090 GRPC_ERROR_INT_WSA_ERROR,
Craig Tillera65475c2016-06-02 15:57:44 -070091 /// File descriptor associated with this error
Craig Tiller80384bd2016-05-06 16:12:31 -070092 GRPC_ERROR_INT_FD,
Craig Tiller34b11df2016-06-09 17:17:15 -070093 /// HTTP status (i.e. 404)
94 GRPC_ERROR_INT_HTTP_STATUS,
Craig Tillerf0f70a82016-06-23 13:55:06 -070095 /// context sensitive limit associated with the error
96 GRPC_ERROR_INT_LIMIT,
Craig Tiller27f59af2016-04-28 14:19:48 -070097} grpc_error_ints;
98
99typedef enum {
Craig Tillera65475c2016-06-02 15:57:44 -0700100 /// top-level textual description of this error
Craig Tiller27f59af2016-04-28 14:19:48 -0700101 GRPC_ERROR_STR_DESCRIPTION,
Craig Tillera65475c2016-06-02 15:57:44 -0700102 /// source file in which this error occurred
Craig Tillerc027e772016-05-03 16:27:00 -0700103 GRPC_ERROR_STR_FILE,
Craig Tillera65475c2016-06-02 15:57:44 -0700104 /// operating system description of this error
Craig Tiller27f59af2016-04-28 14:19:48 -0700105 GRPC_ERROR_STR_OS_ERROR,
Craig Tillera65475c2016-06-02 15:57:44 -0700106 /// syscall that generated this error
Craig Tillerc027e772016-05-03 16:27:00 -0700107 GRPC_ERROR_STR_SYSCALL,
Craig Tillera65475c2016-06-02 15:57:44 -0700108 /// peer that we were trying to communicate when this error occurred
Craig Tillerc027e772016-05-03 16:27:00 -0700109 GRPC_ERROR_STR_TARGET_ADDRESS,
Craig Tillera65475c2016-06-02 15:57:44 -0700110 /// grpc status message associated with this error
Craig Tiller781bab532016-05-05 08:15:28 -0700111 GRPC_ERROR_STR_GRPC_MESSAGE,
Craig Tillera65475c2016-06-02 15:57:44 -0700112 /// hex dump (or similar) with the data that generated this error
Craig Tiller781bab532016-05-05 08:15:28 -0700113 GRPC_ERROR_STR_RAW_BYTES,
Craig Tillera65475c2016-06-02 15:57:44 -0700114 /// tsi error string associated with this error
Craig Tiller804ff712016-05-05 16:25:40 -0700115 GRPC_ERROR_STR_TSI_ERROR,
Craig Tillera65475c2016-06-02 15:57:44 -0700116 /// filename that we were trying to read/write when this error occurred
Craig Tiller4727b9b2016-05-17 17:19:19 -0700117 GRPC_ERROR_STR_FILENAME,
Craig Tiller27f59af2016-04-28 14:19:48 -0700118} grpc_error_strs;
119
Craig Tillerc027e772016-05-03 16:27:00 -0700120typedef enum {
Craig Tillera65475c2016-06-02 15:57:44 -0700121 /// timestamp of error creation
Craig Tillerc027e772016-05-03 16:27:00 -0700122 GRPC_ERROR_TIME_CREATED,
123} grpc_error_times;
124
Craig Tiller27f59af2016-04-28 14:19:48 -0700125#define GRPC_ERROR_NONE ((grpc_error *)NULL)
126#define GRPC_ERROR_OOM ((grpc_error *)1)
Craig Tillerc027e772016-05-03 16:27:00 -0700127#define GRPC_ERROR_CANCELLED ((grpc_error *)2)
Craig Tiller27f59af2016-04-28 14:19:48 -0700128
129const char *grpc_error_string(grpc_error *error);
130void grpc_error_free_string(const char *str);
131
Craig Tillera65475c2016-06-02 15:57:44 -0700132/// Create an error - but use GRPC_ERROR_CREATE instead
Craig Tillerc027e772016-05-03 16:27:00 -0700133grpc_error *grpc_error_create(const char *file, int line, const char *desc,
134 grpc_error **referencing, size_t num_referencing);
Craig Tillera65475c2016-06-02 15:57:44 -0700135/// Create an error (this is the preferred way of generating an error that is
136/// not due to a system call - for system calls, use GRPC_OS_ERROR or
137/// GRPC_WSA_ERROR as appropriate)
138/// \a referencing is an array of num_referencing elements indicating one or
139/// more errors that are believed to have contributed to this one
140/// err = grpc_error_create(x, y, z, r, nr) is equivalent to:
141/// err = grpc_error_create(x, y, z, NULL, 0);
142/// for (i=0; i<nr; i++) err = grpc_error_add_child(err, r[i]);
Craig Tillerc027e772016-05-03 16:27:00 -0700143#define GRPC_ERROR_CREATE(desc) \
144 grpc_error_create(__FILE__, __LINE__, desc, NULL, 0)
David Garcia Quintas32ec1332016-05-11 12:22:53 -0700145
146// Create an error that references some other errors. This function adds a
147// reference to each error in errs - it does not consume an existing reference
Craig Tillerc027e772016-05-03 16:27:00 -0700148#define GRPC_ERROR_CREATE_REFERENCING(desc, errs, count) \
149 grpc_error_create(__FILE__, __LINE__, desc, errs, count)
Craig Tillerf707d622016-05-06 14:26:12 -0700150
Craig Tiller47017682016-05-13 16:56:26 -0700151//#define GRPC_ERROR_REFCOUNT_DEBUG
Craig Tillere0d6c572016-05-13 07:23:36 -0700152#ifdef GRPC_ERROR_REFCOUNT_DEBUG
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700153grpc_error *grpc_error_ref(grpc_error *err, const char *file, int line,
154 const char *func);
155void grpc_error_unref(grpc_error *err, const char *file, int line,
156 const char *func);
157#define GRPC_ERROR_REF(err) grpc_error_ref(err, __FILE__, __LINE__, __func__)
158#define GRPC_ERROR_UNREF(err) \
159 grpc_error_unref(err, __FILE__, __LINE__, __func__)
Craig Tillere0d6c572016-05-13 07:23:36 -0700160#else
161grpc_error *grpc_error_ref(grpc_error *err);
162void grpc_error_unref(grpc_error *err);
163#define GRPC_ERROR_REF(err) grpc_error_ref(err)
164#define GRPC_ERROR_UNREF(err) grpc_error_unref(err)
165#endif
Craig Tillerf707d622016-05-06 14:26:12 -0700166
Craig Tiller27f59af2016-04-28 14:19:48 -0700167grpc_error *grpc_error_set_int(grpc_error *src, grpc_error_ints which,
Craig Tillerf0f70a82016-06-23 13:55:06 -0700168 intptr_t value) GRPC_MUST_USE_RESULT;
Craig Tiller965eab32016-05-07 22:11:37 -0700169bool grpc_error_get_int(grpc_error *error, grpc_error_ints which, intptr_t *p);
Craig Tillerc027e772016-05-03 16:27:00 -0700170grpc_error *grpc_error_set_time(grpc_error *src, grpc_error_times which,
Craig Tillerf0f70a82016-06-23 13:55:06 -0700171 gpr_timespec value) GRPC_MUST_USE_RESULT;
Craig Tiller27f59af2016-04-28 14:19:48 -0700172grpc_error *grpc_error_set_str(grpc_error *src, grpc_error_strs which,
Craig Tillerf0f70a82016-06-23 13:55:06 -0700173 const char *value) GRPC_MUST_USE_RESULT;
174const char *grpc_error_get_str(grpc_error *error, grpc_error_strs which);
Craig Tillera65475c2016-06-02 15:57:44 -0700175/// Add a child error: an error that is believed to have contributed to this
176/// error occurring. Allows root causing high level errors from lower level
177/// errors that contributed to them.
Craig Tillerf0f70a82016-06-23 13:55:06 -0700178grpc_error *grpc_error_add_child(grpc_error *src,
179 grpc_error *child) GRPC_MUST_USE_RESULT;
Craig Tillerc027e772016-05-03 16:27:00 -0700180grpc_error *grpc_os_error(const char *file, int line, int err,
Craig Tillerf0f70a82016-06-23 13:55:06 -0700181 const char *call_name) GRPC_MUST_USE_RESULT;
Craig Tillera65475c2016-06-02 15:57:44 -0700182/// create an error associated with errno!=0 (an 'operating system' error)
Craig Tillerc027e772016-05-03 16:27:00 -0700183#define GRPC_OS_ERROR(err, call_name) \
184 grpc_os_error(__FILE__, __LINE__, err, call_name)
Craig Tillera41ac572016-05-17 16:08:17 -0700185grpc_error *grpc_wsa_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/// windows only: create an error associated with WSAGetLastError()!=0
Craig Tillera41ac572016-05-17 16:08:17 -0700188#define GRPC_WSA_ERROR(err, call_name) \
189 grpc_wsa_error(__FILE__, __LINE__, err, call_name)
Craig Tiller27f59af2016-04-28 14:19:48 -0700190
Craig Tiller4f1d0f32016-05-06 17:12:37 -0700191bool grpc_log_if_error(const char *what, grpc_error *error, const char *file,
192 int line);
193#define GRPC_LOG_IF_ERROR(what, error) \
194 grpc_log_if_error((what), (error), __FILE__, __LINE__)
195
Craig Tiller27f59af2016-04-28 14:19:48 -0700196#endif /* GRPC_CORE_LIB_IOMGR_ERROR_H */