Exceptions.h

00001 /*
00002  *  Phusion Passenger - http://www.modrails.com/
00003  *  Copyright (C) 2008  Phusion
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; version 2 of the License.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License along
00015  *  with this program; if not, write to the Free Software Foundation, Inc.,
00016  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00017  */
00018 #ifndef _PASSENGER_EXCEPTIONS_H_
00019 #define _PASSENGER_EXCEPTIONS_H_
00020 
00021 #include <exception>
00022 #include <string>
00023 #include <sstream>
00024 
00025 /**
00026  * @defgroup Exceptions Exceptions
00027  */
00028 
00029 namespace Passenger {
00030 
00031 using namespace std;
00032 
00033 /**
00034  * Represents an error returned by a system call or a standard library call.
00035  *
00036  * Use the code() method to find out the value of <tt>errno</tt> at the time
00037  * the error occured.
00038  *
00039  * @ingroup Exceptions
00040  */
00041 class SystemException: public exception {
00042 private:
00043         string briefMessage;
00044         string systemMessage;
00045         string fullMessage;
00046         int m_code;
00047 public:
00048         /**
00049          * Create a new SystemException.
00050          *
00051          * @param message A message describing the error.
00052          * @param errorCode The error code, i.e. the value of errno right after the error occured.
00053          * @note A system description of the error will be appended to the given message.
00054          *    For example, if <tt>errorCode</tt> is <tt>EBADF</tt>, and <tt>message</tt>
00055          *    is <em>"Something happened"</em>, then what() will return <em>"Something happened: Bad
00056          *    file descriptor (10)"</em> (if 10 is the number for EBADF).
00057          * @post code() == errorCode
00058          * @post brief() == message
00059          */
00060         SystemException(const string &message, int errorCode) {
00061                 stringstream str;
00062                 
00063                 briefMessage = message;
00064                 str << strerror(errorCode) << " (" << errorCode << ")";
00065                 systemMessage = str.str();
00066                 
00067                 fullMessage = briefMessage + ": " + systemMessage;
00068                 m_code = errorCode;
00069         }
00070         
00071         virtual ~SystemException() throw() {}
00072         
00073         virtual const char *what() const throw() {
00074                 return fullMessage.c_str();
00075         }
00076         
00077         /**
00078          * The value of <tt>errno</tt> at the time the error occured.
00079          */
00080         int code() const throw() {
00081                 return m_code;
00082         }
00083         
00084         /**
00085          * Returns a brief version of the exception message. This message does
00086          * not include the system error description, and is equivalent to the
00087          * value of the <tt>message</tt> parameter as passed to the constructor.
00088          */
00089         string brief() const throw() {
00090                 return briefMessage;
00091         }
00092         
00093         /**
00094          * Returns the system's error message. This message contains both the
00095          * content of <tt>strerror(errno)</tt> and the errno number itself.
00096          */
00097         string sys() const throw() {
00098                 return systemMessage;
00099         }
00100 };
00101 
00102 /**
00103  * Represents an error that occured during an I/O operation.
00104  *
00105  * @ingroup Exceptions
00106  */
00107 class IOException: public exception {
00108 private:
00109         string msg;
00110 public:
00111         IOException(const string &message): msg(message) {}
00112         virtual ~IOException() throw() {}
00113         virtual const char *what() const throw() { return msg.c_str(); }
00114 };
00115 
00116 /**
00117  * Thrown when a certain file cannot be found.
00118  */
00119 class FileNotFoundException: public IOException {
00120 public:
00121         FileNotFoundException(const string &message): IOException(message) {}
00122         virtual ~FileNotFoundException() throw() {}
00123 };
00124 
00125 /**
00126  * Thrown when SpawnManager or ApplicationPool fails to spawn an application
00127  * instance. The exception may contain an error page, which is a user-friendly
00128  * HTML page with details about the error.
00129  */
00130 class SpawnException: public exception {
00131 private:
00132         string msg;
00133         bool m_hasErrorPage;
00134         string m_errorPage;
00135 public:
00136         SpawnException(const string &message)
00137                 : msg(message) {
00138                 m_hasErrorPage = false;
00139         }
00140         
00141         SpawnException(const string &message, const string &errorPage)
00142                 : msg(message), m_errorPage(errorPage) {
00143                 m_hasErrorPage = true;
00144         }
00145         
00146         virtual ~SpawnException() throw() {}
00147         virtual const char *what() const throw() { return msg.c_str(); }
00148         
00149         /**
00150          * Check whether an error page is available.
00151          */
00152         bool hasErrorPage() const {
00153                 return m_hasErrorPage;
00154         }
00155         
00156         /**
00157          * Return the error page content.
00158          *
00159          * @pre hasErrorPage()
00160          */
00161         const string getErrorPage() const {
00162                 return m_errorPage;
00163         }
00164 };
00165 
00166 } // namespace Passenger
00167 
00168 #endif /* _PASSENGER_EXCEPTIONS_H_ */

Generated on Fri Apr 11 18:16:04 2008 for Passenger by  doxygen 1.5.3