Debian Codegraph @

src/core/lib/iomgr/error.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 #include <grpc/support/port_platform.h>
19
20 #include "src/core/lib/iomgr/error.h"
21
22 #include <inttypes.h>
23 #include <string.h>
24
25 #include "absl/strings/str_format.h"
26
27 #include <grpc/impl/codegen/status.h>
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/log.h>
30 #include <grpc/support/string_util.h>
31
32 #ifdef GPR_WINDOWS
33 #include <grpc/support/log_windows.h>
34 #endif
35
36 #include "src/core/lib/debug/trace.h"
37 #include "src/core/lib/gpr/useful.h"
38 #include "src/core/lib/gprpp/strerror.h"
39 #include "src/core/lib/slice/slice_internal.h"
40
41 grpc_core::DebugOnlyTraceFlag grpc_trace_error_refcount(false,
42 "error_refcount");
43 grpc_core::DebugOnlyTraceFlag grpc_trace_closure(false, "closure");
44
45 absl::Status grpc_status_create(absl::StatusCode code, absl::string_view msg,
46 const grpc_core::DebugLocation& location,
47 size_t children_count, absl::Status* children) {
48 absl::Status s = StatusCreate(code, msg, location, {});
49 for (size_t i = 0; i < children_count; ++i) {
50 if (!children[i].ok()) {
51 grpc_core::StatusAddChild(&s, children[i]);
52 }
53 }
54 return s;
55 }
56
57 absl::Status grpc_os_error(const grpc_core::DebugLocation& location, int err,
58 const char* call_name) {
59 auto err_string = grpc_core::StrError(err);
60 absl::Status s =
61 StatusCreate(absl::StatusCode::kUnknown, err_string, location, {});
62 grpc_core::StatusSetInt(&s, grpc_core::StatusIntProperty::kErrorNo, err);
63 grpc_core::StatusSetStr(&s, grpc_core::StatusStrProperty::kOsError,
64 err_string);
65 grpc_core::StatusSetStr(&s, grpc_core::StatusStrProperty::kSyscall,
66 call_name);
67 return s;
68 }
69
70 #ifdef GPR_WINDOWS
71 absl::Status grpc_wsa_error(const grpc_core::DebugLocation& location, int err,
72 const char* call_name) {
73 char* utf8_message = gpr_format_message(err);
74 absl::Status s =
75 StatusCreate(absl::StatusCode::kUnavailable, "WSA Error", location, {});
76 StatusSetInt(&s, grpc_core::StatusIntProperty::kWsaError, err);
77 StatusSetInt(&s, grpc_core::StatusIntProperty::kRpcStatus,
78 GRPC_STATUS_UNAVAILABLE);
79 StatusSetStr(&s, grpc_core::StatusStrProperty::kOsError, utf8_message);
80 StatusSetStr(&s, grpc_core::StatusStrProperty::kSyscall, call_name);
81 gpr_free(utf8_message);
82 return s;
83 }
84 #endif
85
86 grpc_error_handle grpc_error_set_int(grpc_error_handle src,
87 grpc_core::StatusIntProperty which,
88 intptr_t value) {
89 if (src.ok()) {
90 src = absl::UnknownError("");
91 StatusSetInt(&src, grpc_core::StatusIntProperty::kRpcStatus,
92 GRPC_STATUS_OK);
93 }
94 grpc_core::StatusSetInt(&src, which, value);
95 return src;
96 }
97
98 bool grpc_error_get_int(grpc_error_handle error,
99 grpc_core::StatusIntProperty which, intptr_t* p) {
100 absl::optional<intptr_t> value = grpc_core::StatusGetInt(error, which);
101 if (value.has_value()) {
102 *p = *value;
103 return true;
104 } else {
105 // TODO(veblush): Remove this once absl::Status migration is done
106 if (which == grpc_core::StatusIntProperty::kRpcStatus) {
107 switch (error.code()) {
108 case absl::StatusCode::kOk:
109 *p = GRPC_STATUS_OK;
110 return true;
111 case absl::StatusCode::kResourceExhausted:
112 *p = GRPC_STATUS_RESOURCE_EXHAUSTED;
113 return true;
114 case absl::StatusCode::kCancelled:
115 *p = GRPC_STATUS_CANCELLED;
116 return true;
117 default:
118 break;
119 }
120 }
121 return false;
122 }
123 }
124
125 grpc_error_handle grpc_error_set_str(grpc_error_handle src,
126 grpc_core::StatusStrProperty which,
127 absl::string_view str) {
128 if (src.ok()) {
129 src = absl::UnknownError("");
130 StatusSetInt(&src, grpc_core::StatusIntProperty::kRpcStatus,
131 GRPC_STATUS_OK);
132 }
133 if (which == grpc_core::StatusStrProperty::kDescription) {
134 // To change the message of absl::Status, a new instance should be created
135 // with a code and payload because it doesn't have a setter for it.
136 absl::Status s = absl::Status(src.code(), str);
137 src.ForEachPayload(
138 [&](absl::string_view type_url, const absl::Cord& payload) {
139 s.SetPayload(type_url, payload);
140 });
141 return s;
142 } else {
143 grpc_core::StatusSetStr(&src, which, str);
144 }
145 return src;
146 }
147
148 bool grpc_error_get_str(grpc_error_handle error,
149 grpc_core::StatusStrProperty which, std::string* s) {
150 if (which == grpc_core::StatusStrProperty::kDescription) {
151 // absl::Status uses the message field for
152 // grpc_core::StatusStrProperty::kDescription instead of using payload.
153 absl::string_view msg = error.message();
154 if (msg.empty()) {
155 return false;
156 } else {
157 *s = std::string(msg);
158 return true;
159 }
160 } else {
161 absl::optional<std::string> value = grpc_core::StatusGetStr(error, which);
162 if (value.has_value()) {
163 *s = std::move(*value);
164 return true;
165 } else {
166 // TODO(veblush): Remove this once absl::Status migration is done
167 if (which == grpc_core::StatusStrProperty::kGrpcMessage) {
168 switch (error.code()) {
169 case absl::StatusCode::kOk:
170 *s = "";
171 return true;
172 case absl::StatusCode::kCancelled:
173 *s = "CANCELLED";
174 return true;
175 default:
176 break;
177 }
178 }
179 return false;
180 }
181 }
182 }
183
184 grpc_error_handle grpc_error_add_child(grpc_error_handle src,
185 grpc_error_handle child) {
186 if (src.ok()) {
187 return child;
188 } else {
189 if (!child.ok()) {
190 grpc_core::StatusAddChild(&src, child);
191 }
192 return src;
193 }
194 }
195
196 bool grpc_log_error(const char* what, grpc_error_handle error, const char* file,
197 int line) {
198 GPR_DEBUG_ASSERT(!error.ok());
199 gpr_log(file, line, GPR_LOG_SEVERITY_ERROR, "%s: %s", what,
200 grpc_core::StatusToString(error).c_str());
201 return false;
202 }