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