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 <exception>
00024 #include <string>
00025 #include <sstream>
00026 
00027 /**
00028  * @defgroup Exceptions Exceptions
00029  */
00030 
00031 namespace Passenger {
00032 
00033 using namespace std;
00034 
00035 /**
00036  * Represents an error returned by a system call or a standard library call.
00037  *
00038  * Use the code() method to find out the value of <tt>errno</tt> at the time
00039  * the error occured.
00040  *
00041  * @ingroup Exceptions
00042  */
00043 class SystemException: public exception {
00044 private:
00045         string briefMessage;
00046         string systemMessage;
00047         string fullMessage;
00048         int m_code;
00049 public:
00050         /**
00051          * Create a new SystemException.
00052          *
00053          * @param message A message describing the error.
00054          * @param errorCode The error code, i.e. the value of errno right after the error occured.
00055          * @note A system description of the error will be appended to the given message.
00056          *    For example, if <tt>errorCode</tt> is <tt>EBADF</tt>, and <tt>message</tt>
00057          *    is <em>"Something happened"</em>, then what() will return <em>"Something happened: Bad
00058          *    file descriptor (10)"</em> (if 10 is the number for EBADF).
00059          * @post code() == errorCode
00060          * @post brief() == message
00061          */
00062         SystemException(const string &message, int errorCode) {
00063                 stringstream str;
00064                 
00065                 briefMessage = message;
00066                 str << strerror(errorCode) << " (" << errorCode << ")";
00067                 systemMessage = str.str();
00068                 
00069                 fullMessage = briefMessage + ": " + systemMessage;
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         /**
00080          * The value of <tt>errno</tt> at the time the error occured.
00081          */
00082         int code() const throw() {
00083                 return m_code;
00084         }
00085         
00086         /**
00087          * Returns a brief version of the exception message. This message does
00088          * not include the system error description, and is equivalent to the
00089          * value of the <tt>message</tt> parameter as passed to the constructor.
00090          */
00091         string brief() const throw() {
00092                 return briefMessage;
00093         }
00094         
00095         /**
00096          * Returns the system's error message. This message contains both the
00097          * content of <tt>strerror(errno)</tt> and the errno number itself.
00098          */
00099         string sys() const throw() {
00100                 return systemMessage;
00101         }
00102 };
00103 
00104 /**
00105  * A filesystem error, as returned by the operating system. This may include,
00106  * for example, permission errors.
00107  *
00108  * @ingroup Exceptions
00109  */
00110 class FileSystemException: public SystemException {
00111 private:
00112         string m_filename;
00113 public:
00114         FileSystemException(const string &message, int errorCode,
00115                 const string &filename)
00116                 : SystemException(message, errorCode),
00117                   m_filename(filename) {}
00118         
00119         virtual ~FileSystemException() throw() {}
00120         
00121         /**
00122          * The filename that's associated to the error.
00123          */
00124         string filename() const throw() {
00125                 return m_filename;
00126         }
00127 };
00128 
00129 /**
00130  * Represents an error that occured during an I/O operation.
00131  *
00132  * @ingroup Exceptions
00133  */
00134 class IOException: public exception {
00135 private:
00136         string msg;
00137 public:
00138         IOException(const string &message): msg(message) {}
00139         virtual ~IOException() throw() {}
00140         virtual const char *what() const throw() { return msg.c_str(); }
00141 };
00142 
00143 /**
00144  * Thrown when a certain file cannot be found.
00145  */
00146 class FileNotFoundException: public IOException {
00147 public:
00148         FileNotFoundException(const string &message): IOException(message) {}
00149         virtual ~FileNotFoundException() throw() {}
00150 };
00151 
00152 /**
00153  * Thrown when an invalid configuration is given.
00154  */
00155 class ConfigurationException: public exception {
00156 private:
00157         string msg;
00158 public:
00159         ConfigurationException(const string &message): msg(message) {}
00160         virtual ~ConfigurationException() throw() {}
00161         virtual const char *what() const throw() { return msg.c_str(); }
00162 };
00163 
00164 /**
00165  * Thrown when SpawnManager or ApplicationPool fails to spawn an application
00166  * instance. The exception may contain an error page, which is a user-friendly
00167  * HTML page with details about the error.
00168  */
00169 class SpawnException: public exception {
00170 private:
00171         string msg;
00172         bool m_hasErrorPage;
00173         string m_errorPage;
00174 public:
00175         SpawnException(const string &message)
00176                 : msg(message) {
00177                 m_hasErrorPage = false;
00178         }
00179         
00180         SpawnException(const string &message, const string &errorPage)
00181                 : msg(message), m_errorPage(errorPage) {
00182                 m_hasErrorPage = true;
00183         }
00184         
00185         virtual ~SpawnException() throw() {}
00186         virtual const char *what() const throw() { return msg.c_str(); }
00187         
00188         /**
00189          * Check whether an error page is available.
00190          */
00191         bool hasErrorPage() const {
00192                 return m_hasErrorPage;
00193         }
00194         
00195         /**
00196          * Return the error page content.
00197          *
00198          * @pre hasErrorPage()
00199          */
00200         const string getErrorPage() const {
00201                 return m_errorPage;
00202         }
00203 };
00204 
00205 /**
00206  * The application pool is too busy and cannot fulfill a get() request.
00207  *
00208  * @ingroup Exceptions
00209  */
00210 class BusyException: public exception {
00211 private:
00212         string msg;
00213 public:
00214         BusyException(const string &message): msg(message) {}
00215         virtual ~BusyException() throw() {}
00216         virtual const char *what() const throw() { return msg.c_str(); }
00217 };
00218 
00219 } // namespace Passenger
00220 
00221 #endif /* _PASSENGER_EXCEPTIONS_H_ */

Generated on Fri Jan 23 08:28:57 2009 for Passenger by  doxygen 1.4.7