Each exception can show error message from previous.

--HG--
branch : feature
This commit is contained in:
dismine 2014-03-14 17:59:28 +02:00
parent 7d01bca72d
commit 4d210ddd48
9 changed files with 115 additions and 60 deletions

View File

@ -31,11 +31,16 @@
#include <QSpacerItem> #include <QSpacerItem>
#include <QGridLayout> #include <QGridLayout>
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"); 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 VException::ErrorMessage() const
{ {
QString error = QString("Exception: %1").arg(what); 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()); layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());
msgBox.exec(); 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;
}
}

View File

@ -49,7 +49,7 @@ public:
* @brief VException copy constructor * @brief VException copy constructor
* @param e exception * @param e exception
*/ */
VException(const VException &e):what(e.What()){} VException(const VException &e);
virtual ~VException() noexcept (true){} virtual ~VException() noexcept (true){}
/** /**
* @brief raise method raise for exception * @brief raise method raise for exception
@ -69,7 +69,7 @@ public:
* @brief DetailedInformation return detailed information about error * @brief DetailedInformation return detailed information about error
* @return detailed information * @return detailed information
*/ */
virtual QString DetailedInformation() const { return QString(); } virtual QString DetailedInformation() const;
/** /**
* @brief What return string with error * @brief What return string with error
* @return string with error * @return string with error
@ -80,11 +80,26 @@ public:
* @param situation main text message box. * @param situation main text message box.
*/ */
virtual void CriticalMessageBox(const QString &situation, QWidget *parent = nullptr) const; 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: protected:
/** /**
* @brief what string with error * @brief what string with error
*/ */
QString what; QString what;
/**
* @brief moreInfo more information about error
*/
QString moreInfo;
QString MoreInfo(const QString &detInfo) const;
}; };
inline void VException::raise() const inline void VException::raise() const
@ -97,9 +112,19 @@ inline VException *VException::clone() const
return new VException(*this); return new VException(*this);
} }
inline QString VException::DetailedInformation() const
{
return moreInfo;
}
inline QString VException::What() const inline QString VException::What() const
{ {
return what; return what;
} }
inline QString VException::MoreInformation() const
{
return moreInfo;
}
#endif // VEXCEPTION_H #endif // VEXCEPTION_H

View File

@ -56,6 +56,5 @@ QString VExceptionEmptyParameter::ErrorMessage() const
QString VExceptionEmptyParameter::DetailedInformation() const QString VExceptionEmptyParameter::DetailedInformation() const
{ {
QString detail = QString("tag: %1 in line %2\nFull tag:\n%3").arg(tagName).arg(lineNumber).arg(tagText); return MoreInfo(QString("tag: %1 in line %2\nFull tag:\n%3").arg(tagName).arg(lineNumber).arg(tagText));
return detail;
} }

View File

@ -30,7 +30,7 @@
#include <QDebug> #include <QDebug>
VExceptionObjectError::VExceptionObjectError(const QString &what, const QDomElement &domElement) 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"); Q_ASSERT_X(domElement.isNull() == false, Q_FUNC_INFO, "domElement is null");
QTextStream stream(&tagText); QTextStream stream(&tagText);
@ -40,8 +40,7 @@ VExceptionObjectError::VExceptionObjectError(const QString &what, const QDomElem
} }
VExceptionObjectError::VExceptionObjectError(const VExceptionObjectError &e) VExceptionObjectError::VExceptionObjectError(const VExceptionObjectError &e)
:VException(e), tagText(e.TagText()), tagName(e.TagName()), lineNumber(e.LineNumber()), :VException(e), tagText(e.TagText()), tagName(e.TagName()), lineNumber(e.LineNumber())
moreInfo(e.MoreInformation())
{ {
} }
@ -54,24 +53,5 @@ QString VExceptionObjectError::ErrorMessage() const
QString VExceptionObjectError::DetailedInformation() const QString VExceptionObjectError::DetailedInformation() const
{ {
QString detail; return MoreInfo(QString("tag: %1 in line %2\n%3").arg(tagName).arg(lineNumber).arg(tagText));
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."<<Q_FUNC_INFO;
}
this->moreInfo.append(info);
} }

View File

@ -76,16 +76,6 @@ public:
* @return line number * @return line number
*/ */
qint32 LineNumber() const; 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: protected:
/** /**
* @brief tagText tag text * @brief tagText tag text
@ -99,10 +89,6 @@ protected:
* @brief lineNumber line number * @brief lineNumber line number
*/ */
qint32 lineNumber; qint32 lineNumber;
/**
* @brief moreInfo more information about error
*/
QString moreInfo;
}; };
inline QString VExceptionObjectError::TagText() const inline QString VExceptionObjectError::TagText() const
@ -120,9 +106,4 @@ inline qint32 VExceptionObjectError::LineNumber() const
return lineNumber; return lineNumber;
} }
inline QString VExceptionObjectError::MoreInformation() const
{
return moreInfo;
}
#endif // VEXCEPTIONOBJECTERROR_H #endif // VEXCEPTIONOBJECTERROR_H

View File

@ -53,6 +53,5 @@ QString VExceptionWrongId::ErrorMessage() const
QString VExceptionWrongId::DetailedInformation() const QString VExceptionWrongId::DetailedInformation() const
{ {
QString detail = QString("tag: %1 in line %2\nFull tag:\n%3").arg(tagName).arg(lineNumber).arg(tagText); return MoreInfo(QString("tag: %1 in line %2\nFull tag:\n%3").arg(tagName).arg(lineNumber).arg(tagText));
return detail;
} }

View File

@ -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(name.isEmpty() == false, Q_FUNC_INFO, "name of parametr is empty");
Q_ASSERT_X(domElement.isNull() == false, Q_FUNC_INFO, "domElement is null"); Q_ASSERT_X(domElement.isNull() == false, Q_FUNC_INFO, "domElement is null");
bool ok = false; bool ok = false;
const QString parametr = GetParametrString(domElement, name, defValue); QString parametr;
const quint32 id = parametr.toUInt(&ok); quint32 id = 0;
if (ok == false)
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; 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(name.isEmpty() == false, Q_FUNC_INFO, "name of parametr is empty");
Q_ASSERT_X(domElement.isNull() == false, Q_FUNC_INFO, "domElement is null"); Q_ASSERT_X(domElement.isNull() == false, Q_FUNC_INFO, "domElement is null");
bool ok = false; bool ok = false;
QString parametr = GetParametrString(domElement, name, defValue); qreal param = 0;
const qreal param = parametr.replace(",", ".").toDouble(&ok);
if (ok == false) 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; return param;
} }

View File

@ -88,8 +88,11 @@ public:
const QString &defValue) const; const QString &defValue) const;
/** /**
* @brief GetParametrString return string value of attribute. * @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 domElement tag in xml tree.
* @param name attribute name. * @param name attribute name.
* @throw VExceptionEmptyParameter when attribute is empty
* @return attribute value. * @return attribute value.
*/ */
QString GetParametrString(const QDomElement& domElement, const QString &name, QString GetParametrString(const QDomElement& domElement, const QString &name,

View File

@ -34,6 +34,7 @@
#include "../tools/nodeDetails/nodedetails.h" #include "../tools/nodeDetails/nodedetails.h"
#include "../exception/vexceptionobjecterror.h" #include "../exception/vexceptionobjecterror.h"
#include "../exception/vexceptionwrongid.h" #include "../exception/vexceptionwrongid.h"
#include "../exception/vexceptionconversionerror.h"
#include <QMessageBox> #include <QMessageBox>
@ -1244,10 +1245,23 @@ void VPattern::ParseIncrementsElement(const QDomNode &node)
quint32 VPattern::GetParametrId(const QDomElement &domElement) const quint32 VPattern::GetParametrId(const QDomElement &domElement) const
{ {
Q_ASSERT_X(domElement.isNull() == false, Q_FUNC_INFO, "domElement is null"); 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; return id;
} }