r1355@opsdev009 (orig r71477): mcslee | 2007-11-27 00:42:19 -0800
[amiethrift.git] / lib / cpp / src / transport / TTransportException.h
blob9b75826ec3cdcc72d7707417713d8b8e2bc6edf4
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 return (std::string("Default Transport Exception: ") +
79 boost::lexical_cast<std::string>(type_)).c_str();
80 } else {
81 return message_.c_str();
85 protected:
86 /** Just like strerror_r but returns a C++ string object. */
87 std::string strerror_s(int errno_copy);
89 /** Error code */
90 TTransportExceptionType type_;
94 }}} // facebook::thrift::transport
96 #endif // #ifndef _THRIFT_TRANSPORT_TTRANSPORTEXCEPTION_H_