blob: d024ad584a83d33d733030ed8a2d6319243b36b9 [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 Sleef5f2be42006-09-05 21:05:31 +00007#ifndef _THRIFT_THRIFT_H_
8#define _THRIFT_THRIFT_H_ 1
Mark Sleee8540632006-05-30 09:24:40 +00009
Mark Slee8d7e1f62006-06-07 06:48:56 +000010#include <netinet/in.h>
11#include <inttypes.h>
Mark Sleee8540632006-05-30 09:24:40 +000012#include <string>
13#include <map>
14#include <list>
15#include <set>
Mark Slee4ecbebc2006-09-05 00:14:21 +000016#include <vector>
Marc Slemko5b126d62006-08-11 23:03:42 +000017#include <exception>
18
Aditya Agarwald622e962006-10-11 02:42:49 +000019#include "TLogging.h"
20
Mark Sleecfc01932006-09-01 22:18:16 +000021namespace facebook { namespace thrift {
Marc Slemko5b126d62006-08-11 23:03:42 +000022
Mark Sleef9831082007-02-20 20:59:21 +000023namespace protocol {
24 class TProtocol;
25}
26
Mark Sleeb9ff32a2006-11-16 01:00:24 +000027class TException : public std::exception {
Marc Slemko5b126d62006-08-11 23:03:42 +000028public:
Mark Sleeb9ff32a2006-11-16 01:00:24 +000029 TException() {}
30
Mark Slee82a6c0f2007-04-04 21:08:21 +000031 TException(const std::string& message) :
Mark Slee2f6404d2006-10-10 01:37:40 +000032 message_(message) {}
33
Martin Kraemer8196a612006-12-09 01:57:58 +000034 virtual ~TException() throw() {}
Mark Slee2f6404d2006-10-10 01:37:40 +000035
Martin Kraemer92a2eac2007-02-05 20:58:41 +000036 virtual const char* what() const throw() {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000037 if (message_.empty()) {
38 return "Default TException.";
39 } else {
40 return message_.c_str();
41 }
Mark Slee2f6404d2006-10-10 01:37:40 +000042 }
43
Martin Kraemer92a2eac2007-02-05 20:58:41 +000044protected:
Mark Slee2abc9df2006-12-16 01:06:49 +000045 std::string message_;
Mark Slee2f6404d2006-10-10 01:37:40 +000046
Marc Slemko5b126d62006-08-11 23:03:42 +000047};
48
Mark Sleef9831082007-02-20 20:59:21 +000049class TApplicationException : public TException {
50public:
51
52 /**
53 * Error codes for the various types of exceptions.
54 */
55 enum TApplicationExceptionType {
56 UNKNOWN = 0,
Mark Sleef3d33632007-02-21 01:39:23 +000057 UNKNOWN_METHOD = 1,
58 INVALID_MESSAGE_TYPE = 2,
59 WRONG_METHOD_NAME = 3,
60 BAD_SEQUENCE_ID = 4,
61 MISSING_RESULT = 5,
Mark Sleef9831082007-02-20 20:59:21 +000062 };
63
64 TApplicationException() :
65 TException(),
66 type_(UNKNOWN) {}
67
68 TApplicationException(TApplicationExceptionType type) :
69 TException(),
70 type_(type) {}
71
Mark Slee82a6c0f2007-04-04 21:08:21 +000072 TApplicationException(const std::string& message) :
Mark Sleef9831082007-02-20 20:59:21 +000073 TException(message),
74 type_(UNKNOWN) {}
75
76 TApplicationException(TApplicationExceptionType type,
Mark Slee82a6c0f2007-04-04 21:08:21 +000077 const std::string& message) :
Mark Sleef9831082007-02-20 20:59:21 +000078 TException(message),
79 type_(type) {}
80
81 virtual ~TApplicationException() throw() {}
82
83 /**
84 * Returns an error code that provides information about the type of error
85 * that has occurred.
86 *
87 * @return Error code
88 */
89 TApplicationExceptionType getType() {
90 return type_;
91 }
92
93 virtual const char* what() const throw() {
94 if (message_.empty()) {
95 return "Default TApplicationException.";
96 } else {
97 return message_.c_str();
98 }
99 }
100
Mark Sleeba8f8d72007-04-03 00:34:00 +0000101 uint32_t read(protocol::TProtocol* iprot);
102 uint32_t write(protocol::TProtocol* oprot) const;
Mark Sleef9831082007-02-20 20:59:21 +0000103
104protected:
105 /**
106 * Error code
107 */
108 TApplicationExceptionType type_;
109
110};
111
112
Marc Slemko5b126d62006-08-11 23:03:42 +0000113}} // facebook::thrift
Mark Sleee8540632006-05-30 09:24:40 +0000114
Mark Sleef5f2be42006-09-05 21:05:31 +0000115#endif // #ifndef _THRIFT_THRIFT_H_