- Main Page
- Modules
- Classes
- Files

- File List

# Exceptions.h

    00001 /*
    00002  *  Phusion Passenger - http://www.modrails.com/
    00003  *  Copyright (C) 2008  Phusion
    00004  *
    00005  *  Phusion Passenger is a trademark of Hongli Lai & Ninh Bui.
    00006  *
    00007  *  This program is free software; you can redistribute it and/or modify
    00008  *  it under the terms of the GNU General Public License as published by
    00009  *  the Free Software Foundation; version 2 of the License.
    00010  *
    00011  *  This program is distributed in the hope that it will be useful,
    00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00014  *  GNU General Public License for more details.
    00015  *
    00016  *  You should have received a copy of the GNU General Public License along
    00017  *  with this program; if not, write to the Free Software Foundation, Inc.,
    00018  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    00019  */
    00020 #ifndef _PASSENGER_EXCEPTIONS_H_
    00021 #define _PASSENGER_EXCEPTIONS_H_
    00022
    00023 #include <oxt/tracable_exception.hpp>
    00024 #include <string>
    00025 #include <sstream>
    00026 #include <cstring>
    00027
    00028 /**
    00029  * @defgroup Exceptions Exceptions
    00030  */
    00031
    00032 namespace Passenger {
    00033
    00034 using namespace std;
    00035
    00036 /**
    00037  * Represents an error returned by a system call or a standard library call.
    00038  *
    00039  * Use the code() method to find out the value of <tt>errno</tt> at the time
    00040  * the error occured.
    00041  *
    00042  * @ingroup Exceptions
    00043  */
    00044 class SystemException: public oxt::tracable_exception {
    00045 private:
    00046         string briefMessage;
    00047         string systemMessage;
    00048         string fullMessage;
    00049         int m_code;
    00050 public:
    00051         /**
    00052          * Create a new SystemException.
    00053          *
    00054          * @param briefMessage A brief message describing the error.
    00055          * @param errorCode The error code, i.e. the value of errno right after the error occured.
    00056          * @note A system description of the error will be appended to the given message.
    00057          *    For example, if <tt>errorCode</tt> is <tt>EBADF</tt>, and <tt>briefMessage</tt>
    00058          *    is <em>"Something happened"</em>, then what() will return <em>"Something happened: Bad
    00059          *    file descriptor (10)"</em> (if 10 is the number for EBADF).
    00060          * @post code() == errorCode
    00061          * @post brief() == briefMessage
    00062          */
    00063         SystemException(const string &briefMessage, int errorCode) {
    00064                 stringstream str;
    00065
    00066                 str << strerror(errorCode) << " (" << errorCode << ")";
    00067                 systemMessage = str.str();
    00068
    00069                 setBriefMessage(briefMessage);
    00070                 m_code = errorCode;
    00071         }
    00072
    00073         virtual ~SystemException() throw() {}
    00074
    00075         virtual const char *what() const throw() {
    00076                 return fullMessage.c_str();
    00077         }
    00078
    00079         void setBriefMessage(const string &message) {
    00080                 briefMessage = message;
    00081                 fullMessage = briefMessage + ": " + systemMessage;
    00082         }
    00083
    00084         /**
    00085          * The value of <tt>errno</tt> at the time the error occured.
    00086          */
    00087         int code() const throw() {
    00088                 return m_code;
    00089         }
    00090
    00091         /**
    00092          * Returns a brief version of the exception message. This message does
    00093          * not include the system error description, and is equivalent to the
    00094          * value of the <tt>message</tt> parameter as passed to the constructor.
    00095          */
    00096         string brief() const throw() {
    00097                 return briefMessage;
    00098         }
    00099
    00100         /**
    00101          * Returns the system's error message. This message contains both the
    00102          * content of <tt>strerror(errno)</tt> and the errno number itself.
    00103          */
    00104         string sys() const throw() {
    00105                 return systemMessage;
    00106         }
    00107 };
    00108
    00109 /**
    00110  * A filesystem error, as returned by the operating system. This may include,
    00111  * for example, permission errors.
    00112  *
    00113  * @ingroup Exceptions
    00114  */
    00115 class FileSystemException: public SystemException {
    00116 private:
    00117         string m_filename;
    00118 public:
    00119         FileSystemException(const string &message, int errorCode,
    00120                 const string &filename)
    00121                 : SystemException(message, errorCode),
    00122                   m_filename(filename) {}
    00123
    00124         virtual ~FileSystemException() throw() {}
    00125
    00126         /**
    00127          * The filename that's associated to the error.
    00128          */
    00129         string filename() const throw() {
    00130                 return m_filename;
    00131         }
    00132 };
    00133
    00134 /**
    00135  * Represents an error that occured during an I/O operation.
    00136  *
    00137  * @ingroup Exceptions
    00138  */
    00139 class IOException: public oxt::tracable_exception {
    00140 private:
    00141         string msg;
    00142 public:
    00143         IOException(const string &message): msg(message) {}
    00144         virtual ~IOException() throw() {}
    00145         virtual const char *what() const throw() { return msg.c_str(); }
    00146 };
    00147
    00148 /**
    00149  * Thrown when a certain file cannot be found.
    00150  */
    00151 class FileNotFoundException: public IOException {
    00152 public:
    00153         FileNotFoundException(const string &message): IOException(message) {}
    00154         virtual ~FileNotFoundException() throw() {}
    00155 };
    00156
    00157 /**
    00158  * Thrown when an invalid configuration is given.
    00159  */
    00160 class ConfigurationException: public oxt::tracable_exception {
    00161 private:
    00162         string msg;
    00163 public:
    00164         ConfigurationException(const string &message): msg(message) {}
    00165         virtual ~ConfigurationException() throw() {}
    00166         virtual const char *what() const throw() { return msg.c_str(); }
    00167 };
    00168
    00169 /**
    00170  * Thrown when SpawnManager or ApplicationPool fails to spawn an application
    00171  * instance. The exception may contain an error page, which is a user-friendly
    00172  * HTML page with details about the error.
    00173  */
    00174 class SpawnException: public oxt::tracable_exception {
    00175 private:
    00176         string msg;
    00177         bool m_hasErrorPage;
    00178         string m_errorPage;
    00179 public:
    00180         SpawnException(const string &message)
    00181                 : msg(message) {
    00182                 m_hasErrorPage = false;
    00183         }
    00184
    00185         SpawnException(const string &message, const string &errorPage)
    00186                 : msg(message), m_errorPage(errorPage) {
    00187                 m_hasErrorPage = true;
    00188         }
    00189
    00190         virtual ~SpawnException() throw() {}
    00191         virtual const char *what() const throw() { return msg.c_str(); }
    00192
    00193         /**
    00194          * Check whether an error page is available.
    00195          */
    00196         bool hasErrorPage() const {
    00197                 return m_hasErrorPage;
    00198         }
    00199
    00200         /**
    00201          * Return the error page content.
    00202          *
    00203          * @pre hasErrorPage()
    00204          */
    00205         const string getErrorPage() const {
    00206                 return m_errorPage;
    00207         }
    00208 };
    00209
    00210 /**
    00211  * A generic runtime exception.
    00212  *
    00213  * @ingroup Exceptions
    00214  */
    00215 class RuntimeException: public oxt::tracable_exception {
    00216 private:
    00217         string msg;
    00218 public:
    00219         RuntimeException(const string &message): msg(message) {}
    00220         virtual ~RuntimeException() throw() {}
    00221         virtual const char *what() const throw() { return msg.c_str(); }
    00222 };
    00223
    00224 /**
    00225  * The application pool is too busy and cannot fulfill a get() request.
    00226  *
    00227  * @ingroup Exceptions
    00228  */
    00229 class BusyException: public oxt::tracable_exception {
    00230 private:
    00231         string msg;
    00232 public:
    00233         BusyException(const string &message): msg(message) {}
    00234         virtual ~BusyException() throw() {}
    00235         virtual const char *what() const throw() { return msg.c_str(); }
    00236 };
    00237
    00238 } // namespace Passenger
    00239
    00240 #endif /* _PASSENGER_EXCEPTIONS_H_ */