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