refactor exceptions

This commit is contained in:
Johann Dreo 2020-03-26 22:46:52 +01:00
commit 6aeb74f6e0

View file

@ -27,26 +27,31 @@ Johann Dréo <johann.dreo@thalesgroup.com>
#include <ctime> #include <ctime>
#include <stdexcept> #include <stdexcept>
#include <sstream> #include <sstream>
#include <string>
//! You can catch this base exception if you want to catch anything thrown by ParadisEO. @ingroup Core //! You can catch this base exception if you want to catch anything thrown by ParadisEO. @ingroup Core
class eoException : public std::runtime_error class eoException : public std::runtime_error
{ {
public: public:
eoException(std::string msg = "") : eoException(std::string msg = "") :
std::runtime_error(msg) std::runtime_error(""),
_msg(msg)
{ }
virtual std::string message() const throw()
{ {
_msg << msg; return _msg;
} }
const char* what() const throw() const char* what() const throw()
{ {
return _msg.str().c_str(); return message().c_str();
} }
~eoException() throw() {} ~eoException() throw() {}
protected: protected:
std::ostringstream _msg; const std::string _msg;
}; };
/** Base class for exceptions which need to stop the algorithm to be handled /** Base class for exceptions which need to stop the algorithm to be handled
@ -98,12 +103,22 @@ class eoMaxTimeException : public eoMaxException
{ {
public: public:
eoMaxTimeException( time_t elapsed) : eoMaxTimeException( time_t elapsed) :
eoMaxException("STOP") eoMaxException(),
_elapsed(elapsed)
{ }
virtual std::string message() const throw()
{ {
_msg << " the maximum number of allowed seconds has been reached (" std::ostringstream msg;
<< elapsed << ")"; msg << "STOP because the maximum number of allowed seconds has been reached ("
<< _elapsed << ")";
return msg.str();
} }
~eoMaxTimeException() throw() {} ~eoMaxTimeException() throw() {}
protected:
const time_t _elapsed;
}; };
@ -118,12 +133,22 @@ class eoMaxEvalException : public eoMaxException
{ {
public: public:
eoMaxEvalException(unsigned long threshold) : eoMaxEvalException(unsigned long threshold) :
eoMaxException("STOP") eoMaxException(),
_threshold(threshold)
{ }
virtual std::string message() const throw()
{ {
_msg << " the maximum number of evaluation has been reached (" std::ostringstream msg;
<< threshold << ")."; msg << " the maximum number of evaluation has been reached ("
<< _threshold << ").";
return msg.str();
} }
~eoMaxEvalException() throw() {} ~eoMaxEvalException() throw() {}
protected:
const unsigned long _threshold;
}; };
//! Base class for exceptions related to eoParam management. @ingroup Parameters //! Base class for exceptions related to eoParam management. @ingroup Parameters
@ -144,11 +169,21 @@ class eoMissingParamException : public eoParamException
{ {
public: public:
eoMissingParamException(std::string name) : eoMissingParamException(std::string name) :
eoParamException() eoParamException(),
_name(name)
{ }
virtual std::string message() const throw()
{ {
_msg << "The command parameter " << name << " has not been declared"; std::ostringstream msg;
msg << "The command parameter " << _name << " has not been declared";
return msg.str();
} }
~eoMissingParamException() throw() {} ~eoMissingParamException() throw() {}
protected:
const std::string _name;
}; };
@ -163,12 +198,22 @@ class eoWrongParamTypeException : public eoParamException
{ {
public: public:
eoWrongParamTypeException(std::string name) : eoWrongParamTypeException(std::string name) :
eoParamException() eoParamException(),
_name(name)
{ }
virtual std::string message() const throw()
{ {
_msg << "You asked for the parameter " << name std::ostringstream msg;
msg << "You asked for the parameter " << _name
<< " but it has not been declared under this type"; << " but it has not been declared under this type";
return msg.str();
} }
~eoWrongParamTypeException() throw() {} ~eoWrongParamTypeException() throw() {}
protected:
const std::string _name;
}; };
@ -179,21 +224,14 @@ public:
eoSystemError(std::string cmd) : eoSystemError(std::string cmd) :
eoException(), eoException(),
_cmd(cmd), _has_pipe(false), _err_code(-1), _output("") _cmd(cmd), _has_pipe(false), _err_code(-1), _output("")
{ { }
_msg << msg();
}
eoSystemError(std::string cmd, int err_code, std::string output) : eoSystemError(std::string cmd, int err_code, std::string output) :
eoException(), eoException(),
_cmd(cmd), _has_pipe(true), _err_code(err_code), _output(output) _cmd(cmd), _has_pipe(true), _err_code(err_code), _output(output)
{ { }
_msg << msg();
}
~eoSystemError() throw() {} virtual std::string message() const throw()
protected:
const std::string msg() const throw()
{ {
std::ostringstream ss; std::ostringstream ss;
ss << "System call: `" << _cmd << "` error"; ss << "System call: `" << _cmd << "` error";
@ -204,6 +242,9 @@ protected:
return ss.str(); return ss.str();
} }
~eoSystemError() throw() {}
protected:
const std::string _cmd; const std::string _cmd;
const bool _has_pipe; const bool _has_pipe;
const int _err_code; const int _err_code;