/
Exceptions.h
258 lines (229 loc) · 6.84 KB
/
Exceptions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* Phusion Passenger - http://www.modrails.com/
* Copyright (c) 2008, 2009 Phusion
*
* "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef _PASSENGER_EXCEPTIONS_H_
#define _PASSENGER_EXCEPTIONS_H_
#include <oxt/tracable_exception.hpp>
#include <string>
#include <sstream>
#include <cstring>
/**
* @defgroup Exceptions Exceptions
*/
namespace Passenger {
using namespace std;
/**
* Represents an error returned by a system call or a standard library call.
*
* Use the code() method to find out the value of <tt>errno</tt> at the time
* the error occured.
*
* @ingroup Exceptions
*/
class SystemException: public oxt::tracable_exception {
private:
string briefMessage;
string systemMessage;
string fullMessage;
int m_code;
public:
/**
* Create a new SystemException.
*
* @param briefMessage A brief message describing the error.
* @param errorCode The error code, i.e. the value of errno right after the error occured.
* @note A system description of the error will be appended to the given message.
* For example, if <tt>errorCode</tt> is <tt>EBADF</tt>, and <tt>briefMessage</tt>
* is <em>"Something happened"</em>, then what() will return <em>"Something happened: Bad
* file descriptor (10)"</em> (if 10 is the number for EBADF).
* @post code() == errorCode
* @post brief() == briefMessage
*/
SystemException(const string &briefMessage, int errorCode) {
stringstream str;
str << strerror(errorCode) << " (" << errorCode << ")";
systemMessage = str.str();
setBriefMessage(briefMessage);
m_code = errorCode;
}
virtual ~SystemException() throw() {}
virtual const char *what() const throw() {
return fullMessage.c_str();
}
void setBriefMessage(const string &message) {
briefMessage = message;
fullMessage = briefMessage + ": " + systemMessage;
}
/**
* The value of <tt>errno</tt> at the time the error occured.
*/
int code() const throw() {
return m_code;
}
/**
* Returns a brief version of the exception message. This message does
* not include the system error description, and is equivalent to the
* value of the <tt>message</tt> parameter as passed to the constructor.
*/
string brief() const throw() {
return briefMessage;
}
/**
* Returns the system's error message. This message contains both the
* content of <tt>strerror(errno)</tt> and the errno number itself.
*/
string sys() const throw() {
return systemMessage;
}
};
/**
* A filesystem error, as returned by the operating system. This may include,
* for example, permission errors.
*
* @ingroup Exceptions
*/
class FileSystemException: public SystemException {
private:
string m_filename;
public:
FileSystemException(const string &message, int errorCode,
const string &filename)
: SystemException(message, errorCode),
m_filename(filename) {}
virtual ~FileSystemException() throw() {}
/**
* The filename that's associated to the error.
*/
string filename() const throw() {
return m_filename;
}
};
/**
* Unable to retrieve the system time using <tt>time()</tt>.
*
* @ingroup Exceptions
*/
class TimeRetrievalException: public SystemException {
public:
TimeRetrievalException(const string &message, int errorCode)
: SystemException(message, errorCode)
{}
virtual ~TimeRetrievalException() throw() {}
};
/**
* Represents an error that occured during an I/O operation.
*
* @ingroup Exceptions
*/
class IOException: public oxt::tracable_exception {
private:
string msg;
public:
IOException(const string &message): msg(message) {}
virtual ~IOException() throw() {}
virtual const char *what() const throw() { return msg.c_str(); }
};
/**
* Thrown when a certain file cannot be found.
*/
class FileNotFoundException: public IOException {
public:
FileNotFoundException(const string &message): IOException(message) {}
virtual ~FileNotFoundException() throw() {}
};
/**
* Thrown when an invalid configuration is given.
*/
class ConfigurationException: public oxt::tracable_exception {
private:
string msg;
public:
ConfigurationException(const string &message): msg(message) {}
virtual ~ConfigurationException() throw() {}
virtual const char *what() const throw() { return msg.c_str(); }
};
/**
* Thrown when SpawnManager or ApplicationPool fails to spawn an application
* instance. The exception may contain an error page, which is a user-friendly
* HTML page with details about the error.
*/
class SpawnException: public oxt::tracable_exception {
private:
string msg;
bool m_hasErrorPage;
string m_errorPage;
public:
SpawnException(const string &message)
: msg(message) {
m_hasErrorPage = false;
}
SpawnException(const string &message, const string &errorPage)
: msg(message), m_errorPage(errorPage) {
m_hasErrorPage = true;
}
virtual ~SpawnException() throw() {}
virtual const char *what() const throw() { return msg.c_str(); }
/**
* Check whether an error page is available.
*/
bool hasErrorPage() const {
return m_hasErrorPage;
}
/**
* Return the error page content.
*
* @pre hasErrorPage()
*/
const string getErrorPage() const {
return m_errorPage;
}
};
/**
* A generic runtime exception.
*
* @ingroup Exceptions
*/
class RuntimeException: public oxt::tracable_exception {
private:
string msg;
public:
RuntimeException(const string &message): msg(message) {}
virtual ~RuntimeException() throw() {}
virtual const char *what() const throw() { return msg.c_str(); }
};
/**
* The application pool is too busy and cannot fulfill a get() request.
*
* @ingroup Exceptions
*/
class BusyException: public oxt::tracable_exception {
private:
string msg;
public:
BusyException(const string &message): msg(message) {}
virtual ~BusyException() throw() {}
virtual const char *what() const throw() { return msg.c_str(); }
};
} // namespace Passenger
#endif /* _PASSENGER_EXCEPTIONS_H_ */