blob: 2cb656b25c2f629e7def0ae81cf4cd58aa6af28d [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 Slee4f261c52007-04-13 00:33:24 +000010#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
Mark Slee8d7e1f62006-06-07 06:48:56 +000014#include <netinet/in.h>
Mark Slee4f261c52007-04-13 00:33:24 +000015#ifdef HAVE_INTTYPES_H
Mark Slee8d7e1f62006-06-07 06:48:56 +000016#include <inttypes.h>
Mark Slee4f261c52007-04-13 00:33:24 +000017#endif
Mark Sleee8540632006-05-30 09:24:40 +000018#include <string>
19#include <map>
20#include <list>
21#include <set>
Mark Slee4ecbebc2006-09-05 00:14:21 +000022#include <vector>
Marc Slemko5b126d62006-08-11 23:03:42 +000023#include <exception>
24
Aditya Agarwald622e962006-10-11 02:42:49 +000025#include "TLogging.h"
26
Mark Sleecfc01932006-09-01 22:18:16 +000027namespace facebook { namespace thrift {
Marc Slemko5b126d62006-08-11 23:03:42 +000028
Mark Sleef9831082007-02-20 20:59:21 +000029namespace protocol {
30 class TProtocol;
31}
32
Mark Sleeb9ff32a2006-11-16 01:00:24 +000033class TException : public std::exception {
Marc Slemko5b126d62006-08-11 23:03:42 +000034public:
Mark Sleeb9ff32a2006-11-16 01:00:24 +000035 TException() {}
36
Mark Slee82a6c0f2007-04-04 21:08:21 +000037 TException(const std::string& message) :
Mark Slee2f6404d2006-10-10 01:37:40 +000038 message_(message) {}
39
Martin Kraemer8196a612006-12-09 01:57:58 +000040 virtual ~TException() throw() {}
Mark Slee2f6404d2006-10-10 01:37:40 +000041
Martin Kraemer92a2eac2007-02-05 20:58:41 +000042 virtual const char* what() const throw() {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000043 if (message_.empty()) {
44 return "Default TException.";
45 } else {
46 return message_.c_str();
47 }
Mark Slee2f6404d2006-10-10 01:37:40 +000048 }
49
Martin Kraemer92a2eac2007-02-05 20:58:41 +000050protected:
Mark Slee2abc9df2006-12-16 01:06:49 +000051 std::string message_;
Mark Slee2f6404d2006-10-10 01:37:40 +000052
Marc Slemko5b126d62006-08-11 23:03:42 +000053};
54
Mark Sleef9831082007-02-20 20:59:21 +000055class TApplicationException : public TException {
56public:
57
58 /**
59 * Error codes for the various types of exceptions.
60 */
61 enum TApplicationExceptionType {
62 UNKNOWN = 0,
Mark Sleef3d33632007-02-21 01:39:23 +000063 UNKNOWN_METHOD = 1,
64 INVALID_MESSAGE_TYPE = 2,
65 WRONG_METHOD_NAME = 3,
66 BAD_SEQUENCE_ID = 4,
67 MISSING_RESULT = 5,
Mark Sleef9831082007-02-20 20:59:21 +000068 };
69
70 TApplicationException() :
71 TException(),
72 type_(UNKNOWN) {}
73
74 TApplicationException(TApplicationExceptionType type) :
75 TException(),
76 type_(type) {}
77
Mark Slee82a6c0f2007-04-04 21:08:21 +000078 TApplicationException(const std::string& message) :
Mark Sleef9831082007-02-20 20:59:21 +000079 TException(message),
80 type_(UNKNOWN) {}
81
82 TApplicationException(TApplicationExceptionType type,
Mark Slee82a6c0f2007-04-04 21:08:21 +000083 const std::string& message) :
Mark Sleef9831082007-02-20 20:59:21 +000084 TException(message),
85 type_(type) {}
86
87 virtual ~TApplicationException() throw() {}
88
89 /**
90 * Returns an error code that provides information about the type of error
91 * that has occurred.
92 *
93 * @return Error code
94 */
95 TApplicationExceptionType getType() {
96 return type_;
97 }
98
99 virtual const char* what() const throw() {
100 if (message_.empty()) {
101 return "Default TApplicationException.";
102 } else {
103 return message_.c_str();
104 }
105 }
106
Mark Sleeba8f8d72007-04-03 00:34:00 +0000107 uint32_t read(protocol::TProtocol* iprot);
108 uint32_t write(protocol::TProtocol* oprot) const;
Mark Sleef9831082007-02-20 20:59:21 +0000109
110protected:
111 /**
112 * Error code
113 */
114 TApplicationExceptionType type_;
115
116};
117
118
Marc Slemko5b126d62006-08-11 23:03:42 +0000119}} // facebook::thrift
Mark Sleee8540632006-05-30 09:24:40 +0000120
Mark Sleef5f2be42006-09-05 21:05:31 +0000121#endif // #ifndef _THRIFT_THRIFT_H_