status.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. // success
  43. bool ok() const { return code_ == 0; }
  44. // Returns the string "OK" for success.
  45. std::string ToString() const;
  46. // get error code
  47. int code() const { return code_; }
  48. enum Code {
  49. kOk = 0,
  50. kInvalidArgument = static_cast<unsigned>(std::errc::invalid_argument),
  51. kResourceUnavailable = static_cast<unsigned>(std::errc::resource_unavailable_try_again),
  52. kUnimplemented = static_cast<unsigned>(std::errc::function_not_supported),
  53. kOperationCanceled = static_cast<unsigned>(std::errc::operation_canceled),
  54. kPermissionDenied = static_cast<unsigned>(std::errc::permission_denied),
  55. kPathNotFound = static_cast<unsigned>(std::errc::no_such_file_or_directory),
  56. kNotADirectory = static_cast<unsigned>(std::errc::not_a_directory),
  57. kFileAlreadyExists = static_cast<unsigned>(std::errc::file_exists),
  58. kPathIsNotEmptyDirectory = static_cast<unsigned>(std::errc::directory_not_empty),
  59. // non-errc codes start at 256
  60. kException = 256,
  61. kAuthenticationFailed = 257,
  62. kAccessControlException = 258,
  63. kStandbyException = 259,
  64. kSnapshotProtocolException = 260,
  65. };
  66. std::string get_exception_class_str() const {
  67. return exception_class_;
  68. }
  69. int get_server_exception_type() const {
  70. return code_;
  71. }
  72. private:
  73. int code_;
  74. std::string msg_;
  75. std::string exception_class_;
  76. };
  77. }
  78. #endif