blob: 8f939c6b07bca3fd3f74e19a5b6e85c4fad6305d [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,
34 SIZE_LIMIT = 3
35 };
36
37 TProtocolException() :
38 facebook::thrift::TException(),
39 type_(UNKNOWN) {}
40
41 TProtocolException(TProtocolExceptionType type) :
42 facebook::thrift::TException(),
43 type_(type) {}
44
Mark Slee82a6c0f2007-04-04 21:08:21 +000045 TProtocolException(const std::string& message) :
Mark Sleef9831082007-02-20 20:59:21 +000046 facebook::thrift::TException(message),
47 type_(UNKNOWN) {}
48
Mark Slee82a6c0f2007-04-04 21:08:21 +000049 TProtocolException(TProtocolExceptionType type, const std::string& message) :
Mark Sleef9831082007-02-20 20:59:21 +000050 facebook::thrift::TException(message),
51 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()) {
67 return (std::string("Default Protocol Exception: ") +
68 boost::lexical_cast<std::string>(type_)).c_str();
69 } else {
70 return message_.c_str();
71 }
72 }
73
74 protected:
75 /**
76 * Error code
77 */
78 TProtocolExceptionType type_;
79
80};
81
82}}} // facebook::thrift::protocol
83
84#endif // #ifndef _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_