| 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 "absl/status/status.h" |
| 28 |
|
| 29 |
#include <grpc/slice.h> |
| 30 |
#include <grpc/status.h> |
| 31 |
#include <grpc/support/log.h> |
| 32 |
#include <grpc/support/time.h> |
| 33 |
|
| 34 |
#include "src/core/lib/debug/trace.h" |
| 35 |
#include "src/core/lib/gpr/spinlock.h" |
| 36 |
#include "src/core/lib/gprpp/status_helper.h" |
| 37 |
#include "src/core/lib/slice/slice_internal.h" |
| 38 |
|
| 39 |
/// Opaque representation of an error. |
| 40 |
/// See https://github.com/grpc/grpc/blob/master/doc/core/grpc-error.md for a |
| 41 |
/// full write up of this object. |
| 42 |
|
| 43 |
typedef absl::Status grpc_error_handle; |
| 44 |
|
| 45 |
#define GRPC_ERROR_CREATE(desc) \ |
| 46 |
StatusCreate(absl::StatusCode::kUnknown, desc, DEBUG_LOCATION, {}) |
| 47 |
|
| 48 |
absl::Status grpc_status_create(absl::StatusCode code, absl::string_view msg, |
| 49 |
const grpc_core::DebugLocation& location, |
| 50 |
size_t children_count, |
| 51 |
absl::Status* children) GRPC_MUST_USE_RESULT; |
| 52 |
|
| 53 |
// Create an error that references some other errors. |
| 54 |
#define GRPC_ERROR_CREATE_REFERENCING(desc, errs, count) \ |
| 55 |
grpc_status_create(absl::StatusCode::kUnknown, desc, DEBUG_LOCATION, count, \ |
| 56 |
errs) |
| 57 |
|
| 58 |
// Consumes all the errors in the vector and forms a referencing error from |
| 59 |
// them. If the vector is empty, return absl::OkStatus(). |
| 60 |
template <typename VectorType> |
| 61 |
static absl::Status grpc_status_create_from_vector( |
| 62 |
const grpc_core::DebugLocation& location, absl::string_view desc, |
| 63 |
VectorType* error_list) { |
| 64 |
absl::Status error; |
| 65 |
if (error_list->size() != 0) { |
| 66 |
error = grpc_status_create(absl::StatusCode::kUnknown, desc, location, |
| 67 |
error_list->size(), error_list->data()); |
| 68 |
error_list->clear(); |
| 69 |
} |
| 70 |
return error; |
| 71 |
} |
| 72 |
|
| 73 |
#define GRPC_ERROR_CREATE_FROM_VECTOR(desc, error_list) \ |
| 74 |
grpc_status_create_from_vector(DEBUG_LOCATION, desc, error_list) |
| 75 |
|
| 76 |
absl::Status grpc_os_error(const grpc_core::DebugLocation& location, int err, |
| 77 |
const char* call_name) GRPC_MUST_USE_RESULT; |
| 78 |
|
| 79 |
inline absl::Status grpc_assert_never_ok(absl::Status error) { |
| 80 |
GPR_ASSERT(!error.ok()); |
| 81 |
return error; |
| 82 |
} |
| 83 |
|
| 84 |
/// create an error associated with errno!=0 (an 'operating system' error) |
| 85 |
#define GRPC_OS_ERROR(err, call_name) \ |
| 86 |
grpc_assert_never_ok(grpc_os_error(DEBUG_LOCATION, err, call_name)) |
| 87 |
|
| 88 |
absl::Status grpc_wsa_error(const grpc_core::DebugLocation& location, int err, |
| 89 |
const char* call_name) GRPC_MUST_USE_RESULT; |
| 90 |
|
| 91 |
/// windows only: create an error associated with WSAGetLastError()!=0 |
| 92 |
#define GRPC_WSA_ERROR(err, call_name) \ |
| 93 |
grpc_wsa_error(DEBUG_LOCATION, err, call_name) |
| 94 |
|
| 95 |
grpc_error_handle grpc_error_set_int(grpc_error_handle src, |
| 96 |
grpc_core::StatusIntProperty which, |
| 97 |
intptr_t value) GRPC_MUST_USE_RESULT; |
| 98 |
/// It is an error to pass nullptr as `p`. Caller should allocate a phony |
| 99 |
/// intptr_t for `p`, even if the value of `p` is not used. |
| 100 |
bool grpc_error_get_int(grpc_error_handle error, |
| 101 |
grpc_core::StatusIntProperty which, intptr_t* p); |
| 102 |
grpc_error_handle grpc_error_set_str( |
| 103 |
grpc_error_handle src, grpc_core::StatusStrProperty which, |
| 104 |
absl::string_view str) GRPC_MUST_USE_RESULT; |
| 105 |
/// Returns false if the specified string is not set. |
| 106 |
bool grpc_error_get_str(grpc_error_handle error, |
| 107 |
grpc_core::StatusStrProperty which, std::string* str); |
| 108 |
|
| 109 |
/// Add a child error: an error that is believed to have contributed to this |
| 110 |
/// error occurring. Allows root causing high level errors from lower level |
| 111 |
/// errors that contributed to them. The src error takes ownership of the |
| 112 |
/// child error. |
| 113 |
/// |
| 114 |
/// Edge Conditions - |
| 115 |
/// 1) If either of \a src or \a child is absl::OkStatus(), returns a reference |
| 116 |
/// to the other argument. 2) If both \a src and \a child are absl::OkStatus(), |
| 117 |
/// returns absl::OkStatus(). 3) If \a src and \a child point to the same error, |
| 118 |
/// returns a single reference. (Note that, 2 references should have been |
| 119 |
/// received to the error in this case.) |
| 120 |
grpc_error_handle grpc_error_add_child( |
| 121 |
grpc_error_handle src, grpc_error_handle child) GRPC_MUST_USE_RESULT; |
| 122 |
|
| 123 |
bool grpc_log_error(const char* what, grpc_error_handle error, const char* file, |
| 124 |
int line); |
| 125 |
inline bool grpc_log_if_error(const char* what, grpc_error_handle error, |
| 126 |
const char* file, int line) { |
| 127 |
return error.ok() ? true : grpc_log_error(what, error, file, line); |
| 128 |
} |
| 129 |
|
| 130 |
#define GRPC_LOG_IF_ERROR(what, error) \ |
| 131 |
(grpc_log_if_error((what), (error), __FILE__, __LINE__)) |
| 132 |
|
| 133 |
/// Helper class to get & set grpc_error_handle in a thread-safe fashion. |
| 134 |
/// This could be considered as atomic<grpc_error_handle>. |
| 135 |
class AtomicError { |
| 136 |
public: |
| 137 |
AtomicError() = default; |
| 138 |
explicit AtomicError(grpc_error_handle error) { error_ = error; } |
| 139 |
|
| 140 |
AtomicError(const AtomicError&) = delete; |
| 141 |
AtomicError& operator=(const AtomicError&) = delete; |
| 142 |
|
| 143 |
/// returns get() == absl::OkStatus() |
| 144 |
bool ok() { |
| 145 |
gpr_spinlock_lock(&lock_); |
| 146 |
bool ret = error_.ok(); |
| 147 |
gpr_spinlock_unlock(&lock_); |
| 148 |
return ret; |
| 149 |
} |
| 150 |
|
| 151 |
grpc_error_handle get() { |
| 152 |
gpr_spinlock_lock(&lock_); |
| 153 |
grpc_error_handle ret = error_; |
| 154 |
gpr_spinlock_unlock(&lock_); |
| 155 |
return ret; |
| 156 |
} |
| 157 |
|
| 158 |
void set(grpc_error_handle error) { |
| 159 |
gpr_spinlock_lock(&lock_); |
| 160 |
error_ = error; |
| 161 |
gpr_spinlock_unlock(&lock_); |
| 162 |
} |
| 163 |
|
| 164 |
private: |
| 165 |
grpc_error_handle error_; |
| 166 |
gpr_spinlock lock_ = GPR_SPINLOCK_STATIC_INITIALIZER; |
| 167 |
}; |
| 168 |
|
| 169 |
#endif /* GRPC_CORE_LIB_IOMGR_ERROR_H */ |