- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List

# Exceptions.h

    00001 /*
    00002  *  Phusion Passenger - http://www.modrails.com/
    00003  *  Copyright (c) 2010 Phusion
    00004  *
    00005  *  "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
    00006  *
    00007  *  Permission is hereby granted, free of charge, to any person obtaining a copy
    00008  *  of this software and associated documentation files (the "Software"), to deal
    00009  *  in the Software without restriction, including without limitation the rights
    00010  *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    00011  *  copies of the Software, and to permit persons to whom the Software is
    00012  *  furnished to do so, subject to the following conditions:
    00013  *
    00014  *  The above copyright notice and this permission notice shall be included in
    00015  *  all copies or substantial portions of the Software.
    00016  *
    00017  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    00018  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    00019  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    00020  *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    00021  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    00022  *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    00023  *  THE SOFTWARE.
    00024  */
    00025 #ifndef _PASSENGER_EXCEPTIONS_H_
    00026 #define _PASSENGER_EXCEPTIONS_H_
    00027
    00028 #include <oxt/tracable_exception.hpp>
    00029 #include <string>
    00030 #include <sstream>
    00031 #include <cstring>
    00032
    00033 /**
    00034  * @defgroup Exceptions Exceptions
    00035  */
    00036
    00037 namespace Passenger {
    00038
    00039 using namespace std;
    00040
    00041 /**
    00042  * Represents an error returned by a system call or a standard library call.
    00043  *
    00044  * Use the code() method to find out the value of <tt>errno</tt> at the time
    00045  * the error occured.
    00046  *
    00047  * @ingroup Exceptions
    00048  */
    00049 class SystemException: public oxt::tracable_exception {
    00050 private:
    00051         string briefMessage;
    00052         string systemMessage;
    00053         string fullMessage;
    00054         int m_code;
    00055 public:
    00056         /**
    00057          * Create a new SystemException.
    00058          *
    00059          * @param briefMessage A brief message describing the error.
    00060          * @param errorCode The error code, i.e. the value of errno right after the error occured.
    00061          * @note A system description of the error will be appended to the given message.
    00062          *    For example, if <tt>errorCode</tt> is <tt>EBADF</tt>, and <tt>briefMessage</tt>
    00063          *    is <em>"Something happened"</em>, then what() will return <em>"Something happened: Bad
    00064          *    file descriptor (10)"</em> (if 10 is the number for EBADF).
    00065          * @post code() == errorCode
    00066          * @post brief() == briefMessage
    00067          */
    00068         SystemException(const string &briefMessage, int errorCode) {
    00069                 stringstream str;
    00070
    00071                 str << strerror(errorCode) << " (" << errorCode << ")";
    00072                 systemMessage = str.str();
    00073
    00074                 setBriefMessage(briefMessage);
    00075                 m_code = errorCode;
    00076         }
    00077
    00078         virtual ~SystemException() throw() {}
    00079
    00080         virtual const char *what() const throw() {
    00081                 return fullMessage.c_str();
    00082         }
    00083
    00084         void setBriefMessage(const string &message) {
    00085                 briefMessage = message;
    00086                 fullMessage = briefMessage + ": " + systemMessage;
    00087         }
    00088
    00089         /**
    00090          * The value of <tt>errno</tt> at the time the error occured.
    00091          */
    00092         int code() const throw() {
    00093                 return m_code;
    00094         }
    00095
    00096         /**
    00097          * Returns a brief version of the exception message. This message does
    00098          * not include the system error description, and is equivalent to the
    00099          * value of the <tt>message</tt> parameter as passed to the constructor.
    00100          */
    00101         string brief() const throw() {
    00102                 return briefMessage;
    00103         }
    00104
    00105         /**
    00106          * Returns the system's error message. This message contains both the
    00107          * content of <tt>strerror(errno)</tt> and the errno number itself.
    00108          */
    00109         string sys() const throw() {
    00110                 return systemMessage;
    00111         }
    00112 };
    00113
    00114 /**
    00115  * A filesystem error, as returned by the operating system. This may include,
    00116  * for example, permission errors.
    00117  *
    00118  * @ingroup Exceptions
    00119  */
    00120 class FileSystemException: public SystemException {
    00121 private:
    00122         string m_filename;
    00123 public:
    00124         FileSystemException(const string &message, int errorCode,
    00125                 const string &filename)
    00126                 : SystemException(message, errorCode),
    00127                   m_filename(filename) {}
    00128
    00129         virtual ~FileSystemException() throw() {}
    00130
    00131         /**
    00132          * The filename that's associated to the error.
    00133          */
    00134         string filename() const throw() {
    00135                 return m_filename;
    00136         }
    00137 };
    00138
    00139 /**
    00140  * Unable to retrieve the system time using <tt>time()</tt>.
    00141  *
    00142  * @ingroup Exceptions
    00143  */
    00144 class TimeRetrievalException: public SystemException {
    00145 public:
    00146         TimeRetrievalException(const string &message, int errorCode)
    00147                 : SystemException(message, errorCode)
    00148                 {}
    00149         virtual ~TimeRetrievalException() throw() {}
    00150 };
    00151
    00152 /**
    00153  * Represents an error that occured during an I/O operation.
    00154  *
    00155  * @ingroup Exceptions
    00156  */
    00157 class IOException: public oxt::tracable_exception {
    00158 private:
    00159         string msg;
    00160 public:
    00161         IOException(const string &message): msg(message) {}
    00162         virtual ~IOException() throw() {}
    00163         virtual const char *what() const throw() { return msg.c_str(); }
    00164 };
    00165
    00166 /**
    00167  * Thrown when a certain file cannot be found.
    00168  */
    00169 class FileNotFoundException: public IOException {
    00170 public:
    00171         FileNotFoundException(const string &message): IOException(message) {}
    00172         virtual ~FileNotFoundException() throw() {}
    00173 };
    00174
    00175 /**
    00176  * An unexpected end-of-file I/O error.
    00177  *
    00178  * @ingroup Exceptions
    00179  */
    00180 class EOFException: public IOException {
    00181 public:
    00182         EOFException(const string &message): IOException(message) {}
    00183         virtual ~EOFException() throw() {}
    00184 };
    00185
    00186 /**
    00187  * Thrown when an invalid configuration is given.
    00188  */
    00189 class ConfigurationException: public oxt::tracable_exception {
    00190 private:
    00191         string msg;
    00192 public:
    00193         ConfigurationException(const string &message): msg(message) {}
    00194         virtual ~ConfigurationException() throw() {}
    00195         virtual const char *what() const throw() { return msg.c_str(); }
    00196 };
    00197
    00198 /**
    00199  * Thrown when SpawnManager or ApplicationPool fails to spawn an application
    00200  * instance. The exception may contain an error page, which is a user-friendly
    00201  * HTML page with details about the error.
    00202  */
    00203 class SpawnException: public oxt::tracable_exception {
    00204 private:
    00205         string msg;
    00206         bool m_hasErrorPage;
    00207         bool m_isHTML;
    00208         string m_errorPage;
    00209 public:
    00210         SpawnException(const string &message)
    00211                 : msg(message) {
    00212                 m_hasErrorPage = false;
    00213                 m_isHTML = false;
    00214         }
    00215
    00216         SpawnException(const string &message, const string &errorPage, bool isHTML = true)
    00217                 : msg(message), m_errorPage(errorPage)
    00218         {
    00219                 m_hasErrorPage = true;
    00220                 m_isHTML = isHTML;
    00221         }
    00222
    00223         virtual ~SpawnException() throw() {}
    00224         virtual const char *what() const throw() { return msg.c_str(); }
    00225
    00226         /**
    00227          * Check whether an error page is available.
    00228          */
    00229         bool hasErrorPage() const {
    00230                 return m_hasErrorPage;
    00231         }
    00232
    00233         /**
    00234          * Return the error page content.
    00235          *
    00236          * @pre hasErrorPage()
    00237          */
    00238         const string getErrorPage() const {
    00239                 return m_errorPage;
    00240         }
    00241
    00242         /**
    00243          * Whether the error page content is HTML.
    00244          *
    00245          * @pre hasErrorPage()
    00246          */
    00247         bool isHTML() const {
    00248                 return m_isHTML;
    00249         }
    00250 };
    00251
    00252 /**
    00253  * Indicates that a specified argument is incorrect or violates a requirement.
    00254  *
    00255  * @ingroup Exceptions
    00256  */
    00257 class ArgumentException: public oxt::tracable_exception {
    00258 private:
    00259         string msg;
    00260 public:
    00261         ArgumentException(const string &message): msg(message) {}
    00262         virtual ~ArgumentException() throw() {}
    00263         virtual const char *what() const throw() { return msg.c_str(); }
    00264 };
    00265
    00266 /*
    00267  * @ingroup Exceptions
    00268  */
    00269 class InvalidModeStringException: public ArgumentException {
    00270 public:
    00271         InvalidModeStringException(const string &message): ArgumentException(message) {}
    00272 };
    00273
    00274 /**
    00275  * A generic runtime exception.
    00276  *
    00277  * @ingroup Exceptions
    00278  */
    00279 class RuntimeException: public oxt::tracable_exception {
    00280 private:
    00281         string msg;
    00282 public:
    00283         RuntimeException(const string &message): msg(message) {}
    00284         virtual ~RuntimeException() throw() {}
    00285         virtual const char *what() const throw() { return msg.c_str(); }
    00286 };
    00287
    00288 /**
    00289  * An exception indicating that some timeout expired.
    00290  *
    00291  * @ingroup Exceptions
    00292  */
    00293 class TimeoutException: public oxt::tracable_exception {
    00294 private:
    00295         string msg;
    00296 public:
    00297         TimeoutException(const string &message): msg(message) {}
    00298         virtual ~TimeoutException() throw() {}
    00299         virtual const char *what() const throw() { return msg.c_str(); }
    00300 };
    00301
    00302 /**
    00303  * Represents some kind of security error.
    00304  *
    00305  * @ingroup Exceptions
    00306  */
    00307 class SecurityException: public oxt::tracable_exception {
    00308 private:
    00309         string msg;
    00310 public:
    00311         SecurityException(const string &message): msg(message) {}
    00312         virtual ~SecurityException() throw() {}
    00313         virtual const char *what() const throw() { return msg.c_str(); }
    00314 };
    00315
    00316 /**
    00317  * @ingroup Exceptions
    00318  */
    00319 class NonExistentUserException: public SecurityException {
    00320 public:
    00321         NonExistentUserException(const string &message): SecurityException(message) {}
    00322 };
    00323
    00324 /**
    00325  * @ingroup Exceptions
    00326  */
    00327 class NonExistentGroupException: public SecurityException {
    00328 public:
    00329         NonExistentGroupException(const string &message): SecurityException(message) {}
    00330 };
    00331
    00332 /**
    00333  * The application pool is too busy and cannot fulfill a get() request.
    00334  *
    00335  * @ingroup Exceptions
    00336  */
    00337 class BusyException: public oxt::tracable_exception {
    00338 private:
    00339         string msg;
    00340 public:
    00341         BusyException(const string &message): msg(message) {}
    00342         virtual ~BusyException() throw() {}
    00343         virtual const char *what() const throw() { return msg.c_str(); }
    00344 };
    00345
    00346 /**
    00347  * A parser detected a syntax error.
    00348  *
    00349  * @ingroup Exceptions
    00350  */
    00351 class SyntaxError: public oxt::tracable_exception {
    00352 private:
    00353         string msg;
    00354 public:
    00355         SyntaxError(const string &message): msg(message) {}
    00356         virtual ~SyntaxError() throw() {}
    00357         virtual const char *what() const throw() { return msg.c_str(); }
    00358 };
    00359
    00360 } // namespace Passenger
    00361
    00362 #endif /* _PASSENGER_EXCEPTIONS_H_ */