r1403@opsdev009 (orig r74388): dreiss | 2007-12-14 12:56:04 -0800
[amiethrift.git] / lib / cpp / src / transport / TTransportException.h
blobc21719b28e331d4aace400a6102d935cfe6f087b
1 // Copyright (c) 2006- Facebook
2 // Distributed under the Thrift Software License
3 //
4 // See accompanying file LICENSE or visit the Thrift site at:
5 // http://developers.facebook.com/thrift/
7 #ifndef _THRIFT_TRANSPORT_TTRANSPORTEXCEPTION_H_
8 #define _THRIFT_TRANSPORT_TTRANSPORTEXCEPTION_H_ 1
10 #include <boost/lexical_cast.hpp>
11 #include <string>
12 #include <Thrift.h>
14 namespace facebook { namespace thrift { namespace transport {
16 /**
17 * Class to encapsulate all the possible types of transport errors that may
18 * occur in various transport systems. This provides a sort of generic
19 * wrapper around the shitty UNIX E_ error codes that lets a common code
20 * base of error handling to be used for various types of transports, i.e.
21 * pipes etc.
23 * @author Mark Slee <mcslee@facebook.com>
25 class TTransportException : public facebook::thrift::TException {
26 public:
27 /**
28 * Error codes for the various types of exceptions.
30 enum TTransportExceptionType {
31 UNKNOWN = 0,
32 NOT_OPEN = 1,
33 ALREADY_OPEN = 2,
34 TIMED_OUT = 3,
35 END_OF_FILE = 4,
36 INTERRUPTED = 5,
37 BAD_ARGS = 6,
38 CORRUPTED_DATA = 7,
39 INTERNAL_ERROR = 8,
42 TTransportException() :
43 facebook::thrift::TException(),
44 type_(UNKNOWN) {}
46 TTransportException(TTransportExceptionType type) :
47 facebook::thrift::TException(),
48 type_(type) {}
50 TTransportException(const std::string& message) :
51 facebook::thrift::TException(message),
52 type_(UNKNOWN) {}
54 TTransportException(TTransportExceptionType type, const std::string& message) :
55 facebook::thrift::TException(message),
56 type_(type) {}
58 TTransportException(TTransportExceptionType type,
59 const std::string& message,
60 int errno_copy) :
61 facebook::thrift::TException(message + ": " + strerror_s(errno_copy)),
62 type_(type) {}
64 virtual ~TTransportException() throw() {}
66 /**
67 * Returns an error code that provides information about the type of error
68 * that has occurred.
70 * @return Error code
72 TTransportExceptionType getType() {
73 return type_;
76 virtual const char* what() const throw() {
77 if (message_.empty()) {
78 switch (type_) {
79 case UNKNOWN : return "TTransportException: Unknown transport exception";
80 case NOT_OPEN : return "TTransportException: Transport not open";
81 case ALREADY_OPEN : return "TTransportException: Transport already open";
82 case TIMED_OUT : return "TTransportException: Timed out";
83 case END_OF_FILE : return "TTransportException: End of file";
84 case INTERRUPTED : return "TTransportException: Interrupted";
85 case BAD_ARGS : return "TTransportException: Invalid arguments";
86 case CORRUPTED_DATA : return "TTransportException: Corrupted Data";
87 case INTERNAL_ERROR : return "TTransportException: Internal error";
88 default : return "TTransportException: (Invalid exception type)";
90 } else {
91 return message_.c_str();
95 protected:
96 /** Just like strerror_r but returns a C++ string object. */
97 std::string strerror_s(int errno_copy);
99 /** Error code */
100 TTransportExceptionType type_;
104 }}} // facebook::thrift::transport
106 #endif // #ifndef _THRIFT_TRANSPORT_TTRANSPORTEXCEPTION_H_