libt2n: (gerd) command handling, unit-test still bailing out
[libt2n] / src / t2n_exception.hxx
1 /***************************************************************************
2  *   Copyright (C) 2006 by Gerd v. Egidy                                   *
3  *   gve@intra2net.com                                                     *
4  *                                                                         *
5  *   This library is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU Lesser General Public License version   *
7  *   2.1 as published by the Free Software Foundation.                     *
8  *                                                                         *
9  *   This library is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU Lesser General Public License for more details.                   *
13  *                                                                         *
14  *   You should have received a copy of the GNU Lesser General Public      *
15  *   License along with this program; if not, write to the                 *
16  *   Free Software Foundation, Inc.,                                       *
17  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18  ***************************************************************************/
19 #ifndef __LIBT2N_T2N_EXCEPTION
20 #define __LIBT2N_T2N_EXCEPTION
21
22 #include <stdexcept>
23 #include <string>
24
25 #include <boost/serialization/serialization.hpp>
26
27 namespace boost {
28 namespace serialization {
29
30 // make std::exception serializable
31 template<class Archive>
32 void serialize(Archive & ar, std::exception & g, const unsigned int version)
33 {
34 }
35
36 } // namespace serialization
37 } // namespace boost
38
39 namespace libt2n
40 {
41 /// a generic exception that can be handeled with libt2n
42 class t2n_exception : public std::exception
43 {
44     private:
45         std::string message;
46
47         friend class boost::serialization::access;
48         template<class Archive>
49         void serialize(Archive & ar, const unsigned int version)
50         {
51             ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(exception);
52             ar & BOOST_SERIALIZATION_NVP(message);
53         }
54
55     public:
56         t2n_exception(const std::string& _message)
57             { message=_message; }
58
59         t2n_exception()
60             { }
61
62         virtual const char* what() const throw()
63             { return message.c_str(); }
64
65         virtual t2n_exception* clone() const
66             { return new t2n_exception(*this); }
67
68         virtual ~t2n_exception() throw()
69             {}
70
71         virtual void do_throw()
72             { throw *this; }
73 };
74
75 /// a (unspecified) problem with libt2n communication
76 class t2n_communication_error : public t2n_exception
77 {
78     private:
79         friend class boost::serialization::access;
80         template<class Archive>
81         void serialize(Archive & ar, const unsigned int version)
82         {
83             ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_exception);
84         }
85
86     public:
87         t2n_communication_error(const std::string& _message)
88             : t2n_exception(_message)
89             { }
90
91         t2n_communication_error()
92             { }
93
94         t2n_exception* clone() const
95             { return new t2n_communication_error(*this); }
96
97         void do_throw()
98             { throw *this; }
99 };
100
101 /// can't connect to libt2n server
102 class t2n_connect_error : public t2n_communication_error
103 {
104     private:
105         friend class boost::serialization::access;
106         template<class Archive>
107         void serialize(Archive & ar, const unsigned int version)
108         {
109             ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_communication_error);
110         }
111
112     public:
113         t2n_connect_error(const std::string& _message)
114             : t2n_communication_error(_message)
115             { }
116
117         t2n_connect_error()
118             { }
119
120         t2n_exception* clone() const
121             { return new t2n_connect_error(*this); }
122
123         void do_throw()
124             { throw *this; }
125 };
126
127 /// error establishing a socket on the server (only thrown on the server-side)
128 class t2n_server_error : public t2n_communication_error
129 {
130     private:
131         friend class boost::serialization::access;
132         template<class Archive>
133         void serialize(Archive & ar, const unsigned int version)
134         {
135             ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_communication_error);
136         }
137
138     public:
139         t2n_server_error(const std::string& _message)
140             : t2n_communication_error(_message)
141             { }
142
143         t2n_server_error()
144             { }
145
146         t2n_exception* clone() const
147             { return new t2n_server_error(*this); }
148
149         void do_throw()
150             { throw *this; }
151 };
152
153 /// error transmitting or receiving libt2n messages
154 class t2n_transfer_error : public t2n_communication_error
155 {
156     private:
157         friend class boost::serialization::access;
158         template<class Archive>
159         void serialize(Archive & ar, const unsigned int version)
160         {
161             ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_communication_error);
162         }
163
164     public:
165         t2n_transfer_error(const std::string& _message)
166             : t2n_communication_error(_message)
167             { }
168
169         t2n_transfer_error()
170             { }
171
172         t2n_exception* clone() const
173             { return new t2n_transfer_error(*this); }
174
175         void do_throw()
176             { throw *this; }
177 };
178
179 /// tried to talk to an incompatible libt2n version
180 class t2n_version_mismatch : public t2n_communication_error
181 {
182     private:
183         friend class boost::serialization::access;
184         template<class Archive>
185         void serialize(Archive & ar, const unsigned int version)
186         {
187             ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_communication_error);
188         }
189
190     public:
191         t2n_version_mismatch(const std::string& _message)
192             : t2n_communication_error(_message)
193             { }
194
195         t2n_version_mismatch()
196             { }
197
198         t2n_exception* clone() const
199             { return new t2n_version_mismatch(*this); }
200
201         void do_throw()
202             { throw *this; }
203 };
204
205 /// illegal libt2n command received
206 class t2n_command_error : public t2n_exception
207 {
208     private:
209         friend class boost::serialization::access;
210         template<class Archive>
211         void serialize(Archive & ar, const unsigned int version)
212         {
213             ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_exception);
214         }
215
216     public:
217         t2n_command_error(const std::string& _message)
218             : t2n_exception(_message)
219             { }
220
221         t2n_command_error()
222             { }
223
224         t2n_exception* clone() const
225             { return new t2n_command_error(*this); }
226
227         void do_throw()
228             { throw *this; }
229 };
230
231 /// error serializing or deserializing a libt2n command packet
232 class t2n_serialization_error : public t2n_exception
233 {
234     private:
235         friend class boost::serialization::access;
236         template<class Archive>
237         void serialize(Archive & ar, const unsigned int version)
238         {
239             ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_exception);
240         }
241
242     public:
243         t2n_serialization_error(const std::string& _message)
244             : t2n_exception(_message)
245             { }
246
247         t2n_serialization_error()
248             { }
249
250         t2n_exception* clone() const
251             { return new t2n_serialization_error(*this); }
252
253         void do_throw()
254             { throw *this; }
255 };
256
257 /** @brief a runtime error within the remote function.
258  derive your own custom exceptions from this one
259 */
260 class t2n_runtime_error : public t2n_exception
261 {
262     private:
263         friend class boost::serialization::access;
264         template<class Archive>
265         void serialize(Archive & ar, const unsigned int version)
266         {
267             ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_exception);
268         }
269
270     public:
271         t2n_runtime_error(const std::string& _message)
272             : t2n_exception(_message)
273             { }
274
275         t2n_runtime_error()
276             { }
277
278         t2n_exception* clone() const
279             { return new t2n_runtime_error(*this); }
280
281         void do_throw()
282             { throw *this; }
283 };
284
285 }  // namespace libt2n
286
287 #endif