• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/transport/status_conversion.h"
26 
recursively_find_error_with_field(grpc_error * error,grpc_error_ints which)27 static grpc_error* recursively_find_error_with_field(grpc_error* error,
28                                                      grpc_error_ints which) {
29   // If the error itself has a status code, return it.
30   if (grpc_error_get_int(error, which, nullptr)) {
31     return error;
32   }
33   if (grpc_error_is_special(error)) return nullptr;
34   // Otherwise, search through its children.
35   uint8_t slot = error->first_err;
36   while (slot != UINT8_MAX) {
37     grpc_linked_error* lerr =
38         reinterpret_cast<grpc_linked_error*>(error->arena + slot);
39     grpc_error* result = recursively_find_error_with_field(lerr->err, which);
40     if (result) return result;
41     slot = lerr->next;
42   }
43   return nullptr;
44 }
45 
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)46 void grpc_error_get_status(grpc_error* error, grpc_millis deadline,
47                            grpc_status_code* code, grpc_slice* slice,
48                            grpc_http2_error_code* http_error,
49                            const char** error_string) {
50   // Start with the parent error and recurse through the tree of children
51   // until we find the first one that has a status code.
52   grpc_error* found_error =
53       recursively_find_error_with_field(error, GRPC_ERROR_INT_GRPC_STATUS);
54   if (found_error == nullptr) {
55     /// If no grpc-status exists, retry through the tree to find a http2 error
56     /// code
57     found_error =
58         recursively_find_error_with_field(error, GRPC_ERROR_INT_HTTP2_ERROR);
59   }
60 
61   // If we found an error with a status code above, use that; otherwise,
62   // fall back to using the parent error.
63   if (found_error == nullptr) found_error = error;
64 
65   grpc_status_code status = GRPC_STATUS_UNKNOWN;
66   intptr_t integer;
67   if (grpc_error_get_int(found_error, GRPC_ERROR_INT_GRPC_STATUS, &integer)) {
68     status = static_cast<grpc_status_code>(integer);
69   } else if (grpc_error_get_int(found_error, GRPC_ERROR_INT_HTTP2_ERROR,
70                                 &integer)) {
71     status = grpc_http2_error_to_grpc_status(
72         static_cast<grpc_http2_error_code>(integer), deadline);
73   }
74   if (code != nullptr) *code = status;
75 
76   if (error_string != nullptr && status != GRPC_STATUS_OK) {
77     *error_string = gpr_strdup(grpc_error_string(error));
78   }
79 
80   if (http_error != nullptr) {
81     if (grpc_error_get_int(found_error, GRPC_ERROR_INT_HTTP2_ERROR, &integer)) {
82       *http_error = static_cast<grpc_http2_error_code>(integer);
83     } else if (grpc_error_get_int(found_error, GRPC_ERROR_INT_GRPC_STATUS,
84                                   &integer)) {
85       *http_error =
86           grpc_status_to_http2_error(static_cast<grpc_status_code>(integer));
87     } else {
88       *http_error = found_error == GRPC_ERROR_NONE ? GRPC_HTTP2_NO_ERROR
89                                                    : GRPC_HTTP2_INTERNAL_ERROR;
90     }
91   }
92 
93   // If the error has a status message, use it.  Otherwise, fall back to
94   // the error description.
95   if (slice != nullptr) {
96     if (!grpc_error_get_str(found_error, GRPC_ERROR_STR_GRPC_MESSAGE, slice)) {
97       if (!grpc_error_get_str(found_error, GRPC_ERROR_STR_DESCRIPTION, slice)) {
98         *slice = grpc_slice_from_static_string("unknown error");
99       }
100     }
101   }
102 }
103 
grpc_error_has_clear_grpc_status(grpc_error * error)104 bool grpc_error_has_clear_grpc_status(grpc_error* error) {
105   if (grpc_error_get_int(error, GRPC_ERROR_INT_GRPC_STATUS, nullptr)) {
106     return true;
107   }
108   uint8_t slot = error->first_err;
109   while (slot != UINT8_MAX) {
110     grpc_linked_error* lerr =
111         reinterpret_cast<grpc_linked_error*>(error->arena + slot);
112     if (grpc_error_has_clear_grpc_status(lerr->err)) {
113       return true;
114     }
115     slot = lerr->next;
116   }
117   return false;
118 }
119