Google Cloud C++ Client  0.4.0
C++ Client Library for Google Cloud Platform
status.h
Go to the documentation of this file.
1 // Copyright 2018 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 // http://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 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STATUS_H_
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STATUS_H_
17 
18 #include "google/cloud/version.h"
19 #include <iostream>
20 #include <tuple>
21 
22 namespace google {
23 namespace cloud {
24 inline namespace GOOGLE_CLOUD_CPP_NS {
25 /**
26  * Well-known status codes with `grpc::StatusCode`-compatible values.
27  *
28  * The semantics of these values are documented in:
29  * https://grpc.io/grpc/cpp/classgrpc_1_1_status.html
30  *
31  */
32 enum class StatusCode {
33  /// Not an error; returned on success.
34  kOk = 0,
35 
36  kCancelled = 1,
37  kUnknown = 2,
38  kInvalidArgument = 3,
40  kNotFound = 5,
41  kAlreadyExists = 6,
43  kUnauthenticated = 16,
46  kAborted = 10,
47  kOutOfRange = 11,
48  kUnimplemented = 12,
49  kInternal = 13,
50  kUnavailable = 14,
51  kDataLoss = 15,
52  kDoNotUse = -1
53 };
54 
55 std::string StatusCodeToString(StatusCode code);
56 std::ostream& operator<<(std::ostream& os, StatusCode code);
57 
58 /**
59  * Reports error code and details from a remote request.
60  *
61  * This class is modeled after `grpc::Status`, it contains the status code and
62  * error message (if applicable) from a JSON request.
63  */
64 class Status {
65  public:
66  Status() : code_(StatusCode::kOk) {}
67 
68  explicit Status(StatusCode status_code, std::string message)
69  : code_(status_code), message_(std::move(message)) {}
70 
71  bool ok() const { return code_ == StatusCode::kOk; }
72 
73  bool operator==(Status const& rhs) const {
74  return code() == rhs.code() && message() == rhs.message();
75  }
76  bool operator!=(Status const& rhs) const { return !(*this == rhs); }
77 
78  StatusCode code() const { return code_; }
79  std::string const& message() const { return message_; }
80 
81  private:
82  StatusCode code_;
83  std::string message_;
84 };
85 
86 inline std::ostream& operator<<(std::ostream& os, Status const& rhs) {
87  return os << rhs.message() << " [" << StatusCodeToString(rhs.code()) << "]";
88 }
89 
90 class RuntimeStatusError : public std::runtime_error {
91  public:
92  explicit RuntimeStatusError(Status status);
93 
94  Status const& status() const { return status_; }
95 
96  private:
97  Status status_;
98 };
99 
100 } // namespace GOOGLE_CLOUD_CPP_NS
101 } // namespace cloud
102 } // namespace google
103 
104 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STATUS_H_
bool operator==(Status const &rhs) const
Definition: status.h:73
#define GOOGLE_CLOUD_CPP_NS
Definition: version.h:24
Reports error code and details from a remote request.
Definition: status.h:64
Contains all the Google Cloud C++ Library APIs.
Definition: iam_bindings.cc:21
std::string StatusCodeToString(StatusCode code)
Definition: status.cc:29
Not an error; returned on success.
Status(StatusCode status_code, std::string message)
Definition: status.h:68
StatusCode code() const
Definition: status.h:78
bool ok() const
Definition: status.h:71
std::string const & message() const
Definition: status.h:79
Status const & status() const
Definition: status.h:94
StatusCode
Well-known status codes with grpc::StatusCode-compatible values.
Definition: status.h:32
bool operator!=(Status const &rhs) const
Definition: status.h:76