blob: 50959b705106dc1b1ac03fb331aa03b4b77248f7 [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
13namespace facebook { namespace thrift { namespace protocol {
14
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,
Mark Sleef9831082007-02-20 20:59:21 +000036 };
37
38 TProtocolException() :
39 facebook::thrift::TException(),
40 type_(UNKNOWN) {}
41
42 TProtocolException(TProtocolExceptionType type) :
43 facebook::thrift::TException(),
44 type_(type) {}
45
Mark Slee82a6c0f2007-04-04 21:08:21 +000046 TProtocolException(const std::string& message) :
Mark Sleef9831082007-02-20 20:59:21 +000047 facebook::thrift::TException(message),
48 type_(UNKNOWN) {}
49
Mark Slee82a6c0f2007-04-04 21:08:21 +000050 TProtocolException(TProtocolExceptionType type, const std::string& message) :
Mark Sleef9831082007-02-20 20:59:21 +000051 facebook::thrift::TException(message),
52 type_(type) {}
53
54 virtual ~TProtocolException() throw() {}
55
56 /**
57 * Returns an error code that provides information about the type of error
58 * that has occurred.
59 *
60 * @return Error code
61 */
62 TProtocolExceptionType getType() {
63 return type_;
64 }
65
66 virtual const char* what() const throw() {
67 if (message_.empty()) {
68 return (std::string("Default Protocol Exception: ") +
69 boost::lexical_cast<std::string>(type_)).c_str();
70 } else {
71 return message_.c_str();
72 }
73 }
74
75 protected:
76 /**
77 * Error code
78 */
79 TProtocolExceptionType type_;
80
81};
82
83}}} // facebook::thrift::protocol
84
85#endif // #ifndef _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_