blob: 2d542a0db4f62df89ea712b0dba1cd8fa7132491 [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,
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) :
44 facebook::thrift::TException(),
45 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()) {
69 return (std::string("Default Protocol Exception: ") +
70 boost::lexical_cast<std::string>(type_)).c_str();
71 } else {
72 return message_.c_str();
73 }
74 }
75
76 protected:
77 /**
78 * Error code
79 */
80 TProtocolExceptionType type_;
81
82};
83
84}}} // facebook::thrift::protocol
85
86#endif // #ifndef _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_