status.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #ifndef GOOGLE_PROTOBUF_STUBS_STATUS_H_
  31. #define GOOGLE_PROTOBUF_STUBS_STATUS_H_
  32. #include <iosfwd>
  33. #include <string>
  34. #include <google/protobuf/stubs/common.h>
  35. #include <google/protobuf/stubs/stringpiece.h>
  36. namespace google {
  37. namespace protobuf {
  38. namespace util {
  39. namespace error {
  40. // These values must match error codes defined in google/rpc/code.proto.
  41. enum Code {
  42. OK = 0,
  43. CANCELLED = 1,
  44. UNKNOWN = 2,
  45. INVALID_ARGUMENT = 3,
  46. DEADLINE_EXCEEDED = 4,
  47. NOT_FOUND = 5,
  48. ALREADY_EXISTS = 6,
  49. PERMISSION_DENIED = 7,
  50. UNAUTHENTICATED = 16,
  51. RESOURCE_EXHAUSTED = 8,
  52. FAILED_PRECONDITION = 9,
  53. ABORTED = 10,
  54. OUT_OF_RANGE = 11,
  55. UNIMPLEMENTED = 12,
  56. INTERNAL = 13,
  57. UNAVAILABLE = 14,
  58. DATA_LOSS = 15,
  59. };
  60. } // namespace error
  61. class LIBPROTOBUF_EXPORT Status {
  62. public:
  63. // Creates a "successful" status.
  64. Status();
  65. // Create a status in the canonical error space with the specified
  66. // code, and error message. If "code == 0", error_message is
  67. // ignored and a Status object identical to Status::OK is
  68. // constructed.
  69. Status(error::Code error_code, StringPiece error_message);
  70. Status(const Status&);
  71. Status& operator=(const Status& x);
  72. ~Status() {}
  73. // Some pre-defined Status objects
  74. static const Status OK; // Identical to 0-arg constructor
  75. static const Status CANCELLED;
  76. static const Status UNKNOWN;
  77. // Accessor
  78. bool ok() const {
  79. return error_code_ == error::OK;
  80. }
  81. int error_code() const {
  82. return error_code_;
  83. }
  84. StringPiece error_message() const {
  85. return error_message_;
  86. }
  87. bool operator==(const Status& x) const;
  88. bool operator!=(const Status& x) const {
  89. return !operator==(x);
  90. }
  91. // Return a combination of the error code name and message.
  92. string ToString() const;
  93. private:
  94. error::Code error_code_;
  95. string error_message_;
  96. };
  97. // Prints a human-readable representation of 'x' to 'os'.
  98. LIBPROTOBUF_EXPORT std::ostream& operator<<(std::ostream& os, const Status& x);
  99. #define EXPECT_OK(value) EXPECT_TRUE((value).ok())
  100. } // namespace util
  101. } // namespace protobuf
  102. } // namespace google
  103. #endif // GOOGLE_PROTOBUF_STUBS_STATUS_H_