From 833f3da5dbc1d650078f364ce21d0e599265907b Mon Sep 17 00:00:00 2001 From: Valentino Orlandi Date: Sat, 3 Feb 2024 17:53:45 +0100 Subject: [PATCH] Improved exceptions Added LogDoctorException, ConversionException and DatabaseException --- logdoctor/modules/exceptions.cpp | 42 +++++++++++++++++++++++++----- logdoctor/modules/exceptions.h | 44 ++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 7 deletions(-) diff --git a/logdoctor/modules/exceptions.cpp b/logdoctor/modules/exceptions.cpp index 8b1b96ce..f7ff7bb4 100644 --- a/logdoctor/modules/exceptions.cpp +++ b/logdoctor/modules/exceptions.cpp @@ -3,14 +3,21 @@ #include +#include + + +DoNotCatchException::DoNotCatchException( const char* msg ) +{ + std::cerr << "LogDoctor: Exception: " << msg << std::endl; +} + ///////////////// //// GENERIC //// GenericException::GenericException( const std::string& msg, const bool to_sys ) { if ( to_sys ) { // when sys, leave un-catched - std::cout << "LogDoctor: Exception: " << msg << std::endl; - std::cerr << "LogDoctor: Exception: " << msg << std::endl; + std::cerr << "LogDoctor: GenericException: " << msg << std::endl; } else { this->msg = QString::fromStdString( msg ); } @@ -25,7 +32,6 @@ const QString& GenericException::what() //// WEB SERVER //// WebServerException::WebServerException( const std::string& msg ) // leave un-catched { - std::cout << "LogDoctor: WebServerException: " << msg << std::endl; std::cerr << "LogDoctor: WebServerException: " << msg << std::endl; /*this->msg = QString::fromStdString( msg );*/ } @@ -39,7 +45,6 @@ WebServerException::WebServerException( const std::string& msg ) // leave un-cat //// LOG FORMAT //// LogFormatException::LogFormatException( const std::string& msg ) { - std::cout << "LogDoctor: LogFormatException: " << msg << std::endl; std::cerr << "LogDoctor: LogFormatException: " << msg << std::endl; this->msg = QString::fromStdString( msg ); } @@ -53,7 +58,6 @@ const QString& LogFormatException::what() //// LOG PARSER //// LogParserException::LogParserException( const std::string& txt , const std::string& val ) { - std::cout << "LogDoctor: LogParserException: " << txt << ": '" << val << "'" << std::endl; std::cerr << "LogDoctor: LogParserException: " << txt << ": '" << val << "'" << std::endl; this->msg = QString("%1:\n'%2'").arg( QString::fromStdString( txt ), @@ -69,7 +73,6 @@ const QString& LogParserException::what() //// DATE-TIME //// DateTimeException::DateTimeException( const std::string& msg ) // leave un-catched { - std::cout << "LogDoctor: DateTimeException: " << msg << std::endl; std::cerr << "LogDoctor: DateTimeException: " << msg << std::endl; /*this->msg = QString::fromStdString( msg );*/ } @@ -79,11 +82,23 @@ DateTimeException::DateTimeException( const std::string& msg ) // leave un-catch }*/ +//////////////////// +//// CONVERSION //// +ConversionException::ConversionException( const std::string& msg ) +{ + //std::cerr << "LogDoctor: ConversionException: " << msg << std::endl; + this->msg = QString::fromStdString( msg ); +} +const QString& ConversionException::what() +{ + return msg; +} + + ////////////////////////// //// BLACK/WARN LISTS //// BWlistException::BWlistException( const std::string& msg ) { - std::cout << "LogDoctor: BWlistException: " << msg << std::endl; std::cerr << "LogDoctor: BWlistException: " << msg << std::endl; /*this->msg = QString::fromStdString( msg );*/ } @@ -91,3 +106,16 @@ BWlistException::BWlistException( const std::string& msg ) { return msg; }*/ + + +////////////////// +//// DATABASE //// +DatabaseException::DatabaseException( QString&& msg ) + : msg{ std::move(msg) } +{ + +} +const QString& DatabaseException::what() const noexcept +{ + return msg; +} diff --git a/logdoctor/modules/exceptions.h b/logdoctor/modules/exceptions.h index 364c4709..719ba36b 100644 --- a/logdoctor/modules/exceptions.h +++ b/logdoctor/modules/exceptions.h @@ -7,6 +7,22 @@ #include +//! LogDoctorException +/*! + Base class for some internal exceptions. + Used on its own when a message has already been shown, + or there is no need to show one. + \see CrapviewException, DatabaseException + */ +class LogDoctorException {}; + + +struct DoNotCatchException final +{ + explicit DoNotCatchException( const char* msg ); +}; + + //! GenericException /*! Generic exception for general purposes @@ -82,6 +98,20 @@ private: }; +//! ConversionException +/*! + Exception related to failure in converting something +*/ +class ConversionException final : public std::exception { +public: + explicit ConversionException( const std::string& msg ); + const QString& what(); + +private: + QString msg; +}; + + //! BWlistException /*! Exception related to a blacklist/warnlist @@ -97,4 +127,18 @@ private: }; +//! DatabaseException +/*! + Exception related to the database +*/ +class DatabaseException final : public LogDoctorException { +public: + explicit DatabaseException( QString&& msg ); + const QString& what() const noexcept; + +private: + QString msg; +}; + + #endif // LOGDOCTOR__EXCEPTIONS_H