status.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 Canceled();
  41. static Status PathNotFound(const char *msg);
  42. static Status InvalidOffset(const char *msg);
  43. static Status PathIsNotDirectory(const char *msg);
  44. // success
  45. bool ok() const { return code_ == 0; }
  46. bool is_invalid_offset() const { return code_ == kInvalidOffset; }
  47. // contains ENOENT error
  48. bool pathNotFound() const { return code_ == kPathNotFound; }
  49. // Returns the string "OK" for success.
  50. std::string ToString() const;
  51. // get error code
  52. int code() const { return code_; }
  53. // if retry can possibly recover an error
  54. bool notWorthRetry() const;
  55. enum Code {
  56. kOk = 0,
  57. kInvalidArgument = static_cast<unsigned>(std::errc::invalid_argument),
  58. kResourceUnavailable = static_cast<unsigned>(std::errc::resource_unavailable_try_again),
  59. kUnimplemented = static_cast<unsigned>(std::errc::function_not_supported),
  60. kOperationCanceled = static_cast<unsigned>(std::errc::operation_canceled),
  61. kPermissionDenied = static_cast<unsigned>(std::errc::permission_denied),
  62. kPathNotFound = static_cast<unsigned>(std::errc::no_such_file_or_directory),
  63. kNotADirectory = static_cast<unsigned>(std::errc::not_a_directory),
  64. kFileAlreadyExists = static_cast<unsigned>(std::errc::file_exists),
  65. kPathIsNotEmptyDirectory = static_cast<unsigned>(std::errc::directory_not_empty),
  66. // non-errc codes start at 256
  67. kException = 256,
  68. kAuthenticationFailed = 257,
  69. kAccessControlException = 258,
  70. kStandbyException = 259,
  71. kSnapshotProtocolException = 260,
  72. kInvalidOffset = 261,
  73. };
  74. std::string get_exception_class_str() const {
  75. return exception_class_;
  76. }
  77. int get_server_exception_type() const {
  78. return code_;
  79. }
  80. private:
  81. int code_;
  82. std::string msg_;
  83. std::string exception_class_;
  84. };
  85. }
  86. #endif