• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2019 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 ICING_TEXT_CLASSIFIER_LIB3_UTILS_BASE_STATUS_H_
16 #define ICING_TEXT_CLASSIFIER_LIB3_UTILS_BASE_STATUS_H_
17 
18 #include <string>
19 
20 #include "icing/text_classifier/lib3/utils/base/logging.h"
21 
22 namespace libtextclassifier3 {
23 
24 enum class StatusCode {
25   // Not an error; returned on success
26   OK = 0,
27 
28   // All of the following StatusCodes represent errors.
29   CANCELLED = 1,
30   UNKNOWN = 2,
31   INVALID_ARGUMENT = 3,
32   DEADLINE_EXCEEDED = 4,
33   NOT_FOUND = 5,
34   ALREADY_EXISTS = 6,
35   PERMISSION_DENIED = 7,
36   RESOURCE_EXHAUSTED = 8,
37   FAILED_PRECONDITION = 9,
38   ABORTED = 10,
39   OUT_OF_RANGE = 11,
40   UNIMPLEMENTED = 12,
41   INTERNAL = 13,
42   UNAVAILABLE = 14,
43   DATA_LOSS = 15,
44   UNAUTHENTICATED = 16
45 };
46 
47 // A Status is a combination of an error code and a string message (for non-OK
48 // error codes).
49 class Status {
50  public:
51   // Creates an OK status
52   Status();
53 
54   // Make a Status from the specified error and message.
55   Status(StatusCode error, const std::string& error_message);
56 
57   // Some pre-defined Status objects
58   static const Status& OK;
59   static const Status& UNKNOWN;
60 
61   // Accessors
ok()62   bool ok() const { return code_ == StatusCode::OK; }
error_code()63   int error_code() const { return static_cast<int>(code_); }
64 
CanonicalCode()65   StatusCode CanonicalCode() const { return code_; }
66 
error_message()67   const std::string& error_message() const { return message_; }
68 
69   // Noop function provided to allow callers to suppress compiler warnings about
70   // ignored return values.
IgnoreError()71   void IgnoreError() const {}
72 
73   bool operator==(const Status& x) const;
74   bool operator!=(const Status& x) const;
75 
76   std::string ToString() const;
77 
78  private:
79   StatusCode code_;
80   std::string message_;
81 };
82 
83 logging::LoggingStringStream& operator<<(logging::LoggingStringStream& stream,
84                                          const Status& status);
85 
86 }  // namespace libtextclassifier3
87 
88 #endif  // ICING_TEXT_CLASSIFIER_LIB3_UTILS_BASE_STATUS_H_
89