Debian Codegraph @

src/core/lib/transport/error_utils.cc

raw
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 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/transport/error_utils.h"
22
23 #include <stdint.h>
24
25 #include <vector>
26
27 #include "absl/strings/string_view.h"
28
29 #include <grpc/support/string_util.h>
30
31 #include "src/core/lib/gprpp/status_helper.h"
32 #include "src/core/lib/transport/status_conversion.h"
33
34 static grpc_error_handle recursively_find_error_with_field(
35 grpc_error_handle error, grpc_core::StatusIntProperty which) {
36 intptr_t unused;
37 // If the error itself has a status code, return it.
38 if (grpc_error_get_int(error, which, &unused)) {
39 return error;
40 }
41 std::vector<absl::Status> children = grpc_core::StatusGetChildren(error);
42 for (const absl::Status& child : children) {
43 grpc_error_handle result = recursively_find_error_with_field(child, which);
44 if (!result.ok()) return result;
45 }
46 return absl::OkStatus();
47 }
48
49 void grpc_error_get_status(grpc_error_handle error,
50 grpc_core::Timestamp deadline,
51 grpc_status_code* code, std::string* message,
52 grpc_http2_error_code* http_error,
53 const char** error_string) {
54 // Fast path: We expect no error.
55 if (GPR_LIKELY(error.ok())) {
56 if (code != nullptr) *code = GRPC_STATUS_OK;
57 if (message != nullptr) {
58 // Normally, we call grpc_error_get_str(
59 // error, grpc_core::StatusStrProperty::kGrpcMessage, message).
60 // We can fastpath since we know that:
61 // 1) Error is null
62 // 2) which == grpc_core::StatusStrProperty::kGrpcMessage
63 // 3) The resulting message is statically known.
64 // 4) Said resulting message is "".
65 // This means 3 movs, instead of 10s of instructions and a strlen.
66 *message = "";
67 }
68 if (http_error != nullptr) {
69 *http_error = GRPC_HTTP2_NO_ERROR;
70 }
71 return;
72 }
73
74 // Start with the parent error and recurse through the tree of children
75 // until we find the first one that has a status code.
76 grpc_error_handle found_error = recursively_find_error_with_field(
77 error, grpc_core::StatusIntProperty::kRpcStatus);
78 if (found_error.ok()) {
79 /// If no grpc-status exists, retry through the tree to find a http2 error
80 /// code
81 found_error = recursively_find_error_with_field(
82 error, grpc_core::StatusIntProperty::kHttp2Error);
83 }
84
85 // If we found an error with a status code above, use that; otherwise,
86 // fall back to using the parent error.
87 if (found_error.ok()) found_error = error;
88
89 grpc_status_code status = GRPC_STATUS_UNKNOWN;
90 intptr_t integer;
91 if (grpc_error_get_int(found_error, grpc_core::StatusIntProperty::kRpcStatus,
92 &integer)) {
93 status = static_cast<grpc_status_code>(integer);
94 } else if (grpc_error_get_int(found_error,
95 grpc_core::StatusIntProperty::kHttp2Error,
96 &integer)) {
97 status = grpc_http2_error_to_grpc_status(
98 static_cast<grpc_http2_error_code>(integer), deadline);
99 } else {
100 status = static_cast<grpc_status_code>(found_error.code());
101 }
102 if (code != nullptr) *code = status;
103
104 if (error_string != nullptr && status != GRPC_STATUS_OK) {
105 *error_string = gpr_strdup(grpc_core::StatusToString(error).c_str());
106 }
107
108 if (http_error != nullptr) {
109 if (grpc_error_get_int(
110 found_error, grpc_core::StatusIntProperty::kHttp2Error, &integer)) {
111 *http_error = static_cast<grpc_http2_error_code>(integer);
112 } else if (grpc_error_get_int(found_error,
113 grpc_core::StatusIntProperty::kRpcStatus,
114 &integer)) {
115 *http_error =
116 grpc_status_to_http2_error(static_cast<grpc_status_code>(integer));
117 } else {
118 *http_error =
119 found_error.ok() ? GRPC_HTTP2_NO_ERROR : GRPC_HTTP2_INTERNAL_ERROR;
120 }
121 }
122
123 // If the error has a status message, use it. Otherwise, fall back to
124 // the error description.
125 if (message != nullptr) {
126 if (!grpc_error_get_str(
127 found_error, grpc_core::StatusStrProperty::kGrpcMessage, message)) {
128 if (!grpc_error_get_str(found_error,
129 grpc_core::StatusStrProperty::kDescription,
130 message)) {
131 *message = grpc_core::StatusToString(error);
132 }
133 }
134 }
135 }
136
137 absl::Status grpc_error_to_absl_status(grpc_error_handle error) {
138 grpc_status_code status;
139 // TODO(yashykt): This should be updated once we decide on how to use the
140 // absl::Status payload to capture all the contents of grpc_error.
141 std::string message;
142 grpc_error_get_status(error, grpc_core::Timestamp::InfFuture(), &status,
143 &message, nullptr /* http_error */,
144 nullptr /* error_string */);
145 return absl::Status(static_cast<absl::StatusCode>(status), message);
146 }
147
148 grpc_error_handle absl_status_to_grpc_error(absl::Status status) {
149 // Special error checks
150 if (status.ok()) {
151 return absl::OkStatus();
152 }
153 return grpc_error_set_int(GRPC_ERROR_CREATE(status.message()),
154 grpc_core::StatusIntProperty::kRpcStatus,
155 static_cast<grpc_status_code>(status.code()));
156 }
157
158 bool grpc_error_has_clear_grpc_status(grpc_error_handle error) {
159 intptr_t unused;
160 if (grpc_error_get_int(error, grpc_core::StatusIntProperty::kRpcStatus,
161 &unused)) {
162 return true;
163 }
164 std::vector<absl::Status> children = grpc_core::StatusGetChildren(error);
165 for (const absl::Status& child : children) {
166 if (grpc_error_has_clear_grpc_status(child)) {
167 return true;
168 }
169 }
170 return false;
171 }