Changeset 540

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

split UIException into 3

Location:
library/bdm/base
Files:
2 modified

Legend:

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

    r493 r540  
    1414 
    1515namespace bdm { 
     16 
     17string UIException::format_message( const string &reason, const string &path ) { 
     18        stringstream ss; 
     19        ss << reason; 
     20        ss << " Check path \"" << path << "\"."; 
     21        return ss.str(); 
     22} 
     23 
    1624///////////////////////////// Class UIFile ///////////////////////////////////////////// 
    1725 
     
    132140 
    133141        if ( !result->exists ( link ) ) 
    134                 throw UIException ( "linked setting was not found", potential_link ); 
     142                throw UISettingException ( "UIException: linked setting was not found.", string ( ( const char* ) potential_link ) ); 
    135143 
    136144        return ( *result ) [link]; 
     
    145153void UI::assert_type ( const Setting &element, Setting::Type type ) { 
    146154        if ( element.getType() != type ) 
    147                 throw UIException ( "wrong setting type", element ); 
     155                throw UISettingException ( "UIException: wrong setting type.", element ); 
    148156} 
    149157 
    150158const Setting& UI::to_child_setting ( const Setting &element, const int index ) { 
    151159        if ( !element.isList() ) 
    152                 throw UIException ( "only TypeList elements could be indexed by integers", element ); 
     160                throw UISettingException ( "UIException: only TypeList elements could be indexed by integers.", element ); 
    153161 
    154162        return element[index]; 
     
    157165const Setting& UI::to_child_setting ( const Setting &element, const string &name ) { 
    158166        if ( !element.isGroup() ) 
    159                 throw UIException ( "only TypeGroup elements could be indexed by strings", element ); 
     167                throw UISettingException ( "UIException: only TypeGroup elements could be indexed by strings.", element ); 
    160168 
    161169        return element[name]; 
     
    236244                        const char* elem1 = ( const char* ) link.result[0]; 
    237245                        if ( ( strcmp ( elem1, "matrix" ) ) ) 
    238                                 throw UIException ( "the setting supposed to represent a matrix element has wrong syntax", link.result ); 
     246                                throw UISettingException ( "UIException: the setting supposed to represent a matrix element has wrong syntax.", link.result ); 
    239247 
    240248                        data_offset = 1; 
    241249                } else 
    242                         throw UIException ( "the setting supposed to represent a matrix element has wrong syntax", link.result ); 
     250                        throw UISettingException ( "UIException: the setting supposed to represent a matrix element has wrong syntax.", link.result ); 
    243251 
    244252                Setting &rows_setting = link.result[0 + data_offset]; 
     
    254262 
    255263                if ( cols < 0 || rows < 0 ) 
    256                         throw UIException ( "the dimensions of a matrix has to be non-negative", link.result ); 
     264                        throw UISettingException ( "UIException: the dimensions of a matrix has to be non-negative.", link.result ); 
    257265 
    258266                if ( elements.getLength() != cols * rows ) 
    259                         throw UIException ( "the count of the matrix elements is incompatible with matrix dimension", elements ); 
     267                        throw UISettingException ( "UIException: the count of the matrix elements is incompatible with matrix dimension.", elements ); 
    260268 
    261269                matrix.set_size ( rows, cols ); 
     
    265273 
    266274                if ( !elements[0].isNumber() ) 
    267                         throw UIException ( "matrix elements have to be numbers", elements[0] ); 
     275                        throw UISettingException ( "UIException: matrix elements have to be numbers.", elements[0] ); 
    268276 
    269277                // build matrix row-wise 
     
    275283        } 
    276284 
    277         throw UIException ( "only numeric types or TypeList are supported as matrix values", link.result ); 
     285        throw UISettingException ( "UIException: only numeric types or TypeList are supported as matrix values.", link.result ); 
    278286} 
    279287 
     
    292300 
    293301                if ( matrix.cols() != 1 && matrix.rows() != 1 ) 
    294                         throw UIException ( "the vector length is invalid, it seems to be rather a matrix", link.result ); 
     302                        throw UISettingException ( "UIException: the vector length is invalid, it seems to be rather a matrix.", link.result ); 
    295303 
    296304                int len = matrix.rows() * matrix.cols(); 
     
    313321 
    314322                if ( !link.result[0].isNumber() ) 
    315                         throw UIException ( "a vector element has to be a number", link.result[0] ); 
     323                        throw UISettingException ( "UIException: a vector element has to be a number.", link.result[0] ); 
    316324 
    317325                for ( int i = 0; i < len; i++ ) 
     
    321329        } 
    322330 
    323         throw UIException ( "only numeric types, TypeArray or TypeList are supported as vector values", link.result ); 
     331        throw UISettingException ( "UIException: only numeric types, TypeArray or TypeList are supported as vector values.", link.result ); 
    324332} 
    325333 
  • 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        }