Google APIs Client Library for C++
|
00001 /* 00002 * \copyright Copyright 2013 Google Inc. All Rights Reserved. 00003 * \license @{ 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 * 00017 * @} 00018 */ 00019 00020 00021 #ifndef UTIL_TASK_STATUS_H_ 00022 #define UTIL_TASK_STATUS_H_ 00023 #include <string> 00024 using std::string; 00025 #include "googleapis/strings/stringpiece.h" 00026 namespace googleapis { 00027 00028 namespace util { 00029 namespace error { 00030 00031 /* 00032 * <table><tr><th>Code<th>Indented Purpose 00033 * <tr><td>OK <td>Everything is fine; no error. 00034 * <tr><td>CANCELLED <td>The operation has been cancelled. 00035 * <tr><td>UNKNOWN <td>The cause of error is unknown. 00036 * <tr><td>INVALID_ARGUMENT <td>The operation received an invalid argument. 00037 * <tr><td>DEADLINE_EXCEEDED 00038 * <td>The operation terminated early due to a deadline. 00039 * <tr><td>NOT_FOUND <td>The requested resource or data element was missing. 00040 * <tr><td>ALREADY_EXISTS <td>The resource or data element already exists. 00041 * <tr><td>PERMISSION_DENIED 00042 * <td>The caller has insufficient permission to perform the operation. 00043 * <tr><td>RESOURCE_EXHAUSTED 00044 * <td>Not enough resources (usually memory, disk etc) to perform the 00045 * operation. 00046 * <tr><td>FAILED_PRECONDITION 00047 * <td>The caller did not meet the operation's requirements. 00048 * <tr><td>ABORTED <td>The operation aborted prematurely for some reason. 00049 * <tr><td>OUT_OF_RANGE 00050 * <td>The requested resource or data element is not valid. Usually this 00051 * refers to some index or key. 00052 * <tr><td>UNIMPLEMENTED <td>The requestd operation is not fully implemented. 00053 * <tr><td>INTERNAL <td>An error in the implementation was detected. 00054 * <tr><td>UNAVAILABLE 00055 * <td>Some resource or data is not available to perform the operation now. 00056 * <tr><td>DATA_LOSS 00057 * <td>The operation could not access all the data, or lost some along 00058 * the way. 00059 * </table> 00060 */ 00061 enum Code { 00062 OK = 0, 00063 CANCELLED = 1, 00064 UNKNOWN = 2, 00065 INVALID_ARGUMENT = 3, 00066 DEADLINE_EXCEEDED = 4, 00067 NOT_FOUND = 5, 00068 ALREADY_EXISTS = 6, 00069 PERMISSION_DENIED = 7, 00070 RESOURCE_EXHAUSTED = 8, 00071 FAILED_PRECONDITION = 9, 00072 ABORTED = 10, 00073 OUT_OF_RANGE = 11, 00074 UNIMPLEMENTED = 12, 00075 INTERNAL = 13, 00076 UNAVAILABLE = 14, 00077 DATA_LOSS = 15, 00078 }; 00079 00080 /* 00081 * Smallest code value (inclusive). 00082 */ 00083 const Code Code_MIN = OK; 00084 00085 /* 00086 * Largest code value (inclusive). 00087 */ 00088 const Code Code_MAX = DATA_LOSS; 00089 00090 } // namespace error 00091 00092 /* 00093 * Denotes whether a call or object is error free, and explains why if not. 00094 * 00095 * Status objects are used through the Google APIs Client Library for C++ 00096 * to return and propagate errors rather than using C++ exceptions. They 00097 * are simple data objects that support copy and assignment so that they 00098 * can propagate across scopes and out of dynamic objects that may be 00099 * destroyed before the error is propagated. 00100 * 00101 * If the status is not ok() then the code() and error_message() will 00102 * indicate why. 00103 */ 00104 class Status { 00105 public: 00106 /* 00107 * Constructs a default OK status. 00108 */ 00109 Status() : code_(googleapis::util::error::OK) {} 00110 00111 /* 00112 * Constructs a status with the given code and message. 00113 * @param[in] code The status code for the instance. 00114 * @param[in] msg If the code is other than OK then this should not be empty. 00115 */ 00116 Status(googleapis::util::error::Code code, const StringPiece& msg) 00117 : code_(code), msg_(msg.as_string()) {} 00118 00119 /* 00120 * Copy constructor. 00121 */ 00122 Status(const Status& status) : code_(status.code_), msg_(status.msg_) {} 00123 00124 /* 00125 * Standard destructor. 00126 */ 00127 ~Status() {} 00128 00129 /* 00130 * Assignment operator. 00131 */ 00132 Status& operator =(const Status& status) { 00133 code_ = status.code_; 00134 msg_ = status.msg_; 00135 return *this; 00136 } 00137 00138 /* 00139 * Equality operator. 00140 */ 00141 bool operator ==(const Status& status) const { 00142 return code_ == status.code_ && msg_ == status.msg_; 00143 } 00144 00145 /* 00146 * Determine if the status is ok(). 00147 * @return true if the error code is OK, false otherwise. 00148 */ 00149 bool ok() const { return code_ == googleapis::util::error::OK; } 00150 00151 /* 00152 * Get explanation bound at construction. 00153 */ 00154 const string& error_message() const { return msg_; } 00155 00156 /* 00157 * Get error_code bound at construction. 00158 */ 00159 googleapis::util::error::Code error_code() const { return code_; } 00160 00161 /* 00162 * Convert the status to a detailed string. 00163 * 00164 * If displaying the error to a user than error_message might be preferred 00165 * since it has less technical jargon. 00166 * 00167 * @see error_message() 00168 */ 00169 string ToString() const; 00170 00171 /* 00172 * This method is a NOP that confirms we are ignoring a status. 00173 */ 00174 void IgnoreError() const {} 00175 00176 private: 00177 googleapis::util::error::Code code_; 00178 string msg_; 00179 }; 00180 00181 } // namespace util 00182 00183 } // namespace googleapis 00184 #endif // UTIL_TASK_STATUS_H_