Catch exceptions inside of QRunnable. ref #974.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2019-06-24 12:14:10 +03:00
parent 422ba5065f
commit a2e71d6e1c
12 changed files with 190 additions and 21 deletions

View File

@ -363,6 +363,7 @@ bool MainWindowsNoGUI::GenerateLayout(VLayoutGenerator& lGenerator)
case LayoutErrors::Timeout: case LayoutErrors::Timeout:
case LayoutErrors::PrepareLayoutError: case LayoutErrors::PrepareLayoutError:
case LayoutErrors::ProcessStoped: case LayoutErrors::ProcessStoped:
case LayoutErrors::TerminatedByException:
default: default:
break; break;
} }
@ -370,6 +371,7 @@ bool MainWindowsNoGUI::GenerateLayout(VLayoutGenerator& lGenerator)
nestingState = lGenerator.State(); nestingState = lGenerator.State();
if (nestingState == LayoutErrors::PrepareLayoutError || nestingState == LayoutErrors::ProcessStoped if (nestingState == LayoutErrors::PrepareLayoutError || nestingState == LayoutErrors::ProcessStoped
|| nestingState == LayoutErrors::TerminatedByException
|| (nestingState == LayoutErrors::NoError && not qFuzzyIsNull(lGenerator.GetEfficiencyCoefficient()) || (nestingState == LayoutErrors::NoError && not qFuzzyIsNull(lGenerator.GetEfficiencyCoefficient())
&& efficiency >= lGenerator.GetEfficiencyCoefficient())) && efficiency >= lGenerator.GetEfficiencyCoefficient()))
{ {
@ -433,6 +435,9 @@ void MainWindowsNoGUI::ShowLayoutError(const LayoutErrors &state)
case LayoutErrors::Timeout: case LayoutErrors::Timeout:
qCritical() << tr("Timeout."); qCritical() << tr("Timeout.");
break; break;
case LayoutErrors::TerminatedByException:
qCritical() << tr("Process has been stoped because of exception.");
break;
case LayoutErrors::ProcessStoped: case LayoutErrors::ProcessStoped:
default: default:
break; break;

View File

@ -535,6 +535,15 @@ DEPENDPATH += $$PWD/../../libs/fervor
win32:!win32-g++: PRE_TARGETDEPS += $$OUT_PWD/../../libs/fervor/$${DESTDIR}/fervor.lib win32:!win32-g++: PRE_TARGETDEPS += $$OUT_PWD/../../libs/fervor/$${DESTDIR}/fervor.lib
else:unix|win32-g++: PRE_TARGETDEPS += $$OUT_PWD/../../libs/fervor/$${DESTDIR}/libfervor.a else:unix|win32-g++: PRE_TARGETDEPS += $$OUT_PWD/../../libs/fervor/$${DESTDIR}/libfervor.a
# VLayout static library (depend on IFC)
unix|win32: LIBS += -L$$OUT_PWD/../../libs/vlayout/$${DESTDIR}/ -lvlayout
INCLUDEPATH += $$PWD/../../libs/vlayout
DEPENDPATH += $$PWD/../../libs/vlayout
win32:!win32-g++: PRE_TARGETDEPS += $$OUT_PWD/../../libs/vlayout/$${DESTDIR}/vlayout.lib
else:unix|win32-g++: PRE_TARGETDEPS += $$OUT_PWD/../../libs/vlayout/$${DESTDIR}/libvlayout.a
# IFC static library (depend on QMuParser, VMisc) # IFC static library (depend on QMuParser, VMisc)
unix|win32: LIBS += -L$$OUT_PWD/../../libs/ifc/$${DESTDIR}/ -lifc unix|win32: LIBS += -L$$OUT_PWD/../../libs/ifc/$${DESTDIR}/ -lifc
@ -571,15 +580,6 @@ DEPENDPATH += $$PWD/../../libs/vdxf
win32:!win32-g++: PRE_TARGETDEPS += $$OUT_PWD/../../libs/vdxf/$${DESTDIR}/vdxf.lib win32:!win32-g++: PRE_TARGETDEPS += $$OUT_PWD/../../libs/vdxf/$${DESTDIR}/vdxf.lib
else:unix|win32-g++: PRE_TARGETDEPS += $$OUT_PWD/../../libs/vdxf/$${DESTDIR}/libvdxf.a else:unix|win32-g++: PRE_TARGETDEPS += $$OUT_PWD/../../libs/vdxf/$${DESTDIR}/libvdxf.a
# VLayout static library
unix|win32: LIBS += -L$$OUT_PWD/../../libs/vlayout/$${DESTDIR}/ -lvlayout
INCLUDEPATH += $$PWD/../../libs/vlayout
DEPENDPATH += $$PWD/../../libs/vlayout
win32:!win32-g++: PRE_TARGETDEPS += $$OUT_PWD/../../libs/vlayout/$${DESTDIR}/vlayout.lib
else:unix|win32-g++: PRE_TARGETDEPS += $$OUT_PWD/../../libs/vlayout/$${DESTDIR}/libvlayout.a
# QMuParser library # QMuParser library
win32:CONFIG(release, debug|release): LIBS += -L$${OUT_PWD}/../../libs/qmuparser/$${DESTDIR} -lqmuparser2 win32:CONFIG(release, debug|release): LIBS += -L$${OUT_PWD}/../../libs/qmuparser/$${DESTDIR} -lqmuparser2
else:win32:CONFIG(debug, debug|release): LIBS += -L$${OUT_PWD}/../../libs/qmuparser/$${DESTDIR} -lqmuparser2 else:win32:CONFIG(debug, debug|release): LIBS += -L$${OUT_PWD}/../../libs/qmuparser/$${DESTDIR} -lqmuparser2

View File

@ -7,6 +7,7 @@ HEADERS += \
$$PWD/vexceptionconversionerror.h \ $$PWD/vexceptionconversionerror.h \
$$PWD/vexceptionbadid.h \ $$PWD/vexceptionbadid.h \
$$PWD/vexception.h \ $$PWD/vexception.h \
$$PWD/vexceptionterminatedposition.h \
$$PWD/vexceptionwrongid.h \ $$PWD/vexceptionwrongid.h \
$$PWD/vexceptionundo.h \ $$PWD/vexceptionundo.h \
$$PWD/vexceptioninvalidnotch.h $$PWD/vexceptioninvalidnotch.h
@ -17,6 +18,7 @@ SOURCES += \
$$PWD/vexceptionconversionerror.cpp \ $$PWD/vexceptionconversionerror.cpp \
$$PWD/vexceptionbadid.cpp \ $$PWD/vexceptionbadid.cpp \
$$PWD/vexception.cpp \ $$PWD/vexception.cpp \
$$PWD/vexceptionterminatedposition.cpp \
$$PWD/vexceptionwrongid.cpp \ $$PWD/vexceptionwrongid.cpp \
$$PWD/vexceptionundo.cpp \ $$PWD/vexceptionundo.cpp \
$$PWD/vexceptioninvalidnotch.cpp $$PWD/vexceptioninvalidnotch.cpp

View File

@ -0,0 +1,68 @@
/************************************************************************
**
** @file vexceptionterminatedposition.cpp
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date 24 6, 2019
**
** @brief
** @copyright
** This source code is part of the Valentina project, a pattern making
** program, whose allow create and modeling patterns of clothing.
** Copyright (C) 2019 Valentina project
** <https://bitbucket.org/dismine/valentina> All Rights Reserved.
**
** Valentina is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Valentina is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#include "vexceptionterminatedposition.h"
//---------------------------------------------------------------------------------------------------------------------
VExceptionTerminatedPosition::VExceptionTerminatedPosition(const QString &error) V_NOEXCEPT_EXPR (true)
:VException(error)
{}
//---------------------------------------------------------------------------------------------------------------------
VExceptionTerminatedPosition::VExceptionTerminatedPosition(const VExceptionTerminatedPosition &e)
V_NOEXCEPT_EXPR (true)
:VException(e)
{}
//---------------------------------------------------------------------------------------------------------------------
VExceptionTerminatedPosition &VExceptionTerminatedPosition::operator=(const VExceptionTerminatedPosition &e)
V_NOEXCEPT_EXPR (true)
{
if ( &e == this )
{
return *this;
}
VException::operator=(e);
return *this;
}
//---------------------------------------------------------------------------------------------------------------------
VExceptionTerminatedPosition::~VExceptionTerminatedPosition() V_NOEXCEPT_EXPR (true)
{}
//---------------------------------------------------------------------------------------------------------------------
void VExceptionTerminatedPosition::raise() const
{
throw *this;
}
//---------------------------------------------------------------------------------------------------------------------
VExceptionTerminatedPosition *VExceptionTerminatedPosition::clone() const
{
return new VExceptionTerminatedPosition(*this);
}

View File

@ -0,0 +1,47 @@
/************************************************************************
**
** @file vexceptionterminatedposition.h
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date 24 6, 2019
**
** @brief
** @copyright
** This source code is part of the Valentina project, a pattern making
** program, whose allow create and modeling patterns of clothing.
** Copyright (C) 2019 Valentina project
** <https://bitbucket.org/dismine/valentina> All Rights Reserved.
**
** Valentina is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Valentina is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#ifndef VEXCEPTIONTERMINATEDPOSITION_H
#define VEXCEPTIONTERMINATEDPOSITION_H
#include "vexception.h"
class VExceptionTerminatedPosition : public VException
{
public:
explicit VExceptionTerminatedPosition(const QString &error) V_NOEXCEPT_EXPR (true);
VExceptionTerminatedPosition(const VExceptionTerminatedPosition &e) V_NOEXCEPT_EXPR (true);
VExceptionTerminatedPosition &operator=(const VExceptionTerminatedPosition &e) V_NOEXCEPT_EXPR (true);
virtual ~VExceptionTerminatedPosition() V_NOEXCEPT_EXPR (true);
Q_NORETURN virtual void raise() const override;
// cppcheck-suppress unusedFunction
Q_REQUIRED_RESULT virtual VExceptionTerminatedPosition *clone() const override;
};
#endif // VEXCEPTIONTERMINATEDPOSITION_H

View File

@ -161,9 +161,23 @@ BestFrom VBestSquare::Type() const
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
qreal VBestSquare::Position() const bool VBestSquare::IsTerminatedByException() const
{ {
return d->depthPosition; return d->terminatedByException;
}
//---------------------------------------------------------------------------------------------------------------------
QString VBestSquare::ReasonTerminatedByException() const
{
return d->exceptionReason;
}
//---------------------------------------------------------------------------------------------------------------------
void VBestSquare::TerminatedByException(const QString &reason)
{
d->valideResult = false;
d->terminatedByException = true;
d->exceptionReason = reason;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------

View File

@ -64,7 +64,9 @@ public:
bool HasValidResult() const; bool HasValidResult() const;
bool Mirror() const; bool Mirror() const;
BestFrom Type() const; BestFrom Type() const;
qreal Position() const; bool IsTerminatedByException() const;
QString ReasonTerminatedByException() const;
void TerminatedByException(const QString &reason);
VBestSquareResData BestResultData() const; VBestSquareResData BestResultData() const;

View File

@ -54,9 +54,10 @@ public:
sheetSize(res.sheetSize), sheetSize(res.sheetSize),
valideResult(res.valideResult), valideResult(res.valideResult),
saveLength(res.saveLength), saveLength(res.saveLength),
depthPosition(res.depthPosition),
data(res.data), data(res.data),
isPortrait(res.isPortrait) isPortrait(res.isPortrait),
terminatedByException(res.terminatedByException),
exceptionReason(res.exceptionReason)
{} {}
~VBestSquareData() {} ~VBestSquareData() {}
@ -64,9 +65,10 @@ public:
QSizeF sheetSize; QSizeF sheetSize;
bool valideResult{false}; bool valideResult{false};
bool saveLength; bool saveLength;
qreal depthPosition{INT_MAX};
VBestSquareResData data{}; VBestSquareResData data{};
bool isPortrait{true}; bool isPortrait{true};
bool terminatedByException{false};
QString exceptionReason{};
private: private:
VBestSquareData &operator=(const VBestSquareData &) Q_DECL_EQ_DELETE; VBestSquareData &operator=(const VBestSquareData &) Q_DECL_EQ_DELETE;

View File

@ -41,7 +41,8 @@ enum class LayoutErrors : char
PrepareLayoutError, PrepareLayoutError,
ProcessStoped, ProcessStoped,
EmptyPaperError, EmptyPaperError,
Timeout Timeout,
TerminatedByException
}; };
enum class BestFrom : char enum class BestFrom : char

View File

@ -37,6 +37,7 @@
#include "../vmisc/vmath.h" #include "../vmisc/vmath.h"
#include "vlayoutpiece.h" #include "vlayoutpiece.h"
#include "vlayoutpaper.h" #include "vlayoutpaper.h"
#include "../ifc/exception/vexceptionterminatedposition.h"
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VLayoutGenerator::VLayoutGenerator(QObject *parent) VLayoutGenerator::VLayoutGenerator(QObject *parent)
@ -186,6 +187,8 @@ void VLayoutGenerator::Generate(const QElapsedTimer &timer, qint64 timeout, Layo
do do
{ {
const int index = bank->GetNext(); const int index = bank->GetNext();
try
{
if (paper.ArrangeDetail(bank->GetDetail(index), stopGeneration)) if (paper.ArrangeDetail(bank->GetDetail(index), stopGeneration))
{ {
bank->Arranged(index); bank->Arranged(index);
@ -194,6 +197,13 @@ void VLayoutGenerator::Generate(const QElapsedTimer &timer, qint64 timeout, Layo
{ {
bank->NotArranged(index); bank->NotArranged(index);
} }
}
catch (const VExceptionTerminatedPosition &e)
{
qCritical() << e.ErrorMessage();
state = LayoutErrors::TerminatedByException;
return;
}
QCoreApplication::processEvents(); QCoreApplication::processEvents();

View File

@ -48,6 +48,7 @@
#include "vlayoutpiece.h" #include "vlayoutpiece.h"
#include "vlayoutpaper_p.h" #include "vlayoutpaper_p.h"
#include "vposition.h" #include "vposition.h"
#include "../ifc/exception/vexceptionterminatedposition.h"
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VLayoutPaper::VLayoutPaper() VLayoutPaper::VLayoutPaper()
@ -272,6 +273,10 @@ bool VLayoutPaper::SaveResult(const VBestSquare &bestResult, const VLayoutPiece
positionChache.layoutAllowancePath = VLayoutPiece::PainterPath(layoutPoints); positionChache.layoutAllowancePath = VLayoutPiece::PainterPath(layoutPoints);
d->positionsCache.append(positionChache); d->positionsCache.append(positionChache);
} }
else if (bestResult.IsTerminatedByException())
{
throw VExceptionTerminatedPosition(bestResult.ReasonTerminatedByException());
}
return bestResult.HasValidResult(); // Do we have the best result? return bestResult.HasValidResult(); // Do we have the best result?
} }

View File

@ -78,8 +78,21 @@ void VPosition::run()
return; return;
} }
try
{
FindBestPosition(); FindBestPosition();
} }
catch (const VException &e)
{
m_bestResult.TerminatedByException(QStringLiteral("%1\n\n%2").arg(e.ErrorMessage(), e.DetailedInformation()));
return;
}
catch (std::exception& e)
{
m_bestResult.TerminatedByException(QString::fromLatin1(e.what()));
return;
}
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VBestSquare VPosition::getBestResult() const VBestSquare VPosition::getBestResult() const