error_utils.cc
Go to the documentation of this file.
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 
20 
22 
23 #include <stdint.h>
24 
25 #include <vector>
26 
27 #include "absl/strings/string_view.h"
28 
30 
33 
34 #ifndef GRPC_ERROR_IS_ABSEIL_STATUS
36 #endif
37 
40  intptr_t unused;
41  // If the error itself has a status code, return it.
42  if (grpc_error_get_int(error, which, &unused)) {
43  return error;
44  }
45 #ifdef GRPC_ERROR_IS_ABSEIL_STATUS
46  std::vector<absl::Status> children = grpc_core::StatusGetChildren(error);
47  for (const absl::Status& child : children) {
49  if (!GRPC_ERROR_IS_NONE(result)) return result;
50  }
51 #else
53  // Otherwise, search through its children.
54  uint8_t slot = error->first_err;
55  while (slot != UINT8_MAX) {
56  grpc_linked_error* lerr =
57  reinterpret_cast<grpc_linked_error*>(error->arena + slot);
60  if (result) return result;
61  slot = lerr->next;
62  }
63 #endif
64  return GRPC_ERROR_NONE;
65 }
66 
68  grpc_core::Timestamp deadline,
70  grpc_http2_error_code* http_error,
71  const char** error_string) {
72  // Fast path: We expect no error.
74  if (code != nullptr) *code = GRPC_STATUS_OK;
75  if (message != nullptr) {
76  // Normally, we call grpc_error_get_str(
77  // error, GRPC_ERROR_STR_GRPC_MESSAGE, message).
78  // We can fastpath since we know that:
79  // 1) Error is null
80  // 2) which == GRPC_ERROR_STR_GRPC_MESSAGE
81  // 3) The resulting message is statically known.
82  // 4) Said resulting message is "".
83  // This means 3 movs, instead of 10s of instructions and a strlen.
84  *message = "";
85  }
86  if (http_error != nullptr) {
87  *http_error = GRPC_HTTP2_NO_ERROR;
88  }
89  return;
90  }
91 
92  // Start with the parent error and recurse through the tree of children
93  // until we find the first one that has a status code.
94  grpc_error_handle found_error =
96  if (GRPC_ERROR_IS_NONE(found_error)) {
99  found_error =
101  }
102 
103  // If we found an error with a status code above, use that; otherwise,
104  // fall back to using the parent error.
105  if (GRPC_ERROR_IS_NONE(found_error)) found_error = error;
106 
108  intptr_t integer;
109  if (grpc_error_get_int(found_error, GRPC_ERROR_INT_GRPC_STATUS, &integer)) {
110  status = static_cast<grpc_status_code>(integer);
111  } else if (grpc_error_get_int(found_error, GRPC_ERROR_INT_HTTP2_ERROR,
112  &integer)) {
114  static_cast<grpc_http2_error_code>(integer), deadline);
115  } else {
116 #ifdef GRPC_ERROR_IS_ABSEIL_STATUS
117  status = static_cast<grpc_status_code>(found_error.code());
118 #endif
119  }
120  if (code != nullptr) *code = status;
121 
122  if (error_string != nullptr && status != GRPC_STATUS_OK) {
123  *error_string = gpr_strdup(grpc_error_std_string(error).c_str());
124  }
125 
126  if (http_error != nullptr) {
127  if (grpc_error_get_int(found_error, GRPC_ERROR_INT_HTTP2_ERROR, &integer)) {
128  *http_error = static_cast<grpc_http2_error_code>(integer);
129  } else if (grpc_error_get_int(found_error, GRPC_ERROR_INT_GRPC_STATUS,
130  &integer)) {
131  *http_error =
132  grpc_status_to_http2_error(static_cast<grpc_status_code>(integer));
133  } else {
134  *http_error = GRPC_ERROR_IS_NONE(found_error) ? GRPC_HTTP2_NO_ERROR
136  }
137  }
138 
139  // If the error has a status message, use it. Otherwise, fall back to
140  // the error description.
141  if (message != nullptr) {
143  message)) {
145  message)) {
146 #ifdef GRPC_ERROR_IS_ABSEIL_STATUS
148 #else
149  *message = "unknown error";
150 #endif
151  }
152  }
153  }
154 }
155 
158  // TODO(yashykt): This should be updated once we decide on how to use the
159  // absl::Status payload to capture all the contents of grpc_error.
162  &message, nullptr /* http_error */,
163  nullptr /* error_string */);
164  return absl::Status(static_cast<absl::StatusCode>(status), message);
165 }
166 
168  // Special error checks
169  if (status.ok()) {
170  return GRPC_ERROR_NONE;
171  }
172  return grpc_error_set_int(
175 }
176 
178  intptr_t unused;
180  return true;
181  }
182 #ifdef GRPC_ERROR_IS_ABSEIL_STATUS
183  std::vector<absl::Status> children = grpc_core::StatusGetChildren(error);
184  for (const absl::Status& child : children) {
186  return true;
187  }
188  }
189 #else
190  uint8_t slot = error->first_err;
191  while (slot != UINT8_MAX) {
192  grpc_linked_error* lerr =
193  reinterpret_cast<grpc_linked_error*>(error->arena + slot);
195  return true;
196  }
197  slot = lerr->next;
198  }
199 #endif
200  return false;
201 }
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
error_internal.h
grpc_core::StatusGetChildren
std::vector< absl::Status > StatusGetChildren(absl::Status status)
Returns all children status from a status.
Definition: status_helper.cc:276
grpc_linked_error
Definition: error_internal.h:35
GRPC_ERROR_INT_HTTP2_ERROR
@ GRPC_ERROR_INT_HTTP2_ERROR
http2 error code associated with the error (see the HTTP2 RFC)
Definition: error.h:77
GPR_LIKELY
#define GPR_LIKELY(x)
Definition: impl/codegen/port_platform.h:769
grpc_core::Timestamp
Definition: src/core/lib/gprpp/time.h:62
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error
grpc_error_handle error
Definition: retry_filter.cc:499
grpc_status_code
grpc_status_code
Definition: include/grpc/impl/codegen/status.h:28
grpc_linked_error::next
uint8_t next
Definition: error_internal.h:37
status
absl::Status status
Definition: rls.cc:251
grpc_linked_error::err
grpc_error_handle err
Definition: error_internal.h:36
status_helper.h
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
GRPC_ERROR_STR_DESCRIPTION
@ GRPC_ERROR_STR_DESCRIPTION
top-level textual description of this error
Definition: error.h:106
message
char * message
Definition: libuv/docs/code/tty-gravity/main.c:12
string_util.h
UINT8_MAX
#define UINT8_MAX
Definition: stdint-msvc2008.h:140
GRPC_STATUS_OK
@ GRPC_STATUS_OK
Definition: include/grpc/impl/codegen/status.h:30
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
absl::Status::message
absl::string_view message() const
Definition: third_party/abseil-cpp/absl/status/status.h:806
python_utils.jobset.which
def which(filename)
Definition: jobset.py:157
grpc_error::first_err
uint8_t first_err
Definition: error_internal.h:56
grpc_error_get_int
bool grpc_error_get_int(grpc_error_handle err, grpc_error_ints which, intptr_t *p)
Definition: error.cc:635
GRPC_ERROR_STR_GRPC_MESSAGE
@ GRPC_ERROR_STR_GRPC_MESSAGE
grpc status message associated with this error
Definition: error.h:120
googletest-filter-unittest.child
child
Definition: bloaty/third_party/googletest/googletest/test/googletest-filter-unittest.py:62
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
grpc_error::arena
intptr_t arena[0]
Definition: error_internal.h:61
grpc_status_to_http2_error
grpc_http2_error_code grpc_status_to_http2_error(grpc_status_code status)
Definition: status_conversion.cc:25
GRPC_HTTP2_INTERNAL_ERROR
@ GRPC_HTTP2_INTERNAL_ERROR
Definition: http2_errors.h:26
stdint.h
absl::Status
ABSL_NAMESPACE_BEGIN class ABSL_MUST_USE_RESULT Status
Definition: abseil-cpp/absl/status/internal/status_internal.h:36
absl_status_to_grpc_error
grpc_error_handle absl_status_to_grpc_error(absl::Status status)
Definition: error_utils.cc:167
grpc_http2_error_code
grpc_http2_error_code
Definition: http2_errors.h:23
absl::StatusCode
StatusCode
Definition: third_party/abseil-cpp/absl/status/status.h:92
grpc_error_set_int
grpc_error_handle grpc_error_set_int(grpc_error_handle src, grpc_error_ints which, intptr_t value)
Definition: error.cc:613
recursively_find_error_with_field
static grpc_error_handle recursively_find_error_with_field(grpc_error_handle error, grpc_error_ints which)
Definition: error_utils.cc:38
grpc_error_std_string
std::string grpc_error_std_string(grpc_error_handle error)
Definition: error.cc:944
GRPC_ERROR_CREATE_FROM_STRING_VIEW
#define GRPC_ERROR_CREATE_FROM_STRING_VIEW(desc)
Definition: error.h:300
absl::Status
Definition: third_party/abseil-cpp/absl/status/status.h:424
grpc_error_has_clear_grpc_status
bool grpc_error_has_clear_grpc_status(grpc_error_handle error)
Definition: error_utils.cc:177
absl::Status::ok
ABSL_MUST_USE_RESULT bool ok() const
Definition: third_party/abseil-cpp/absl/status/status.h:802
grpc_error_is_special
bool grpc_error_is_special(grpc_error_handle err)
Definition: error.h:243
gpr_strdup
GPRAPI char * gpr_strdup(const char *src)
Definition: string.cc:39
grpc_error_ints
grpc_error_ints
Definition: error.h:54
grpc_error_get_status
void grpc_error_get_status(grpc_error_handle error, grpc_core::Timestamp deadline, grpc_status_code *code, std::string *message, grpc_http2_error_code *http_error, const char **error_string)
Definition: error_utils.cc:67
grpc_core::Timestamp::InfFuture
static constexpr Timestamp InfFuture()
Definition: src/core/lib/gprpp/time.h:79
status_conversion.h
grpc_error_get_str
bool grpc_error_get_str(grpc_error_handle err, grpc_error_strs which, std::string *s)
Returns false if the specified string is not set.
Definition: error.cc:659
code
Definition: bloaty/third_party/zlib/contrib/infback9/inftree9.h:24
grpc_error
Definition: error_internal.h:42
grpc_error_to_absl_status
absl::Status grpc_error_to_absl_status(grpc_error_handle error)
Definition: error_utils.cc:156
absl::Status::code
absl::StatusCode code() const
Definition: third_party/abseil-cpp/absl/status/status.cc:233
children
std::map< std::string, Node * > children
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/field_mask_util.cc:257
GRPC_HTTP2_NO_ERROR
@ GRPC_HTTP2_NO_ERROR
Definition: http2_errors.h:24
grpc_http2_error_to_grpc_status
grpc_status_code grpc_http2_error_to_grpc_status(grpc_http2_error_code error, grpc_core::Timestamp deadline)
Definition: status_conversion.cc:44
GRPC_ERROR_INT_GRPC_STATUS
@ GRPC_ERROR_INT_GRPC_STATUS
grpc status code representing this error
Definition: error.h:66
GRPC_STATUS_UNKNOWN
@ GRPC_STATUS_UNKNOWN
Definition: include/grpc/impl/codegen/status.h:40
error_utils.h
GRPC_ERROR_IS_NONE
#define GRPC_ERROR_IS_NONE(err)
Definition: error.h:241
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:19