1 // Copyright 2019 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "sandboxed_api/util/status.h" 16 17 #include <string> 18 #include <utility> 19 20 #include "absl/status/status.h" 21 #include "absl/strings/cord.h" 22 #include "absl/strings/string_view.h" 23 24 namespace sapi { 25 SaveStatusToProto(const absl::Status & status,StatusProto * out)26void SaveStatusToProto(const absl::Status& status, StatusProto* out) { 27 out->set_code(status.raw_code()); 28 out->set_message(std::string(status.message())); 29 auto* payloads = out->mutable_payloads(); 30 status.ForEachPayload( 31 [payloads](absl::string_view type_key, const absl::Cord& payload) { 32 (*payloads)[std::string(type_key)] = static_cast<std::string>(payload); 33 }); 34 } 35 MakeStatusFromProto(const StatusProto & proto)36absl::Status MakeStatusFromProto(const StatusProto& proto) { 37 absl::Status status(static_cast<absl::StatusCode>(proto.code()), 38 proto.message()); 39 // Note: Using C++17 structured bindings instead of `entry` crashes Clang 6.0 40 // on Ubuntu 18.04 (bionic). 41 for (const auto& entry : proto.payloads()) { 42 status.SetPayload(/*type_url=*/entry.first, 43 /*payload=*/absl::Cord(entry.second)); 44 } 45 return status; 46 } 47 48 } // namespace sapi 49