diff --git a/src/exception/vexception.cpp b/src/exception/vexception.cpp index 35216898b..a9121884e 100644 --- a/src/exception/vexception.cpp +++ b/src/exception/vexception.cpp @@ -31,11 +31,16 @@ #include #include -VException::VException(const QString &what):QException(), what(what) +VException::VException(const QString &what):QException(), what(what), moreInfo(QString()) { Q_ASSERT_X(what.isEmpty() == false, Q_FUNC_INFO, "Error message is empty"); } +VException::VException(const VException &e):what(e.What()), moreInfo(e.MoreInformation()) +{ + +} + QString VException::ErrorMessage() const { QString error = QString("Exception: %1").arg(what); @@ -61,3 +66,24 @@ void VException::CriticalMessageBox(const QString &situation, QWidget * parent) layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount()); msgBox.exec(); } + +void VException::AddMoreInformation(const QString &info) +{ + if (info.isEmpty()) + { + return; + } + moreInfo = QString("%1\n%2").arg(moreInfo, info); +} + +QString VException::MoreInfo(const QString &detInfo) const +{ + if (moreInfo.isEmpty() == false) + { + return QString("%1\n%2").arg(moreInfo, detInfo); + } + else + { + return detInfo; + } +} diff --git a/src/exception/vexception.h b/src/exception/vexception.h index ea0145ce7..4a42522d9 100644 --- a/src/exception/vexception.h +++ b/src/exception/vexception.h @@ -49,7 +49,7 @@ public: * @brief VException copy constructor * @param e exception */ - VException(const VException &e):what(e.What()){} + VException(const VException &e); virtual ~VException() noexcept (true){} /** * @brief raise method raise for exception @@ -69,7 +69,7 @@ public: * @brief DetailedInformation return detailed information about error * @return detailed information */ - virtual QString DetailedInformation() const { return QString(); } + virtual QString DetailedInformation() const; /** * @brief What return string with error * @return string with error @@ -80,11 +80,26 @@ public: * @param situation main text message box. */ virtual void CriticalMessageBox(const QString &situation, QWidget *parent = nullptr) const; + /** + * @brief AddMoreInformation add more information for error + * @param info information + */ + void AddMoreInformation(const QString &info); + /** + * @brief MoreInformation return more information for error + * @return information + */ + QString MoreInformation() const; protected: /** * @brief what string with error */ QString what; + /** + * @brief moreInfo more information about error + */ + QString moreInfo; + QString MoreInfo(const QString &detInfo) const; }; inline void VException::raise() const @@ -97,9 +112,19 @@ inline VException *VException::clone() const return new VException(*this); } +inline QString VException::DetailedInformation() const +{ + return moreInfo; +} + inline QString VException::What() const { return what; } +inline QString VException::MoreInformation() const +{ + return moreInfo; +} + #endif // VEXCEPTION_H diff --git a/src/exception/vexceptionemptyparameter.cpp b/src/exception/vexceptionemptyparameter.cpp index 1d12deac5..ed767abc9 100644 --- a/src/exception/vexceptionemptyparameter.cpp +++ b/src/exception/vexceptionemptyparameter.cpp @@ -56,6 +56,5 @@ QString VExceptionEmptyParameter::ErrorMessage() const QString VExceptionEmptyParameter::DetailedInformation() const { - QString detail = QString("tag: %1 in line %2\nFull tag:\n%3").arg(tagName).arg(lineNumber).arg(tagText); - return detail; + return MoreInfo(QString("tag: %1 in line %2\nFull tag:\n%3").arg(tagName).arg(lineNumber).arg(tagText)); } diff --git a/src/exception/vexceptionobjecterror.cpp b/src/exception/vexceptionobjecterror.cpp index ab69f8a3b..4273a745b 100644 --- a/src/exception/vexceptionobjecterror.cpp +++ b/src/exception/vexceptionobjecterror.cpp @@ -30,7 +30,7 @@ #include VExceptionObjectError::VExceptionObjectError(const QString &what, const QDomElement &domElement) - :VException(what), tagText(QString()), tagName(QString()), lineNumber(-1), moreInfo(QString()) + :VException(what), tagText(QString()), tagName(QString()), lineNumber(-1) { Q_ASSERT_X(domElement.isNull() == false, Q_FUNC_INFO, "domElement is null"); QTextStream stream(&tagText); @@ -40,8 +40,7 @@ VExceptionObjectError::VExceptionObjectError(const QString &what, const QDomElem } VExceptionObjectError::VExceptionObjectError(const VExceptionObjectError &e) - :VException(e), tagText(e.TagText()), tagName(e.TagName()), lineNumber(e.LineNumber()), - moreInfo(e.MoreInformation()) + :VException(e), tagText(e.TagText()), tagName(e.TagName()), lineNumber(e.LineNumber()) { } @@ -54,24 +53,5 @@ QString VExceptionObjectError::ErrorMessage() const QString VExceptionObjectError::DetailedInformation() const { - QString detail; - if (moreInfo.isEmpty() == false) - { - QString i = QString("tag: %1 in line %2\n%3").arg(tagName).arg(lineNumber).arg(tagText); - detail = QString("%1\n%2").arg(moreInfo, i); - } - else - { - detail = QString("tag: %1 in line %2\n%3").arg(tagName).arg(lineNumber).arg(tagText); - } - return detail; -} - -void VExceptionObjectError::AddMoreInformation(const QString &info) -{ - if (info.isEmpty()) - { - qWarning()<<"Error additional information is empty."<moreInfo.append(info); + return MoreInfo(QString("tag: %1 in line %2\n%3").arg(tagName).arg(lineNumber).arg(tagText)); } diff --git a/src/exception/vexceptionobjecterror.h b/src/exception/vexceptionobjecterror.h index ab2baa4f5..864a78e13 100644 --- a/src/exception/vexceptionobjecterror.h +++ b/src/exception/vexceptionobjecterror.h @@ -76,16 +76,6 @@ public: * @return line number */ qint32 LineNumber() const; - /** - * @brief AddMoreInformation add more information for error - * @param info information - */ - void AddMoreInformation(const QString &info); - /** - * @brief MoreInformation return more information for error - * @return information - */ - QString MoreInformation() const; protected: /** * @brief tagText tag text @@ -99,10 +89,6 @@ protected: * @brief lineNumber line number */ qint32 lineNumber; - /** - * @brief moreInfo more information about error - */ - QString moreInfo; }; inline QString VExceptionObjectError::TagText() const @@ -120,9 +106,4 @@ inline qint32 VExceptionObjectError::LineNumber() const return lineNumber; } -inline QString VExceptionObjectError::MoreInformation() const -{ - return moreInfo; -} - #endif // VEXCEPTIONOBJECTERROR_H diff --git a/src/exception/vexceptionwrongid.cpp b/src/exception/vexceptionwrongid.cpp index ac78b1d99..76471d0c7 100644 --- a/src/exception/vexceptionwrongid.cpp +++ b/src/exception/vexceptionwrongid.cpp @@ -53,6 +53,5 @@ QString VExceptionWrongId::ErrorMessage() const QString VExceptionWrongId::DetailedInformation() const { - QString detail = QString("tag: %1 in line %2\nFull tag:\n%3").arg(tagName).arg(lineNumber).arg(tagText); - return detail; + return MoreInfo(QString("tag: %1 in line %2\nFull tag:\n%3").arg(tagName).arg(lineNumber).arg(tagText)); } diff --git a/src/xml/vdomdocument.cpp b/src/xml/vdomdocument.cpp index bc248a6bc..ea9f94dee 100644 --- a/src/xml/vdomdocument.cpp +++ b/src/xml/vdomdocument.cpp @@ -150,13 +150,28 @@ quint32 VDomDocument::GetParametrUInt(const QDomElement &domElement, const QStri { Q_ASSERT_X(name.isEmpty() == false, Q_FUNC_INFO, "name of parametr is empty"); Q_ASSERT_X(domElement.isNull() == false, Q_FUNC_INFO, "domElement is null"); + bool ok = false; - const QString parametr = GetParametrString(domElement, name, defValue); - const quint32 id = parametr.toUInt(&ok); - if (ok == false) + QString parametr; + quint32 id = 0; + + QString message = tr("Can't convert toUInt parameter"); + try { - throw VExceptionConversionError(tr("Can't convert toUInt parameter"), name); + parametr = GetParametrString(domElement, name, defValue); + id = parametr.toUInt(&ok); + if (ok == false) + { + throw VExceptionConversionError(message, name); + } } + catch (const VExceptionEmptyParameter &e) + { + VExceptionConversionError excep(message, name); + excep.AddMoreInformation(e.ErrorMessage()); + throw excep; + } + return id; } @@ -184,12 +199,25 @@ qreal VDomDocument::GetParametrDouble(const QDomElement &domElement, const QStri { Q_ASSERT_X(name.isEmpty() == false, Q_FUNC_INFO, "name of parametr is empty"); Q_ASSERT_X(domElement.isNull() == false, Q_FUNC_INFO, "domElement is null"); + bool ok = false; - QString parametr = GetParametrString(domElement, name, defValue); - const qreal param = parametr.replace(",", ".").toDouble(&ok); - if (ok == false) + qreal param = 0; + + QString message = tr("Can't convert toDouble parameter"); + try { - throw VExceptionConversionError(tr("Can't convert toDouble parameter"), name); + QString parametr = GetParametrString(domElement, name, defValue); + param = parametr.replace(",", ".").toDouble(&ok); + if (ok == false) + { + throw VExceptionConversionError(message, name); + } + } + catch (const VExceptionEmptyParameter &e) + { + VExceptionConversionError excep(message, name); + excep.AddMoreInformation(e.ErrorMessage()); + throw excep; } return param; } diff --git a/src/xml/vdomdocument.h b/src/xml/vdomdocument.h index ede7c9963..3de21dc03 100644 --- a/src/xml/vdomdocument.h +++ b/src/xml/vdomdocument.h @@ -88,8 +88,11 @@ public: const QString &defValue) const; /** * @brief GetParametrString return string value of attribute. + * + * if attribute empty return default value. If default value empty too throw exception. * @param domElement tag in xml tree. * @param name attribute name. + * @throw VExceptionEmptyParameter when attribute is empty * @return attribute value. */ QString GetParametrString(const QDomElement& domElement, const QString &name, diff --git a/src/xml/vpattern.cpp b/src/xml/vpattern.cpp index 821feb487..b3f763291 100644 --- a/src/xml/vpattern.cpp +++ b/src/xml/vpattern.cpp @@ -34,6 +34,7 @@ #include "../tools/nodeDetails/nodedetails.h" #include "../exception/vexceptionobjecterror.h" #include "../exception/vexceptionwrongid.h" +#include "../exception/vexceptionconversionerror.h" #include @@ -1244,10 +1245,23 @@ void VPattern::ParseIncrementsElement(const QDomNode &node) quint32 VPattern::GetParametrId(const QDomElement &domElement) const { Q_ASSERT_X(domElement.isNull() == false, Q_FUNC_INFO, "domElement is null"); - const quint32 id = GetParametrUInt(domElement, VAbstractTool::AttrId, "0"); - if (id <= 0) + + quint32 id = 0; + + QString message = tr("Got wrong parameter id. Need only id > 0."); + try { - throw VExceptionWrongId(tr("Got wrong parameter id. Need only id > 0."), domElement); + id = GetParametrUInt(domElement, VAbstractTool::AttrId, "0"); + if (id <= 0) + { + throw VExceptionWrongId(message, domElement); + } + } + catch (const VExceptionConversionError &e) + { + VExceptionWrongId excep(message, domElement); + excep.AddMoreInformation(e.ErrorMessage()); + throw excep; } return id; }