blob: 0532ec55a2ef6ca5f81365bd145e3019e775a505 [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// 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/
6
Mark Sleef9831082007-02-20 20:59:21 +00007#ifndef _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_
8#define _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_ 1
9
10#include <boost/lexical_cast.hpp>
11#include <string>
12
David Reiss0c90f6f2008-02-06 22:18:40 +000013namespace facebook { namespace thrift { namespace protocol {
Mark Sleef9831082007-02-20 20:59:21 +000014
15/**
16 * Class to encapsulate all the possible types of protocol errors that may
17 * occur in various protocol systems. This provides a sort of generic
18 * wrapper around the shitty UNIX E_ error codes that lets a common code
19 * base of error handling to be used for various types of protocols, i.e.
20 * pipes etc.
21 *
22 * @author Mark Slee <mcslee@facebook.com>
23 */
24class TProtocolException : public facebook::thrift::TException {
25 public:
26
27 /**
28 * Error codes for the various types of exceptions.
29 */
30 enum TProtocolExceptionType {
31 UNKNOWN = 0,
32 INVALID_DATA = 1,
33 NEGATIVE_SIZE = 2,
Mark Slee808454e2007-06-20 21:51:57 +000034 SIZE_LIMIT = 3,
35 BAD_VERSION = 4,
David Reiss00dcccf2007-07-21 01:18:10 +000036 NOT_IMPLEMENTED = 5,
Mark Sleef9831082007-02-20 20:59:21 +000037 };
38
39 TProtocolException() :
40 facebook::thrift::TException(),
41 type_(UNKNOWN) {}
42
43 TProtocolException(TProtocolExceptionType type) :
David Reiss0c90f6f2008-02-06 22:18:40 +000044 facebook::thrift::TException(),
Mark Sleef9831082007-02-20 20:59:21 +000045 type_(type) {}
46
Mark Slee82a6c0f2007-04-04 21:08:21 +000047 TProtocolException(const std::string& message) :
Mark Sleef9831082007-02-20 20:59:21 +000048 facebook::thrift::TException(message),
49 type_(UNKNOWN) {}
50
Mark Slee82a6c0f2007-04-04 21:08:21 +000051 TProtocolException(TProtocolExceptionType type, const std::string& message) :
Mark Sleef9831082007-02-20 20:59:21 +000052 facebook::thrift::TException(message),
53 type_(type) {}
54
55 virtual ~TProtocolException() throw() {}
56
57 /**
58 * Returns an error code that provides information about the type of error
59 * that has occurred.
60 *
61 * @return Error code
62 */
63 TProtocolExceptionType getType() {
64 return type_;
65 }
66
67 virtual const char* what() const throw() {
68 if (message_.empty()) {
David Reissadad4ab2007-12-14 20:56:04 +000069 switch (type_) {
70 case UNKNOWN : return "TProtocolException: Unknown protocol exception";
71 case INVALID_DATA : return "TProtocolException: Invalid data";
72 case NEGATIVE_SIZE : return "TProtocolException: Negative size";
73 case SIZE_LIMIT : return "TProtocolException: Exceeded size limit";
74 case BAD_VERSION : return "TProtocolException: Invalid version";
75 case NOT_IMPLEMENTED : return "TProtocolException: Not implemented";
76 default : return "TProtocolException: (Invalid exception type)";
77 }
Mark Sleef9831082007-02-20 20:59:21 +000078 } else {
79 return message_.c_str();
80 }
81 }
82
83 protected:
David Reiss0c90f6f2008-02-06 22:18:40 +000084 /**
Mark Sleef9831082007-02-20 20:59:21 +000085 * Error code
86 */
87 TProtocolExceptionType type_;
David Reiss0c90f6f2008-02-06 22:18:40 +000088
Mark Sleef9831082007-02-20 20:59:21 +000089};
90
91}}} // facebook::thrift::protocol
92
93#endif // #ifndef _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_