TApplicationException.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. #ifndef _THRIFT_TAPPLICATIONEXCEPTION_H_
  20. #define _THRIFT_TAPPLICATIONEXCEPTION_H_ 1
  21. #include <thrift/Thrift.h>
  22. namespace apache {
  23. namespace thrift {
  24. namespace protocol {
  25. class TProtocol;
  26. }
  27. class TApplicationException : public TException {
  28. public:
  29. /**
  30. * Error codes for the various types of exceptions.
  31. */
  32. enum TApplicationExceptionType {
  33. UNKNOWN = 0,
  34. UNKNOWN_METHOD = 1,
  35. INVALID_MESSAGE_TYPE = 2,
  36. WRONG_METHOD_NAME = 3,
  37. BAD_SEQUENCE_ID = 4,
  38. MISSING_RESULT = 5,
  39. INTERNAL_ERROR = 6,
  40. PROTOCOL_ERROR = 7,
  41. INVALID_TRANSFORM = 8,
  42. INVALID_PROTOCOL = 9,
  43. UNSUPPORTED_CLIENT_TYPE = 10
  44. };
  45. TApplicationException() : TException(), type_(UNKNOWN) {}
  46. TApplicationException(TApplicationExceptionType type) : TException(), type_(type) {}
  47. TApplicationException(const std::string& message) : TException(message), type_(UNKNOWN) {}
  48. TApplicationException(TApplicationExceptionType type, const std::string& message)
  49. : TException(message), type_(type) {}
  50. virtual ~TApplicationException() throw() {}
  51. /**
  52. * Returns an error code that provides information about the type of error
  53. * that has occurred.
  54. *
  55. * @return Error code
  56. */
  57. TApplicationExceptionType getType() const { return type_; }
  58. virtual const char* what() const throw() {
  59. if (message_.empty()) {
  60. switch (type_) {
  61. case UNKNOWN:
  62. return "TApplicationException: Unknown application exception";
  63. case UNKNOWN_METHOD:
  64. return "TApplicationException: Unknown method";
  65. case INVALID_MESSAGE_TYPE:
  66. return "TApplicationException: Invalid message type";
  67. case WRONG_METHOD_NAME:
  68. return "TApplicationException: Wrong method name";
  69. case BAD_SEQUENCE_ID:
  70. return "TApplicationException: Bad sequence identifier";
  71. case MISSING_RESULT:
  72. return "TApplicationException: Missing result";
  73. case INTERNAL_ERROR:
  74. return "TApplicationException: Internal error";
  75. case PROTOCOL_ERROR:
  76. return "TApplicationException: Protocol error";
  77. case INVALID_TRANSFORM:
  78. return "TApplicationException: Invalid transform";
  79. case INVALID_PROTOCOL:
  80. return "TApplicationException: Invalid protocol";
  81. case UNSUPPORTED_CLIENT_TYPE:
  82. return "TApplicationException: Unsupported client type";
  83. default:
  84. return "TApplicationException: (Invalid exception type)";
  85. };
  86. } else {
  87. return message_.c_str();
  88. }
  89. }
  90. uint32_t read(protocol::TProtocol* iprot);
  91. uint32_t write(protocol::TProtocol* oprot) const;
  92. protected:
  93. /**
  94. * Error code
  95. */
  96. TApplicationExceptionType type_;
  97. };
  98. }
  99. } // apache::thrift
  100. #endif // #ifndef _THRIFT_TAPPLICATIONEXCEPTION_H_