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

View file

@ -535,6 +535,15 @@ DEPENDPATH += $$PWD/../../libs/fervor
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
# 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)
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
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
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

View file

@ -7,6 +7,7 @@ HEADERS += \
$$PWD/vexceptionconversionerror.h \
$$PWD/vexceptionbadid.h \
$$PWD/vexception.h \
$$PWD/vexceptionterminatedposition.h \
$$PWD/vexceptionwrongid.h \
$$PWD/vexceptionundo.h \
$$PWD/vexceptioninvalidnotch.h
@ -17,6 +18,7 @@ SOURCES += \
$$PWD/vexceptionconversionerror.cpp \
$$PWD/vexceptionbadid.cpp \
$$PWD/vexception.cpp \
$$PWD/vexceptionterminatedposition.cpp \
$$PWD/vexceptionwrongid.cpp \
$$PWD/vexceptionundo.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 Mirror() const;
BestFrom Type() const;
qreal Position() const;
bool IsTerminatedByException() const;
QString ReasonTerminatedByException() const;
void TerminatedByException(const QString &reason);
VBestSquareResData BestResultData() const;

View file

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

View file

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

View file

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

View file

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

View file

@ -78,7 +78,20 @@ void VPosition::run()
return;
}
FindBestPosition();
try
{
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;
}
}
//---------------------------------------------------------------------------------------------------------------------