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_TRANSPORT_TTRANSPORTEXCEPTION_H_
21 #define _THRIFT_TRANSPORT_TTRANSPORTEXCEPTION_H_ 1
22 
23 #include <string>
24 
25 #include <thrift/numeric_cast.h>
26 #include <thrift/Thrift.h>
27 
28 namespace apache {
29 namespace thrift {
30 namespace transport {
31 
32 /**
33  * Class to encapsulate all the possible types of transport errors that may
34  * occur in various transport systems. This provides a sort of generic
35  * wrapper around the vague UNIX E_ error codes that lets a common code
36  * base of error handling to be used for various types of transports, i.e.
37  * pipes etc.
38  *
39  */
40 class TTransportException : public apache::thrift::TException {
41 public:
42   /**
43    * Error codes for the various types of exceptions.
44    */
45   enum TTransportExceptionType {
46     UNKNOWN = 0,
47     NOT_OPEN = 1,
48     TIMED_OUT = 2,
49     END_OF_FILE = 3,
50     INTERRUPTED = 4,
51     BAD_ARGS = 5,
52     CORRUPTED_DATA = 6,
53     INTERNAL_ERROR = 7,
54     CLIENT_DISCONNECT = 8
55   };
56 
TTransportException()57   TTransportException() : apache::thrift::TException(), type_(UNKNOWN) {}
58 
TTransportException(TTransportExceptionType type)59   TTransportException(TTransportExceptionType type) : apache::thrift::TException(), type_(type) {}
60 
TTransportException(const std::string & message)61   TTransportException(const std::string& message)
62     : apache::thrift::TException(message), type_(UNKNOWN) {}
63 
TTransportException(TTransportExceptionType type,const std::string & message)64   TTransportException(TTransportExceptionType type, const std::string& message)
65     : apache::thrift::TException(message), type_(type) {}
66 
TTransportException(TTransportExceptionType type,const std::string & message,int errno_copy)67   TTransportException(TTransportExceptionType type, const std::string& message, int errno_copy)
68     : apache::thrift::TException(message + ": " + TOutput::strerror_s(errno_copy)), type_(type) {}
69 
70   ~TTransportException() noexcept override = default;
71 
72   /**
73    * Returns an error code that provides information about the type of error
74    * that has occurred.
75    *
76    * @return Error code
77    */
getType()78   TTransportExceptionType getType() const noexcept { return type_; }
79 
80   const char* what() const noexcept override;
81 
82 protected:
83   /** Just like strerror_r but returns a C++ string object. */
84   std::string strerror_s(int errno_copy);
85 
86   /** Error code */
87   TTransportExceptionType type_;
88 };
89 
90 /**
91  * Legacy code in transport implementations have overflow issues
92  * that need to be enforced.
93  */
safe_numeric_cast(From i)94 template <typename To, typename From> To safe_numeric_cast(From i) {
95   try {
96     return apache::thrift::numeric_cast<To>(i);
97   }
98   catch (const std::bad_cast& bc) {
99     throw TTransportException(TTransportException::CORRUPTED_DATA,
100                               bc.what());
101   }
102 }
103 
104 }
105 }
106 } // apache::thrift::transport
107 
108 #endif // #ifndef _THRIFT_TRANSPORT_TTRANSPORTEXCEPTION_H_
109