Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-17 08:35:03

0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one
0003  * or more contributor license agreements. See the NOTICE file
0004  * distributed with this work for additional information
0005  * regarding copyright ownership. The ASF licenses this file
0006  * to you under the Apache License, Version 2.0 (the
0007  * "License"); you may not use this file except in compliance
0008  * with the License. You may obtain a copy of the License at
0009  *
0010  *   http://www.apache.org/licenses/LICENSE-2.0
0011  *
0012  * Unless required by applicable law or agreed to in writing,
0013  * software distributed under the License is distributed on an
0014  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0015  * KIND, either express or implied. See the License for the
0016  * specific language governing permissions and limitations
0017  * under the License.
0018  */
0019 
0020 #ifndef _THRIFT_TRANSPORT_TTRANSPORTEXCEPTION_H_
0021 #define _THRIFT_TRANSPORT_TTRANSPORTEXCEPTION_H_ 1
0022 
0023 #include <boost/numeric/conversion/cast.hpp>
0024 #include <string>
0025 #include <thrift/Thrift.h>
0026 
0027 namespace apache {
0028 namespace thrift {
0029 namespace transport {
0030 
0031 /**
0032  * Class to encapsulate all the possible types of transport errors that may
0033  * occur in various transport systems. This provides a sort of generic
0034  * wrapper around the vague UNIX E_ error codes that lets a common code
0035  * base of error handling to be used for various types of transports, i.e.
0036  * pipes etc.
0037  *
0038  */
0039 class TTransportException : public apache::thrift::TException {
0040 public:
0041   /**
0042    * Error codes for the various types of exceptions.
0043    */
0044   enum TTransportExceptionType {
0045     UNKNOWN = 0,
0046     NOT_OPEN = 1,
0047     TIMED_OUT = 2,
0048     END_OF_FILE = 3,
0049     INTERRUPTED = 4,
0050     BAD_ARGS = 5,
0051     CORRUPTED_DATA = 6,
0052     INTERNAL_ERROR = 7,
0053     CLIENT_DISCONNECT = 8
0054   };
0055 
0056   TTransportException() : apache::thrift::TException(), type_(UNKNOWN) {}
0057 
0058   TTransportException(TTransportExceptionType type) : apache::thrift::TException(), type_(type) {}
0059 
0060   TTransportException(const std::string& message)
0061     : apache::thrift::TException(message), type_(UNKNOWN) {}
0062 
0063   TTransportException(TTransportExceptionType type, const std::string& message)
0064     : apache::thrift::TException(message), type_(type) {}
0065 
0066   TTransportException(TTransportExceptionType type, const std::string& message, int errno_copy)
0067     : apache::thrift::TException(message + ": " + TOutput::strerror_s(errno_copy)), type_(type) {}
0068 
0069   ~TTransportException() noexcept override = default;
0070 
0071   /**
0072    * Returns an error code that provides information about the type of error
0073    * that has occurred.
0074    *
0075    * @return Error code
0076    */
0077   TTransportExceptionType getType() const noexcept { return type_; }
0078 
0079   const char* what() const noexcept override;
0080 
0081 protected:
0082   /** Just like strerror_r but returns a C++ string object. */
0083   std::string strerror_s(int errno_copy);
0084 
0085   /** Error code */
0086   TTransportExceptionType type_;
0087 };
0088 
0089 /**
0090  * Legacy code in transport implementations have overflow issues
0091  * that need to be enforced.
0092  */
0093 template <typename To, typename From> To safe_numeric_cast(From i) {
0094   try {
0095     return boost::numeric_cast<To>(i);
0096   }
0097   catch (const std::bad_cast& bc) {
0098     throw TTransportException(TTransportException::CORRUPTED_DATA,
0099                               bc.what());
0100   }
0101 }
0102 
0103 }
0104 }
0105 } // apache::thrift::transport
0106 
0107 #endif // #ifndef _THRIFT_TRANSPORT_TTRANSPORTEXCEPTION_H_