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 
20 #ifndef _THRIFT_TAPPLICATIONEXCEPTION_H_
21 #define _THRIFT_TAPPLICATIONEXCEPTION_H_ 1
22 
23 #include <thrift/Thrift.h>
24 
25 namespace apache {
26 namespace thrift {
27 
28 namespace protocol {
29 class TProtocol;
30 }
31 
32 class TApplicationException : public TException {
33 public:
34   /**
35    * Error codes for the various types of exceptions.
36    */
37   enum TApplicationExceptionType {
38     UNKNOWN = 0,
39     UNKNOWN_METHOD = 1,
40     INVALID_MESSAGE_TYPE = 2,
41     WRONG_METHOD_NAME = 3,
42     BAD_SEQUENCE_ID = 4,
43     MISSING_RESULT = 5,
44     INTERNAL_ERROR = 6,
45     PROTOCOL_ERROR = 7,
46     INVALID_TRANSFORM = 8,
47     INVALID_PROTOCOL = 9,
48     UNSUPPORTED_CLIENT_TYPE = 10
49   };
50 
TApplicationException()51   TApplicationException() : TException(), type_(UNKNOWN) {}
52 
TApplicationException(TApplicationExceptionType type)53   TApplicationException(TApplicationExceptionType type) : TException(), type_(type) {}
54 
TApplicationException(const std::string & message)55   TApplicationException(const std::string& message) : TException(message), type_(UNKNOWN) {}
56 
TApplicationException(TApplicationExceptionType type,const std::string & message)57   TApplicationException(TApplicationExceptionType type, const std::string& message)
58     : TException(message), type_(type) {}
59 
60   ~TApplicationException() noexcept override = default;
61 
62   /**
63    * Returns an error code that provides information about the type of error
64    * that has occurred.
65    *
66    * @return Error code
67    */
getType()68   TApplicationExceptionType getType() const { return type_; }
69 
what()70   const char* what() const noexcept override {
71     if (message_.empty()) {
72       switch (type_) {
73       case UNKNOWN:
74         return "TApplicationException: Unknown application exception";
75       case UNKNOWN_METHOD:
76         return "TApplicationException: Unknown method";
77       case INVALID_MESSAGE_TYPE:
78         return "TApplicationException: Invalid message type";
79       case WRONG_METHOD_NAME:
80         return "TApplicationException: Wrong method name";
81       case BAD_SEQUENCE_ID:
82         return "TApplicationException: Bad sequence identifier";
83       case MISSING_RESULT:
84         return "TApplicationException: Missing result";
85       case INTERNAL_ERROR:
86         return "TApplicationException: Internal error";
87       case PROTOCOL_ERROR:
88         return "TApplicationException: Protocol error";
89       case INVALID_TRANSFORM:
90         return "TApplicationException: Invalid transform";
91       case INVALID_PROTOCOL:
92         return "TApplicationException: Invalid protocol";
93       case UNSUPPORTED_CLIENT_TYPE:
94         return "TApplicationException: Unsupported client type";
95       default:
96         return "TApplicationException: (Invalid exception type)";
97       };
98     } else {
99       return message_.c_str();
100     }
101   }
102 
103   uint32_t read(protocol::TProtocol* iprot);
104   uint32_t write(protocol::TProtocol* oprot) const;
105 
106 protected:
107   /**
108    * Error code
109    */
110   TApplicationExceptionType type_;
111 };
112 }
113 } // apache::thrift
114 
115 #endif // #ifndef _THRIFT_TAPPLICATIONEXCEPTION_H_
116