blob: 0728ce4e2fcf320bc247a6dc59a0926981dac6f5 [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 *
Mark Sleef9831082007-02-20 20:59:21 +000021 */
T Jake Lucianib5e62212009-01-31 22:36:20 +000022class TProtocolException : public apache::thrift::TException {
Mark Sleef9831082007-02-20 20:59:21 +000023 public:
24
25 /**
26 * Error codes for the various types of exceptions.
27 */
David Reiss322e5952008-12-05 02:54:09 +000028 enum TProtocolExceptionType
29 { UNKNOWN = 0
30 , INVALID_DATA = 1
31 , NEGATIVE_SIZE = 2
32 , SIZE_LIMIT = 3
33 , BAD_VERSION = 4
34 , NOT_IMPLEMENTED = 5
Mark Sleef9831082007-02-20 20:59:21 +000035 };
36
37 TProtocolException() :
T Jake Lucianib5e62212009-01-31 22:36:20 +000038 apache::thrift::TException(),
Mark Sleef9831082007-02-20 20:59:21 +000039 type_(UNKNOWN) {}
40
41 TProtocolException(TProtocolExceptionType type) :
T Jake Lucianib5e62212009-01-31 22:36:20 +000042 apache::thrift::TException(),
Mark Sleef9831082007-02-20 20:59:21 +000043 type_(type) {}
44
Mark Slee82a6c0f2007-04-04 21:08:21 +000045 TProtocolException(const std::string& message) :
T Jake Lucianib5e62212009-01-31 22:36:20 +000046 apache::thrift::TException(message),
Mark Sleef9831082007-02-20 20:59:21 +000047 type_(UNKNOWN) {}
48
Mark Slee82a6c0f2007-04-04 21:08:21 +000049 TProtocolException(TProtocolExceptionType type, const std::string& message) :
T Jake Lucianib5e62212009-01-31 22:36:20 +000050 apache::thrift::TException(message),
Mark Sleef9831082007-02-20 20:59:21 +000051 type_(type) {}
52
53 virtual ~TProtocolException() throw() {}
54
55 /**
56 * Returns an error code that provides information about the type of error
57 * that has occurred.
58 *
59 * @return Error code
60 */
61 TProtocolExceptionType getType() {
62 return type_;
63 }
64
65 virtual const char* what() const throw() {
66 if (message_.empty()) {
David Reissadad4ab2007-12-14 20:56:04 +000067 switch (type_) {
68 case UNKNOWN : return "TProtocolException: Unknown protocol exception";
69 case INVALID_DATA : return "TProtocolException: Invalid data";
70 case NEGATIVE_SIZE : return "TProtocolException: Negative size";
71 case SIZE_LIMIT : return "TProtocolException: Exceeded size limit";
72 case BAD_VERSION : return "TProtocolException: Invalid version";
73 case NOT_IMPLEMENTED : return "TProtocolException: Not implemented";
74 default : return "TProtocolException: (Invalid exception type)";
75 }
Mark Sleef9831082007-02-20 20:59:21 +000076 } else {
77 return message_.c_str();
78 }
79 }
80
81 protected:
David Reiss0c90f6f2008-02-06 22:18:40 +000082 /**
Mark Sleef9831082007-02-20 20:59:21 +000083 * Error code
84 */
85 TProtocolExceptionType type_;
David Reiss0c90f6f2008-02-06 22:18:40 +000086
Mark Sleef9831082007-02-20 20:59:21 +000087};
88
T Jake Lucianib5e62212009-01-31 22:36:20 +000089}}} // apache::thrift::protocol
Mark Sleef9831082007-02-20 20:59:21 +000090
91#endif // #ifndef _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_