From ae4009968bd0cabd2d79739dcdf76245b7107628 Mon Sep 17 00:00:00 2001 From: Stephane Albert <stephane.albert@c-s.fr> Date: Wed, 27 Feb 2013 17:31:07 +0100 Subject: [PATCH] ENH: Added SystemError exception; Throw SystemError exceptions when creating Dataset directory. --- Code/Application/mvdApplication.cxx | 2 +- Code/Common/mvdDatasetModel.cxx | 24 +++++- Code/Common/mvdSystemError.cxx | 125 +++++++++++++++++++++++++++ Code/Common/mvdSystemError.h | 127 ++++++++++++++++++++++++++++ 4 files changed, 275 insertions(+), 3 deletions(-) create mode 100644 Code/Common/mvdSystemError.cxx create mode 100644 Code/Common/mvdSystemError.h diff --git a/Code/Application/mvdApplication.cxx b/Code/Application/mvdApplication.cxx index 90b94997cb..2dc71a16fc 100644 --- a/Code/Application/mvdApplication.cxx +++ b/Code/Application/mvdApplication.cxx @@ -96,7 +96,7 @@ Application::LoadDatasetModel( const QString& imageFilename, throw; } - + return model; } diff --git a/Code/Common/mvdDatasetModel.cxx b/Code/Common/mvdDatasetModel.cxx index 2482461f91..4ae93a013e 100644 --- a/Code/Common/mvdDatasetModel.cxx +++ b/Code/Common/mvdDatasetModel.cxx @@ -25,6 +25,8 @@ // // System includes (sorted by alphabetic order) +#include <cerrno> +#include <exception> // // ITK includes (sorted by alphabetic order) @@ -34,6 +36,7 @@ // // Monteverdi includes (sorted by alphabetic order) +#include "mvdSystemError.h" #include "mvdVectorImageModel.h" namespace mvd @@ -70,7 +73,22 @@ DatasetModel if( !pathDir.exists() ) { - throw; + /* + qDebug() << "System error: " << errno << " -- '" << strerror( errno ) << "'"; + + QString message( + QString( "'%1': %2 '%3'" ) + .arg( path ) + .arg( errno ) + .arg( strerror( errno ) ) + ); + + qDebug() << "std::invalid_argument(" << message << ")"; + + throw std::invalid_argument( message.toAscii().constData() ); + */ + + throw SystemError( QString( "('%1')" ).arg( path ).toAscii().constData() ); } if( !pathDir.exists( name ) ) @@ -78,7 +96,9 @@ DatasetModel isEmpty = true; if( !pathDir.mkpath( name ) ) - throw; + { + throw SystemError( QString( "('%1')" ).arg( name ).toAscii().constData() ); + } // TODO: write empty descriptor.xml } diff --git a/Code/Common/mvdSystemError.cxx b/Code/Common/mvdSystemError.cxx new file mode 100644 index 0000000000..72cc9e39f9 --- /dev/null +++ b/Code/Common/mvdSystemError.cxx @@ -0,0 +1,125 @@ +/*========================================================================= + + Program: Monteverdi2 + Language: C++ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See Copyright.txt for details. + + Monteverdi2 is distributed under the CeCILL licence version 2. See + Licence_CeCILL_V2-en.txt or + http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt for more details. + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#include "mvdSystemError.h" + + +/*****************************************************************************/ +/* INCLUDE SECTION */ + +// +// Qt includes (sorted by alphabetic order) +//// Must be included before system/custom includes. +#include <QDebug> + +// +// System includes (sorted by alphabetic order) +#include <cerrno> +#include <cstring> +#include <sstream> + +// +// ITK includes (sorted by alphabetic order) + +// +// OTB includes (sorted by alphabetic order) + +// +// Monteverdi includes (sorted by alphabetic order) + + +namespace mvd +{ +/* + TRANSLATOR mvd::SystemError + + Necessary for lupdate to be aware of C++ namespaces. + + Context comment for translator. +*/ + +/*****************************************************************************/ +/* CLASS IMPLEMENTATION SECTION */ + +/*****************************************************************************/ +std::string +SystemError::whatString( int err, const std::string& msg ) +{ +#if 0 + // Use Qt's tr() in order for the text string to be translated. + // N.B.: strerror() string may use system locales. + return + ( msg.empty() + ? tr( "System error %1: '%2'." ) + .arg( err ) + .arg( strerror( err ) ) + + : tr( "System error %1: '%2' %3." ) + .arg( err ) + .arg( strerror( err ) ) + .arg( msg ) + + ).toAscii().constData(); + +#else + std::stringstream sstream( std::ios_base::out ); + + sstream + << "System error " + << err + << ": " + << strerror( err ) + << ( msg.empty() + ? "." + : " " + msg + "." ); + + qDebug() << sstream.str().c_str(); + + return sstream.str(); + +#endif +} + +/*******************************************************************************/ +SystemError +::SystemError( const std::string& message ) : + std::runtime_error( SystemError::whatString( errno, message ) ), + m_Message(), + m_ErrorCode( errno ) +{ + qDebug() << what(); +} + +/*******************************************************************************/ +SystemError +::SystemError( int errorCode, const std::string& message ) : + std::runtime_error( SystemError::whatString( errorCode, message ) ), + m_Message( message ), + m_ErrorCode( errorCode ) +{ + qDebug() << what(); +} + +/*******************************************************************************/ +SystemError +::~SystemError() + throw() +{ +} + +} // end namespace 'mvd' diff --git a/Code/Common/mvdSystemError.h b/Code/Common/mvdSystemError.h new file mode 100644 index 0000000000..bdc94f2520 --- /dev/null +++ b/Code/Common/mvdSystemError.h @@ -0,0 +1,127 @@ +/*========================================================================= + + Program: Monteverdi2 + Language: C++ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See Copyright.txt for details. + + Monteverdi2 is distributed under the CeCILL licence version 2. See + Licence_CeCILL_V2-en.txt or + http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt for more details. + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#ifndef __mvdSystemError_h +#define __mvdSystemError_h + +// +// Configuration include. +//// Included at first position before any other ones. +#include "ConfigureMonteverdi2.h" + + +/*****************************************************************************/ +/* INCLUDE SECTION */ + +// +// Qt includes (sorted by alphabetic order) +//// Must be included before system/custom includes. + +// +// System includes (sorted by alphabetic order) +#include <stdexcept> + +// +// ITK includes (sorted by alphabetic order) + +// +// OTB includes (sorted by alphabetic order) + +// +// Monteverdi includes (sorted by alphabetic order) + +/*****************************************************************************/ +/* PRE-DECLARATION SECTION */ + +// +// External classes pre-declaration. +namespace +{ +} + +namespace mvd +{ +// +// Internal classes pre-declaration. + + +/*****************************************************************************/ +/* CLASS DEFINITION SECTION */ + +/** + * \class SystemError + * + * \brief WIP. + */ +class Monteverdi2_EXPORT SystemError : + public std::runtime_error +{ + + /*-[ PUBLIC SECTION ]------------------------------------------------------*/ + +// +// Public methods. +public: + + /** \brief Constructor. */ + SystemError( const std::string& message =std::string() ); + + /** + */ + SystemError( int errorCode, const std::string& message =std::string() ); + + /** \brief Destructor. */ + virtual ~SystemError() throw(); + + /*-[ PROTECTED SECTION ]---------------------------------------------------*/ + +// +// Protected methods. +protected: + +// +// Protected attributes. +protected: + + /*-[ PRIVATE SECTION ]-----------------------------------------------------*/ + +// +// Private methods. +private: + /** + */ + static std::string whatString( int errorCode, const std::string& message ); + +// +// Private attributes. +private: + std::string m_Message; + int m_ErrorCode; + +}; + +} // end namespace 'mvd'. + +/*****************************************************************************/ +/* INLINE SECTION */ + +namespace mvd +{ +} // end namespace 'mvd' + +#endif // __mvdSystemError_h -- GitLab