-
Notifications
You must be signed in to change notification settings - Fork 375
/
status.h
174 lines (151 loc) · 5.27 KB
/
status.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STATUS_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STATUS_H
#include "google/cloud/version.h"
#include "absl/types/optional.h"
#include <iostream>
#include <memory>
#include <string>
#include <unordered_map>
namespace google {
namespace cloud {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
/**
* Well-known status codes with `grpc::StatusCode`-compatible values.
*
* The semantics of these values are documented in:
* https://grpc.io/grpc/cpp/classgrpc_1_1_status.html
*/
enum class StatusCode {
/// Not an error; returned on success.
kOk = 0,
kCancelled = 1,
kUnknown = 2,
kInvalidArgument = 3,
kDeadlineExceeded = 4,
kNotFound = 5,
kAlreadyExists = 6,
kPermissionDenied = 7,
kUnauthenticated = 16,
kResourceExhausted = 8,
kFailedPrecondition = 9,
kAborted = 10,
kOutOfRange = 11,
kUnimplemented = 12,
kInternal = 13,
kUnavailable = 14,
kDataLoss = 15,
};
std::string StatusCodeToString(StatusCode code);
std::ostream& operator<<(std::ostream& os, StatusCode code);
class Status;
namespace internal {
void SetPayload(Status&, std::string key, std::string payload);
absl::optional<std::string> GetPayload(Status const&, std::string const& key);
} // namespace internal
/**
* Describes the cause of the error with structured details.
*
* @see
* https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto
*/
class ErrorInfo {
public:
ErrorInfo() = default;
explicit ErrorInfo(std::string reason, std::string domain,
std::unordered_map<std::string, std::string> metadata)
: reason_(std::move(reason)),
domain_(std::move(domain)),
metadata_(std::move(metadata)) {}
std::string const& reason() const { return reason_; }
std::string const& domain() const { return domain_; }
std::unordered_map<std::string, std::string> const& metadata() const {
return metadata_;
}
friend inline bool operator==(ErrorInfo const& a, ErrorInfo const& b) {
return a.reason_ == b.reason_ && a.domain_ == b.domain_ &&
a.metadata_ == b.metadata_;
}
friend inline bool operator!=(ErrorInfo const& a, ErrorInfo const& b) {
return !(a == b);
}
private:
std::string reason_;
std::string domain_;
std::unordered_map<std::string, std::string> metadata_;
};
/**
* Represents success or an error with info about the error.
* This class is typically used to indicate whether or not a function or other
* operation completed successfully. Success is indicated by an "OK" status. OK
* statuses will have `.code() == StatusCode::kOk` and `.ok() == true`, with
* all other properties having empty values. All OK statuses are equal. Any
* non-OK `Status` is considered an error. Users can inspect the error using
* the member functions, or they can simply stream the `Status` object, and it
* will print itself in some human readable way (the streamed format may change
* over time and you should *not* depend on the specific format of a streamed
* `Status` object remaining unchanged).
*
* This is a regular value type that can be copied, moved, compared for
* equality, and streamed.
*/
class Status {
public:
Status();
~Status();
Status(Status const&);
Status& operator=(Status const&);
Status(Status&&) noexcept;
Status& operator=(Status&&) noexcept;
/**
* Constructs a Status with the given @p code and @p message.
*
* Ignores @p message if @p code is `StatusCode::kOk`.
*/
explicit Status(StatusCode code, std::string message, ErrorInfo info = {});
bool ok() const { return !impl_; }
StatusCode code() const;
std::string const& message() const;
ErrorInfo const& error_info() const;
friend inline bool operator==(Status const& a, Status const& b) {
return (a.ok() && b.ok()) || Equals(a, b);
}
friend inline bool operator!=(Status const& a, Status const& b) {
return !(a == b);
}
friend std::ostream& operator<<(std::ostream& os, Status const& s);
private:
static bool Equals(Status const& a, Status const& b);
friend void internal::SetPayload(Status&, std::string, std::string);
friend absl::optional<std::string> internal::GetPayload(Status const&,
std::string const&);
class Impl;
// A null `impl_` is an OK status. Only non-OK Statuses allocate an Impl.
std::unique_ptr<Impl> impl_;
};
/**
* A runtime error that wraps a `google::cloud::Status`.
*/
class RuntimeStatusError : public std::runtime_error {
public:
explicit RuntimeStatusError(Status status);
Status const& status() const { return status_; }
private:
Status status_;
};
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace cloud
} // namespace google
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STATUS_H