1// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc. All rights reserved.
3// https://developers.google.com/protocol-buffers/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31#ifndef GOOGLE_PROTOBUF_STUBS_STATUS_H_
32#define GOOGLE_PROTOBUF_STUBS_STATUS_H_
33
34#include <string>
35
36#include <google/protobuf/stubs/stringpiece.h>
37
38#include <google/protobuf/port_def.inc>
39
40namespace google {
41namespace protobuf {
42namespace util {
43namespace status_internal {
44
45// These values must match error codes defined in google/rpc/code.proto.
46enum class StatusCode : int {
47 kOk = 0,
48 kCancelled = 1,
49 kUnknown = 2,
50 kInvalidArgument = 3,
51 kDeadlineExceeded = 4,
52 kNotFound = 5,
53 kAlreadyExists = 6,
54 kPermissionDenied = 7,
55 kUnauthenticated = 16,
56 kResourceExhausted = 8,
57 kFailedPrecondition = 9,
58 kAborted = 10,
59 kOutOfRange = 11,
60 kUnimplemented = 12,
61 kInternal = 13,
62 kUnavailable = 14,
63 kDataLoss = 15,
64};
65
66class PROTOBUF_EXPORT Status {
67 public:
68 // Creates a "successful" status.
69 Status();
70
71 // Create a status in the canonical error space with the specified
72 // code, and error message. If "code == 0", error_message is
73 // ignored and a Status object identical to Status::kOk is
74 // constructed.
75 Status(StatusCode error_code, StringPiece error_message);
76 Status(const Status&);
77 Status& operator=(const Status& x);
78 ~Status() {}
79
80 // Accessor
81 bool ok() const { return error_code_ == StatusCode::kOk; }
82 StatusCode code() const { return error_code_; }
83 StringPiece message() const {
84 return error_message_;
85 }
86
87 bool operator==(const Status& x) const;
88 bool operator!=(const Status& x) const {
89 return !operator==(x);
90 }
91
92 // Return a combination of the error code name and message.
93 std::string ToString() const;
94
95 private:
96 StatusCode error_code_;
97 std::string error_message_;
98};
99
100// Returns an OK status, equivalent to a default constructed instance. Prefer
101// usage of `OkStatus()` when constructing such an OK status.
102PROTOBUF_EXPORT Status OkStatus();
103
104// Prints a human-readable representation of 'x' to 'os'.
105PROTOBUF_EXPORT std::ostream& operator<<(std::ostream& os, const Status& x);
106
107// These convenience functions return `true` if a given status matches the
108// `StatusCode` error code of its associated function.
109PROTOBUF_EXPORT bool IsAborted(const Status& status);
110PROTOBUF_EXPORT bool IsAlreadyExists(const Status& status);
111PROTOBUF_EXPORT bool IsCancelled(const Status& status);
112PROTOBUF_EXPORT bool IsDataLoss(const Status& status);
113PROTOBUF_EXPORT bool IsDeadlineExceeded(const Status& status);
114PROTOBUF_EXPORT bool IsFailedPrecondition(const Status& status);
115PROTOBUF_EXPORT bool IsInternal(const Status& status);
116PROTOBUF_EXPORT bool IsInvalidArgument(const Status& status);
117PROTOBUF_EXPORT bool IsNotFound(const Status& status);
118PROTOBUF_EXPORT bool IsOutOfRange(const Status& status);
119PROTOBUF_EXPORT bool IsPermissionDenied(const Status& status);
120PROTOBUF_EXPORT bool IsResourceExhausted(const Status& status);
121PROTOBUF_EXPORT bool IsUnauthenticated(const Status& status);
122PROTOBUF_EXPORT bool IsUnavailable(const Status& status);
123PROTOBUF_EXPORT bool IsUnimplemented(const Status& status);
124PROTOBUF_EXPORT bool IsUnknown(const Status& status);
125
126// These convenience functions create an `Status` object with an error code as
127// indicated by the associated function name, using the error message passed in
128// `message`.
129//
130// These functions are intentionally named `*Error` rather than `*Status` to
131// match the names from Abseil:
132// https://github.com/abseil/abseil-cpp/blob/2e9532cc6c701a8323d0cffb468999ab804095ab/absl/status/status.h#L716
133PROTOBUF_EXPORT Status AbortedError(StringPiece message);
134PROTOBUF_EXPORT Status AlreadyExistsError(StringPiece message);
135PROTOBUF_EXPORT Status CancelledError(StringPiece message);
136PROTOBUF_EXPORT Status DataLossError(StringPiece message);
137PROTOBUF_EXPORT Status DeadlineExceededError(StringPiece message);
138PROTOBUF_EXPORT Status FailedPreconditionError(StringPiece message);
139PROTOBUF_EXPORT Status InternalError(StringPiece message);
140PROTOBUF_EXPORT Status InvalidArgumentError(StringPiece message);
141PROTOBUF_EXPORT Status NotFoundError(StringPiece message);
142PROTOBUF_EXPORT Status OutOfRangeError(StringPiece message);
143PROTOBUF_EXPORT Status PermissionDeniedError(StringPiece message);
144PROTOBUF_EXPORT Status ResourceExhaustedError(StringPiece message);
145PROTOBUF_EXPORT Status UnauthenticatedError(StringPiece message);
146PROTOBUF_EXPORT Status UnavailableError(StringPiece message);
147PROTOBUF_EXPORT Status UnimplementedError(StringPiece message);
148PROTOBUF_EXPORT Status UnknownError(StringPiece message);
149
150} // namespace status_internal
151
152using ::google::protobuf::util::status_internal::Status;
153using ::google::protobuf::util::status_internal::StatusCode;
154
155using ::google::protobuf::util::status_internal::IsAborted;
156using ::google::protobuf::util::status_internal::IsAlreadyExists;
157using ::google::protobuf::util::status_internal::IsCancelled;
158using ::google::protobuf::util::status_internal::IsDataLoss;
159using ::google::protobuf::util::status_internal::IsDeadlineExceeded;
160using ::google::protobuf::util::status_internal::IsFailedPrecondition;
161using ::google::protobuf::util::status_internal::IsInternal;
162using ::google::protobuf::util::status_internal::IsInvalidArgument;
163using ::google::protobuf::util::status_internal::IsNotFound;
164using ::google::protobuf::util::status_internal::IsOutOfRange;
165using ::google::protobuf::util::status_internal::IsPermissionDenied;
166using ::google::protobuf::util::status_internal::IsResourceExhausted;
167using ::google::protobuf::util::status_internal::IsUnauthenticated;
168using ::google::protobuf::util::status_internal::IsUnavailable;
169using ::google::protobuf::util::status_internal::IsUnimplemented;
170using ::google::protobuf::util::status_internal::IsUnknown;
171
172using ::google::protobuf::util::status_internal::AbortedError;
173using ::google::protobuf::util::status_internal::AlreadyExistsError;
174using ::google::protobuf::util::status_internal::CancelledError;
175using ::google::protobuf::util::status_internal::DataLossError;
176using ::google::protobuf::util::status_internal::DeadlineExceededError;
177using ::google::protobuf::util::status_internal::FailedPreconditionError;
178using ::google::protobuf::util::status_internal::InternalError;
179using ::google::protobuf::util::status_internal::InvalidArgumentError;
180using ::google::protobuf::util::status_internal::NotFoundError;
181using ::google::protobuf::util::status_internal::OkStatus;
182using ::google::protobuf::util::status_internal::OutOfRangeError;
183using ::google::protobuf::util::status_internal::PermissionDeniedError;
184using ::google::protobuf::util::status_internal::ResourceExhaustedError;
185using ::google::protobuf::util::status_internal::UnauthenticatedError;
186using ::google::protobuf::util::status_internal::UnavailableError;
187using ::google::protobuf::util::status_internal::UnimplementedError;
188using ::google::protobuf::util::status_internal::UnknownError;
189
190} // namespace util
191} // namespace protobuf
192} // namespace google
193
194#include <google/protobuf/port_undef.inc>
195
196#endif // GOOGLE_PROTOBUF_STUBS_STATUS_H_
197