1 // src/Exception.hh
2 // This file is part of libpbe; see http://decimail.org
3 // (C) 2004-2007 Philip Endecott
4
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 #ifndef libpbe_Exception_hh
20 #define libpbe_Exception_hh
21
22 #include <errno.h>
23
24 #include <iostream>
25 #include <string>
26
27
28 namespace pbe {
29
30 class Exception
31 {
32 public:
Exception(void)33 Exception(void): exit_status(1) {}
~Exception()34 virtual ~Exception() {}
35 virtual void report(std::ostream& s) const = 0;
36 const int exit_status;
37 };
38
39
40 class StrException: public Exception
41 {
42 private:
43 const std::string msg;
44 public:
StrException(std::string s)45 StrException(std::string s): msg(s) {}
report(std::ostream & s) const46 void report(std::ostream& s) const { s << msg << std::endl; }
47 };
48
49
50 class StdException: public Exception
51 {
52 private:
53 std::string msg;
54 public:
StdException(std::exception & e)55 StdException(std::exception& e): msg(e.what()) {}
report(std::ostream & s) const56 void report(std::ostream& s) const { s << msg << std::endl; }
57 };
58
59
60 class UnknownException: public Exception
61 {
62 public:
63 void report(std::ostream& s) const;
64 };
65
66
67 class ErrnoException: public Exception
68 {
69 private:
70 int error_number;
71 std::string doing_what;
72
73 public:
ErrnoException(std::string dw,int errno_=errno)74 ErrnoException(std::string dw, int errno_=errno): error_number(errno_), doing_what(dw) {}
get_errno(void)75 int get_errno(void) { return error_number; }
76 void report(std::ostream& s) const;
77 };
78
79
80 struct NoSuchFileOrDirectory: public ErrnoException {
NoSuchFileOrDirectorypbe::NoSuchFileOrDirectory81 NoSuchFileOrDirectory(std::string dw):
82 ErrnoException(dw) {}
83 };
84
85 struct ConnectionRefused: public ErrnoException {
ConnectionRefusedpbe::ConnectionRefused86 ConnectionRefused(std::string dw):
87 ErrnoException(dw) {}
88 };
89
90 struct NoSuchDevice: public ErrnoException {
NoSuchDevicepbe::NoSuchDevice91 NoSuchDevice(std::string dw):
92 ErrnoException(dw) {}
93 };
94
95 struct HostUnreachable: public ErrnoException {
HostUnreachablepbe::HostUnreachable96 HostUnreachable(std::string dw):
97 ErrnoException(dw) {}
98 };
99
100 struct NoDataAvailable: public ErrnoException {
NoDataAvailablepbe::NoDataAvailable101 NoDataAvailable(std::string dw):
102 ErrnoException(dw) {}
103 };
104
105 struct BrokenPipe: public ErrnoException {
BrokenPipepbe::BrokenPipe106 BrokenPipe(std::string dw):
107 ErrnoException(dw) {}
108 };
109
110 struct Overflow: public ErrnoException {
Overflowpbe::Overflow111 Overflow(std::string dw):
112 ErrnoException(dw) {}
113 };
114
115 struct InvalidArgument: public ErrnoException {
InvalidArgumentpbe::InvalidArgument116 InvalidArgument(std::string dw):
117 ErrnoException(dw) {}
118 };
119
120 struct WouldBlock: public ErrnoException {
WouldBlockpbe::WouldBlock121 WouldBlock(std::string dw):
122 ErrnoException(dw) {}
123 };
124
125 struct TimedOut: public ErrnoException {
TimedOutpbe::TimedOut126 TimedOut(std::string dw):
127 ErrnoException(dw) {}
128 };
129
130 struct IOError: public ErrnoException {
IOErrorpbe::IOError131 IOError(std::string dw):
132 ErrnoException(dw) {}
133 };
134
135 struct InterruptedSysCall: public ErrnoException {
InterruptedSysCallpbe::InterruptedSysCall136 InterruptedSysCall(std::string dw):
137 ErrnoException(dw) {}
138 };
139
140 struct NoSpace: public ErrnoException {
NoSpacepbe::NoSpace141 NoSpace(std::string dw):
142 ErrnoException(dw) {}
143 };
144
throw_ErrnoException(std::string dw,int errno_=errno)145 inline void throw_ErrnoException(std::string dw, int errno_=errno) {
146 switch (errno_) {
147 case ENOENT: throw NoSuchFileOrDirectory(dw);
148 case ECONNREFUSED: throw ConnectionRefused(dw);
149 case ENODEV: throw NoSuchDevice(dw);
150 case EHOSTUNREACH: throw HostUnreachable(dw);
151 #ifdef ENODATA
152 // FreeBSD doesn't have ENODATA
153 case ENODATA: throw NoDataAvailable(dw);
154 #endif
155 case EPIPE: throw BrokenPipe(dw);
156 case EOVERFLOW: throw Overflow(dw);
157 case EINVAL: throw InvalidArgument(dw);
158 case EWOULDBLOCK: throw WouldBlock(dw);
159 case ETIMEDOUT: throw TimedOut(dw);
160 case EIO: throw IOError(dw);
161 case EINTR: throw InterruptedSysCall(dw);
162 case ENOSPC: throw NoSpace(dw);
163 default: throw ErrnoException(dw,errno_);
164 }
165 }
166
167
168 #define RETHROW_MISC_EXCEPTIONS \
169 catch(pbe::Exception& E) { \
170 throw; \
171 } \
172 catch(std::exception& e) { \
173 throw pbe::StdException(e); \
174 } \
175 catch(const char* s) { \
176 throw pbe::StrException(s); \
177 } \
178 catch(std::string s) { \
179 throw pbe::StrException(s); \
180 } \
181 catch(...) { \
182 throw pbe::UnknownException();\
183 }
184
185
186 };
187
188
189 #endif
190