Show
Ignore:
Timestamp:
08/17/09 09:25:19 (15 years ago)
Author:
vbarta
Message:

split UIException into 3

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/base/user_info.h

    r535 r540  
    3434namespace bdm { 
    3535 
    36 //! Exception prepared for reporting user-info errors which are always related to some concrete Setting path 
     36//! Generic exception for reporting configuration errors 
    3737//! 
    3838//!  \ref ui_page 
    3939class UIException : public std::exception { 
    40  
    41 public: 
     40private: 
    4241        //! Error message 
    4342        const string message; 
    4443 
    45         //! Path to the problematic setting 
    46         const string path; 
    47  
    48 private: 
    49         string what_message; 
    50  
     44public: 
     45        /*! 
     46          \brief The constructor 
     47          \param message the reason for throwing the exception. Should be a complete English sentence (or a couple sentences), starting with "UIException: ". 
     48        */ 
     49        UIException ( const string &message ) : 
     50                message ( message ) { 
     51        } 
     52 
     53        //! Overriden method for reporting the error message 
     54        virtual const char* what() const throw() { 
     55                return message.c_str(); 
     56        } 
     57 
     58        ~UIException() throw() {}; 
     59 
     60protected: 
     61        /*! 
     62          Formats error messages for derived classes, which use a 
     63          Setting path in addition to the message. 
     64        */ 
     65        static string format_message( const string &reason, const string &path ); 
     66}; 
     67 
     68//! Exception for reporting configuration errors related to some concrete Setting path 
     69//! 
     70//!  \ref ui_page 
     71class UISettingException : public UIException { 
    5172public: 
    5273        //! Use this constructor when you can pass the problematical Setting as a parameter 
    53         UIException ( const string &message, const Setting &element ) 
    54                         : message ( "UI error: " + message + "." ), path ( "Check path \"" + string ( element.getPath() ) + "\"." ) { 
    55                 init_what_message(); 
     74        UISettingException ( const string &message, const Setting &element ): 
     75                UIException ( format_message ( message, string ( element.getPath() ) ) ) { 
    5676        } 
    5777 
    5878        //! This constructor is for other occasions, when only path of problematical Setting is known 
    59         UIException ( const string &message, const string &path ) 
    60                         : message ( "UI error: " + message + "." ), path ( "Check path \"" + path + "\"." ) { 
    61                 init_what_message(); 
    62         } 
    63  
    64         //! Overriden method for reporting an error message 
    65         virtual const char* what() const throw() { 
    66                 return what_message.c_str(); 
    67         } 
    68  
    69         ~UIException() throw() {}; 
    70  
    71 private: 
    72         void init_what_message() { 
    73                 what_message = message; 
    74                 what_message += ' '; 
    75                 what_message += path; 
    76         } 
     79        UISettingException ( const string &message, const string &path ): 
     80                UIException ( format_message ( message, path ) ) { 
     81        } 
     82 
     83        ~UISettingException() throw() {}; 
    7784}; 
    7885 
     86//! Exception for reporting configuration errors in the "class" attribute 
     87//! 
     88//!  \ref ui_page 
     89class UIClassException : public UIException { 
     90public: 
     91        //! Use this constructor when you can pass the problematical Setting as a parameter 
     92        UIClassException ( const string &message, const Setting &element ): 
     93                UIException ( format_message ( message, string ( element.getPath() ) ) ) { 
     94        } 
     95 
     96        //! This constructor is for other occasions, when only path of problematical Setting is known 
     97        UIClassException ( const string &message, const string &path ): 
     98                UIException ( format_message ( message, path ) ) { 
     99        } 
     100 
     101        ~UIClassException() throw() {}; 
     102}; 
    79103 
    80104/*! 
     
    291315                string class_name; 
    292316                if ( !link.result.lookupValue ( "class", class_name ) ) 
    293                         throw UIException ( "the obligatory \"class\" identifier is missing", link.result ); 
     317                        throw UIClassException ( "UIException: the obligatory \"class\" identifier is missing.", link.result ); 
    294318 
    295319                // then we find a user-info related to this type 
     
    302326                if ( !instance ) { 
    303327                        delete typeless_instance; 
    304                         throw UIException ( "class " + class_name + " is not a descendant of the desired output class. Try to call the UI::build<T> function with a different type parameter.", link.result ); 
     328                        throw UIClassException ( "UIException: class " + class_name + " is not a descendant of the desired output class. Try to call the UI::build<T> function with a different type parameter.", link.result ); 
    305329                } 
    306330 
     
    308332                        instance->from_setting ( link.result ); 
    309333                } catch ( SettingException sttng_xcptn ) { 
    310                         string msg = "the method " + class_name + ".from_setting(Setting&) has thrown a SettingException. Try to correct this method. Check path \"" + sttng_xcptn.getPath() + "\"."; 
    311                         throw UIException(msg, link.result); 
     334                        string msg = "UIException: method "; 
     335                        msg += class_name; 
     336                        msg += ".from_setting(Setting&) has thrown a SettingException."; 
     337                        throw UISettingException(msg, sttng_xcptn.getPath()); 
    312338                } 
    313339        } 
     
    342368        //! The exception can help to find the place where the template is misused and also to correct it. 
    343369        template<class T> static void from_setting ( T &variable_to_load, const Setting &element ) { 
    344                 std::string msg = "from_setting is not implemented for type "; 
     370                std::string msg = "UIException: from_setting is not implemented for type "; 
    345371                msg += typeid(T).name(); 
    346                 throw UIException ( msg, element ); 
     372                msg += '.'; 
     373                throw UISettingException ( msg, element ); 
    347374        } 
    348375 
     
    372399                                return shared_ptr<T>(); 
    373400                        else 
    374                                 throw UIException ( "the compulsory Setting named \"" + name + "\" is missing", element ); 
     401                                throw UISettingException ( "UIException: the compulsory Setting named \"" + name + "\" is missing.", element ); 
    375402                } 
    376403 
     
    391418                                stringstream stream; 
    392419                                stream << index; 
    393                                 throw UIException ( "the compulsory Setting with the index " + stream.str() + " is missing", element ); 
     420                                throw UISettingException ( "UIException: the compulsory Setting with the index " + stream.str() + " is missing.", element ); 
    394421                        } 
    395422                } 
     
    413440                                return false; 
    414441                        else 
    415                                 throw UIException ( "the compulsory Setting named \"" + name + "\" is missing", element ); 
     442                                throw UISettingException ( "UIException: the compulsory Setting named \"" + name + "\" is missing.", element ); 
    416443                } 
    417444 
     
    428455                        else { 
    429456                                stringstream stream; 
     457                                stream << "UIException: the compulsory Setting with the index " << index << " is missing."; 
    430458                                stream << index; 
    431                                 throw UIException ( "the compulsory Setting with the index " + stream.str() + " is missing", element ); 
     459                                throw UISettingException (stream.str(), element ); 
    432460                        } 
    433461                } 
     
    494522                        instance->to_setting ( set ); 
    495523                } catch ( SettingException sttng_xcptn ) { 
    496                     string msg = "the method "; 
    497                     msg += class_name; 
    498                     msg += ".to_setting(Setting&) has thrown a SettingException. Try to correct this method."; 
    499                     throw UIException(msg, sttng_xcptn.getPath()); 
     524                        string msg = "UIException: method "; 
     525                        msg += class_name; 
     526                        msg += ".to_setting(Setting&) has thrown a SettingException."; 
     527                        throw UISettingException(msg, sttng_xcptn.getPath()); 
    500528                } 
    501529        }