00001 // Copyright (c) 2017, The Regents of the University of California 00002 // All rights reserved. 00003 // 00004 // Redistribution and use in source and binary forms, with or without 00005 // modification, are permitted provided that the following conditions are met: 00006 // * Redistributions of source code must retain the above copyright 00007 // notice, this list of conditions and the following disclaimer. 00008 // * Redistributions in binary form must reproduce the above copyright 00009 // notice, this list of conditions and the following disclaimer in the 00010 // documentation and/or other materials provided with the distribution. 00011 // * Neither the name of the University of California nor the 00012 // names of its contributors may be used to endorse or promote products 00013 // derived from this software without specific prior written permission. 00014 // 00015 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00016 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00017 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00018 // ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OF THE UNIVERSITY OF CALIFORNIA 00019 // BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00020 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00021 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00022 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00023 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00024 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00025 // POSSIBILITY OF SUCH DAMAGE. 00026 00027 #include "util/status.h" 00028 00029 #include <cstdio> 00030 #include <ostream> 00031 #include <string> 00032 #include <utility> 00033 00034 namespace util { 00035 namespace error { 00036 inline std::string CodeEnumToString(Code code) { 00037 switch (code) { 00038 case OK: 00039 return "OK"; 00040 case CANCELLED: 00041 return "CANCELLED"; 00042 case UNKNOWN: 00043 return "UNKNOWN"; 00044 case INVALID_ARGUMENT: 00045 return "INVALID_ARGUMENT"; 00046 case DEADLINE_EXCEEDED: 00047 return "DEADLINE_EXCEEDED"; 00048 case NOT_FOUND: 00049 return "NOT_FOUND"; 00050 case ALREADY_EXISTS: 00051 return "ALREADY_EXISTS"; 00052 case PERMISSION_DENIED: 00053 return "PERMISSION_DENIED"; 00054 case UNAUTHENTICATED: 00055 return "UNAUTHENTICATED"; 00056 case RESOURCE_EXHAUSTED: 00057 return "RESOURCE_EXHAUSTED"; 00058 case FAILED_PRECONDITION: 00059 return "FAILED_PRECONDITION"; 00060 case ABORTED: 00061 return "ABORTED"; 00062 case OUT_OF_RANGE: 00063 return "OUT_OF_RANGE"; 00064 case UNIMPLEMENTED: 00065 return "UNIMPLEMENTED"; 00066 case INTERNAL: 00067 return "INTERNAL"; 00068 case UNAVAILABLE: 00069 return "UNAVAILABLE"; 00070 case DATA_LOSS: 00071 return "DATA_LOSS"; 00072 } 00073 00074 // No default clause, clang will abort if a code is missing from 00075 // above switch. 00076 return "UNKNOWN"; 00077 } 00078 } // namespace error. 00079 00080 const Status Status::OK = Status(); 00081 const Status Status::CANCELLED = Status(error::CANCELLED, ""); 00082 const Status Status::UNKNOWN = Status(error::UNKNOWN, ""); 00083 00084 Status::Status() : error_code_(error::OK) { 00085 } 00086 00087 Status::Status(error::Code error_code, std::string error_message) 00088 : error_code_(error_code) { 00089 if (error_code != error::OK) { 00090 error_message_ = error_message; 00091 } 00092 } 00093 00094 Status::Status(const Status& other) 00095 : error_code_(other.error_code_), error_message_(other.error_message_) { 00096 } 00097 00098 Status& Status::operator=(const Status& other) { 00099 error_code_ = other.error_code_; 00100 error_message_ = other.error_message_; 00101 return *this; 00102 } 00103 00104 bool Status::operator==(const Status& x) const { 00105 return error_code_ == x.error_code_ && 00106 error_message_ == x.error_message_; 00107 } 00108 00109 std::string Status::ToString() const { 00110 if (error_code_ == error::OK) { 00111 return "OK"; 00112 } else { 00113 if (error_message_.empty()) { 00114 return error::CodeEnumToString(error_code_); 00115 } else { 00116 return error::CodeEnumToString(error_code_) + ":" + 00117 error_message_; 00118 } 00119 } 00120 } 00121 00122 std::ostream& operator<<(std::ostream& os, const Status& x) { 00123 os << x.ToString(); 00124 return os; 00125 } 00126 00127 } // namespace util 00128 00129 00130 // Protocol Buffers - Google's data interchange format 00131 // Copyright 2008 Google Inc. All rights reserved. 00132 // https://developers.google.com/protocol-buffers/ 00133 // 00134 // Redistribution and use in source and binary forms, with or without 00135 // modification, are permitted provided that the following conditions are 00136 // met: 00137 // 00138 // * Redistributions of source code must retain the above copyright 00139 // notice, this list of conditions and the following disclaimer. 00140 // * Redistributions in binary form must reproduce the above 00141 // copyright notice, this list of conditions and the following disclaimer 00142 // in the documentation and/or other materials provided with the 00143 // distribution. 00144 // * Neither the name of Google Inc. nor the names of its 00145 // contributors may be used to endorse or promote products derived from 00146 // this software without specific prior written permission. 00147 // 00148 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00149 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00150 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00151 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00152 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00153 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00154 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00155 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00156 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00157 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00158 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.