blob: be0767be5100cff11e7b71df994b485a8baac588 [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
Mark Sleef9831082007-02-20 20:59:21 +000010#include <string>
11
T Jake Lucianib5e62212009-01-31 22:36:20 +000012namespace apache { namespace thrift { namespace protocol {
Mark Sleef9831082007-02-20 20:59:21 +000013
14/**
15 * Class to encapsulate all the possible types of protocol errors that may
16 * occur in various protocol systems. This provides a sort of generic
17 * wrapper around the shitty UNIX E_ error codes that lets a common code
18 * base of error handling to be used for various types of protocols, i.e.
19 * pipes etc.
20 *
21 * @author Mark Slee <mcslee@facebook.com>
22 */
T Jake Lucianib5e62212009-01-31 22:36:20 +000023class TProtocolException : public apache::thrift::TException {
Mark Sleef9831082007-02-20 20:59:21 +000024 public:
25
26 /**
27 * Error codes for the various types of exceptions.
28 */
David Reiss322e5952008-12-05 02:54:09 +000029 enum TProtocolExceptionType
30 { UNKNOWN = 0
31 , INVALID_DATA = 1
32 , NEGATIVE_SIZE = 2
33 , SIZE_LIMIT = 3
34 , BAD_VERSION = 4
35 , NOT_IMPLEMENTED = 5
Mark Sleef9831082007-02-20 20:59:21 +000036 };
37
38 TProtocolException() :
T Jake Lucianib5e62212009-01-31 22:36:20 +000039 apache::thrift::TException(),
Mark Sleef9831082007-02-20 20:59:21 +000040 type_(UNKNOWN) {}
41
42 TProtocolException(TProtocolExceptionType type) :
T Jake Lucianib5e62212009-01-31 22:36:20 +000043 apache::thrift::TException(),
Mark Sleef9831082007-02-20 20:59:21 +000044 type_(type) {}
45
Mark Slee82a6c0f2007-04-04 21:08:21 +000046 TProtocolException(const std::string& message) :
T Jake Lucianib5e62212009-01-31 22:36:20 +000047 apache::thrift::TException(message),
Mark Sleef9831082007-02-20 20:59:21 +000048 type_(UNKNOWN) {}
49
Mark Slee82a6c0f2007-04-04 21:08:21 +000050 TProtocolException(TProtocolExceptionType type, const std::string& message) :
T Jake Lucianib5e62212009-01-31 22:36:20 +000051 apache::thrift::TException(message),
Mark Sleef9831082007-02-20 20:59:21 +000052 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()) {
David Reissadad4ab2007-12-14 20:56:04 +000068 switch (type_) {
69 case UNKNOWN : return "TProtocolException: Unknown protocol exception";
70 case INVALID_DATA : return "TProtocolException: Invalid data";
71 case NEGATIVE_SIZE : return "TProtocolException: Negative size";
72 case SIZE_LIMIT : return "TProtocolException: Exceeded size limit";
73 case BAD_VERSION : return "TProtocolException: Invalid version";
74 case NOT_IMPLEMENTED : return "TProtocolException: Not implemented";
75 default : return "TProtocolException: (Invalid exception type)";
76 }
Mark Sleef9831082007-02-20 20:59:21 +000077 } else {
78 return message_.c_str();
79 }
80 }
81
82 protected:
David Reiss0c90f6f2008-02-06 22:18:40 +000083 /**
Mark Sleef9831082007-02-20 20:59:21 +000084 * Error code
85 */
86 TProtocolExceptionType type_;
David Reiss0c90f6f2008-02-06 22:18:40 +000087
Mark Sleef9831082007-02-20 20:59:21 +000088};
89
T Jake Lucianib5e62212009-01-31 22:36:20 +000090}}} // apache::thrift::protocol
Mark Sleef9831082007-02-20 20:59:21 +000091
92#endif // #ifndef _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_