1 /*
2 *
3 * Copyright 2017 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 <grpcpp/support/error_details.h>
20
21 #include "src/proto/grpc/status/status.pb.h"
22
23 namespace grpc_impl {
24
ExtractErrorDetails(const grpc::Status & from,::google::rpc::Status * to)25 grpc::Status ExtractErrorDetails(const grpc::Status& from,
26 ::google::rpc::Status* to) {
27 if (to == nullptr) {
28 return grpc::Status(grpc::StatusCode::FAILED_PRECONDITION, "");
29 }
30 if (!to->ParseFromString(from.error_details())) {
31 return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "");
32 }
33 return grpc::Status::OK;
34 }
35
SetErrorDetails(const::google::rpc::Status & from,grpc::Status * to)36 grpc::Status SetErrorDetails(const ::google::rpc::Status& from,
37 grpc::Status* to) {
38 if (to == nullptr) {
39 return grpc::Status(grpc::StatusCode::FAILED_PRECONDITION, "");
40 }
41 grpc::StatusCode code = grpc::StatusCode::UNKNOWN;
42 if (from.code() >= grpc::StatusCode::OK &&
43 from.code() <= grpc::StatusCode::UNAUTHENTICATED) {
44 code = static_cast<grpc::StatusCode>(from.code());
45 }
46 *to = grpc::Status(code, from.message(), from.SerializeAsString());
47 return grpc::Status::OK;
48 }
49
50 } // namespace grpc_impl
51