- 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         string m_errorPage;
    00208 public:
    00209         SpawnException(const string &message)
    00210                 : msg(message) {
    00211                 m_hasErrorPage = false;
    00212         }
    00213
    00214         SpawnException(const string &message, const string &errorPage)
    00215                 : msg(message), m_errorPage(errorPage) {
    00216                 m_hasErrorPage = true;
    00217         }
    00218
    00219         virtual ~SpawnException() throw() {}
    00220         virtual const char *what() const throw() { return msg.c_str(); }
    00221
    00222         /**
    00223          * Check whether an error page is available.
    00224          */
    00225         bool hasErrorPage() const {
    00226                 return m_hasErrorPage;
    00227         }
    00228
    00229         /**
    00230          * Return the error page content.
    00231          *
    00232          * @pre hasErrorPage()
    00233          */
    00234         const string getErrorPage() const {
    00235                 return m_errorPage;
    00236         }
    00237 };
    00238
    00239 /**
    00240  * Indicates that a specified argument is incorrect or violates a requirement.
    00241  *
    00242  * @ingroup Exceptions
    00243  */
    00244 class ArgumentException: public oxt::tracable_exception {
    00245 private:
    00246         string msg;
    00247 public:
    00248         ArgumentException(const string &message): msg(message) {}
    00249         virtual ~ArgumentException() throw() {}
    00250         virtual const char *what() const throw() { return msg.c_str(); }
    00251 };
    00252
    00253 /*
    00254  * @ingroup Exceptions
    00255  */
    00256 class InvalidModeStringException: public ArgumentException {
    00257 public:
    00258         InvalidModeStringException(const string &message): ArgumentException(message) {}
    00259 };
    00260
    00261 /**
    00262  * A generic runtime exception.
    00263  *
    00264  * @ingroup Exceptions
    00265  */
    00266 class RuntimeException: public oxt::tracable_exception {
    00267 private:
    00268         string msg;
    00269 public:
    00270         RuntimeException(const string &message): msg(message) {}
    00271         virtual ~RuntimeException() throw() {}
    00272         virtual const char *what() const throw() { return msg.c_str(); }
    00273 };
    00274
    00275 /**
    00276  * An exception indicating that some timeout expired.
    00277  *
    00278  * @ingroup Exceptions
    00279  */
    00280 class TimeoutException: public oxt::tracable_exception {
    00281 private:
    00282         string msg;
    00283 public:
    00284         TimeoutException(const string &message): msg(message) {}
    00285         virtual ~TimeoutException() throw() {}
    00286         virtual const char *what() const throw() { return msg.c_str(); }
    00287 };
    00288
    00289 /**
    00290  * Represents some kind of security error.
    00291  *
    00292  * @ingroup Exceptions
    00293  */
    00294 class SecurityException: public oxt::tracable_exception {
    00295 private:
    00296         string msg;
    00297 public:
    00298         SecurityException(const string &message): msg(message) {}
    00299         virtual ~SecurityException() throw() {}
    00300         virtual const char *what() const throw() { return msg.c_str(); }
    00301 };
    00302
    00303 /**
    00304  * @ingroup Exceptions
    00305  */
    00306 class NonExistentUserException: public SecurityException {
    00307 public:
    00308         NonExistentUserException(const string &message): SecurityException(message) {}
    00309 };
    00310
    00311 /**
    00312  * @ingroup Exceptions
    00313  */
    00314 class NonExistentGroupException: public SecurityException {
    00315 public:
    00316         NonExistentGroupException(const string &message): SecurityException(message) {}
    00317 };
    00318
    00319 /**
    00320  * The application pool is too busy and cannot fulfill a get() request.
    00321  *
    00322  * @ingroup Exceptions
    00323  */
    00324 class BusyException: public oxt::tracable_exception {
    00325 private:
    00326         string msg;
    00327 public:
    00328         BusyException(const string &message): msg(message) {}
    00329         virtual ~BusyException() throw() {}
    00330         virtual const char *what() const throw() { return msg.c_str(); }
    00331 };
    00332
    00333 } // namespace Passenger
    00334
    00335 #endif /* _PASSENGER_EXCEPTIONS_H_ */