Google Cloud C++ Client  1.32.1
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 <string>
21 #include <tuple>
22 
23 namespace google {
24 namespace cloud {
25 inline namespace GOOGLE_CLOUD_CPP_NS {
26 /**
27  * Well-known status codes with `grpc::StatusCode`-compatible values.
28  *
29  * The semantics of these values are documented in:
30  * https://grpc.io/grpc/cpp/classgrpc_1_1_status.html
31  *
32  */
33 enum class StatusCode {
34  /// Not an error; returned on success.
35  kOk = 0,
36 
37  kCancelled = 1,
38  kUnknown = 2,
39  kInvalidArgument = 3,
41  kNotFound = 5,
42  kAlreadyExists = 6,
44  kUnauthenticated = 16,
47  kAborted = 10,
48  kOutOfRange = 11,
49  kUnimplemented = 12,
50  kInternal = 13,
51  kUnavailable = 14,
52  kDataLoss = 15,
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() = default;
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  StatusCode code() const { return code_; }
74  std::string const& message() const { return message_; }
75 
76  private:
78  std::string message_;
79 };
80 
81 inline std::ostream& operator<<(std::ostream& os, Status const& rhs) {
82  return os << rhs.message() << " [" << StatusCodeToString(rhs.code()) << "]";
83 }
84 
85 inline bool operator==(Status const& lhs, Status const& rhs) {
86  return lhs.code() == rhs.code() && lhs.message() == rhs.message();
87 }
88 
89 inline bool operator!=(Status const& lhs, Status const& rhs) {
90  return !(lhs == rhs);
91 }
92 
93 class RuntimeStatusError : public std::runtime_error {
94  public:
95  explicit RuntimeStatusError(Status status);
96 
97  Status const& status() const { return status_; }
98 
99  private:
100  Status status_;
101 };
102 
103 } // namespace GOOGLE_CLOUD_CPP_NS
104 } // namespace cloud
105 } // namespace google
106 
107 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STATUS_H