status.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef LIBHDFSPP_STATUS_H_
  19. #define LIBHDFSPP_STATUS_H_
  20. #include <string>
  21. #include <system_error>
  22. namespace hdfs {
  23. class Status {
  24. public:
  25. // Create a success status.
  26. Status() : code_(0) {};
  27. // Note: Avoid calling the Status constructors directly, call the factory methods instead
  28. // Used for common status types
  29. Status(int code, const char *msg);
  30. // Used for server side exceptions reported through RpcResponseProto and similar
  31. Status(int code, const char *exception_class, const char *exception_details);
  32. // Factory methods
  33. static Status OK();
  34. static Status InvalidArgument(const char *msg);
  35. static Status ResourceUnavailable(const char *msg);
  36. static Status Unimplemented();
  37. static Status Exception(const char *exception_class_name, const char *exception_details);
  38. static Status Error(const char *error_message);
  39. static Status AuthenticationFailed();
  40. static Status AuthenticationFailed(const char *msg);
  41. static Status Canceled();
  42. static Status PathNotFound(const char *msg);
  43. static Status InvalidOffset(const char *msg);
  44. static Status PathIsNotDirectory(const char *msg);
  45. // success
  46. bool ok() const { return code_ == 0; }
  47. bool is_invalid_offset() const { return code_ == kInvalidOffset; }
  48. // contains ENOENT error
  49. bool pathNotFound() const { return code_ == kPathNotFound; }
  50. // Returns the string "OK" for success.
  51. std::string ToString() const;
  52. // get error code
  53. int code() const { return code_; }
  54. // if retry can possibly recover an error
  55. bool notWorthRetry() const;
  56. enum Code {
  57. kOk = 0,
  58. kInvalidArgument = static_cast<unsigned>(std::errc::invalid_argument),
  59. kResourceUnavailable = static_cast<unsigned>(std::errc::resource_unavailable_try_again),
  60. kUnimplemented = static_cast<unsigned>(std::errc::function_not_supported),
  61. kOperationCanceled = static_cast<unsigned>(std::errc::operation_canceled),
  62. kPermissionDenied = static_cast<unsigned>(std::errc::permission_denied),
  63. kPathNotFound = static_cast<unsigned>(std::errc::no_such_file_or_directory),
  64. kNotADirectory = static_cast<unsigned>(std::errc::not_a_directory),
  65. kFileAlreadyExists = static_cast<unsigned>(std::errc::file_exists),
  66. kPathIsNotEmptyDirectory = static_cast<unsigned>(std::errc::directory_not_empty),
  67. // non-errc codes start at 256
  68. kException = 256,
  69. kAuthenticationFailed = 257,
  70. kAccessControlException = 258,
  71. kStandbyException = 259,
  72. kSnapshotProtocolException = 260,
  73. kInvalidOffset = 261,
  74. };
  75. std::string get_exception_class_str() const {
  76. return exception_class_;
  77. }
  78. int get_server_exception_type() const {
  79. return code_;
  80. }
  81. private:
  82. int code_;
  83. std::string msg_;
  84. std::string exception_class_;
  85. };
  86. }
  87. #endif