blob: 60618fbe83c10555a1c6b2b7fe12feca2534da62 [file] [log] [blame]
David Reissac110e42010-03-09 05:20:07 +00001/*
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
Roger Meier49ff8b12012-04-13 09:12:31 +000023#include <thrift/Thrift.h>
David Reissac110e42010-03-09 05:20:07 +000024
Konrad Grochowski16a23a62014-11-13 15:33:38 +010025namespace apache {
26namespace thrift {
David Reissac110e42010-03-09 05:20:07 +000027
28namespace protocol {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010029class TProtocol;
David Reissac110e42010-03-09 05:20:07 +000030}
31
32class TApplicationException : public TException {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010033public:
David Reissac110e42010-03-09 05:20:07 +000034 /**
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,
Roger Meier345ecc72011-08-03 09:49:27 +000043 MISSING_RESULT = 5,
44 INTERNAL_ERROR = 6,
Roger Meier01931492012-12-22 21:31:03 +010045 PROTOCOL_ERROR = 7,
46 INVALID_TRANSFORM = 8,
47 INVALID_PROTOCOL = 9,
48 UNSUPPORTED_CLIENT_TYPE = 10
David Reissac110e42010-03-09 05:20:07 +000049 };
50
Konrad Grochowski16a23a62014-11-13 15:33:38 +010051 TApplicationException() : TException(), type_(UNKNOWN) {}
David Reissac110e42010-03-09 05:20:07 +000052
Konrad Grochowski16a23a62014-11-13 15:33:38 +010053 TApplicationException(TApplicationExceptionType type) : TException(), type_(type) {}
David Reissac110e42010-03-09 05:20:07 +000054
Konrad Grochowski16a23a62014-11-13 15:33:38 +010055 TApplicationException(const std::string& message) : TException(message), type_(UNKNOWN) {}
David Reissac110e42010-03-09 05:20:07 +000056
Konrad Grochowski16a23a62014-11-13 15:33:38 +010057 TApplicationException(TApplicationExceptionType type, const std::string& message)
58 : TException(message), type_(type) {}
David Reissac110e42010-03-09 05:20:07 +000059
cyy7b935592019-01-05 10:04:25 +080060 virtual ~TApplicationException() noexcept {}
David Reissac110e42010-03-09 05:20:07 +000061
62 /**
63 * Returns an error code that provides information about the type of error
64 * that has occurred.
65 *
66 * @return Error code
67 */
tpcwangd42d8be2016-03-24 09:56:10 -070068 TApplicationExceptionType getType() const { return type_; }
David Reissac110e42010-03-09 05:20:07 +000069
cyy7b935592019-01-05 10:04:25 +080070 virtual const char* what() const noexcept {
David Reissac110e42010-03-09 05:20:07 +000071 if (message_.empty()) {
72 switch (type_) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010073 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)";
David Reissac110e42010-03-09 05:20:07 +000097 };
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
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100106protected:
David Reissac110e42010-03-09 05:20:07 +0000107 /**
108 * Error code
109 */
110 TApplicationExceptionType type_;
David Reissac110e42010-03-09 05:20:07 +0000111};
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100112}
113} // apache::thrift
David Reissac110e42010-03-09 05:20:07 +0000114
115#endif // #ifndef _THRIFT_TAPPLICATIONEXCEPTION_H_