Use exception-based error handling in ConvertWorker

This commit is contained in:
Anton Thomasson 2021-03-05 20:28:10 +01:00
parent b569aede6b
commit 22c823563b
2 changed files with 50 additions and 50 deletions

View file

@ -49,12 +49,13 @@ void ConvertWorker::convertPdf(QNetworkRequest request, QString filename, QTempo
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble, quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble,
quint32 PageRangeLow, quint32 PageRangeHigh) quint32 PageRangeLow, quint32 PageRangeHigh)
{ {
try {
quint32 pages = ConvertChecker::instance()->pdfPages(filename); quint32 pages = ConvertChecker::instance()->pdfPages(filename);
if (!pages) if (!pages)
{ {
qDebug() << "pdfinfo returned 0 pages"; qDebug() << "pdfinfo returned 0 pages";
tempfile->deleteLater(); throw ConvertFailedException(tr("Failed to get info about PDF file"));
emit failed(tr("Failed to get info about PDF file"));
} }
if(PageRangeLow==0) if(PageRangeLow==0)
@ -94,17 +95,13 @@ void ConvertWorker::convertPdf(QNetworkRequest request, QString filename, QTempo
} }
else else
{ {
tempfile->deleteLater(); throw ConvertFailedException(tr("Unsupported target format"));
emit failed(tr("Unsupported target format"));
return;
} }
if(urf && (HwResX != HwResY)) if(urf && (HwResX != HwResY))
{ // URF only supports symmetric resolutions { // URF only supports symmetric resolutions
qDebug() << "Unsupported URF resolution" << PaperSize; qDebug() << "Unsupported URF resolution" << PaperSize;
tempfile->deleteLater(); throw ConvertFailedException(tr("Unsupported resolution (dpi)"));
emit failed(tr("Unsupported resolution (dpi)"));
return;
} }
QString ShortPaperSize; QString ShortPaperSize;
@ -127,9 +124,7 @@ void ConvertWorker::convertPdf(QNetworkRequest request, QString filename, QTempo
else else
{ {
qDebug() << "Unsupported PDF paper size" << PaperSize; qDebug() << "Unsupported PDF paper size" << PaperSize;
tempfile->deleteLater(); throw ConvertFailedException(tr("Unsupported PDF paper size"));
emit failed(tr("Unsupported PDF paper size"));
return;
} }
if(ps) if(ps)
@ -159,9 +154,7 @@ void ConvertWorker::convertPdf(QNetworkRequest request, QString filename, QTempo
if(!pdftops->waitForStarted()) if(!pdftops->waitForStarted())
{ {
qDebug() << "pdftops died"; qDebug() << "pdftops died";
tempfile->deleteLater(); throw ConvertFailedException();
emit failed(tr("Conversion error"));
return;
} }
qDebug() << "Started"; qDebug() << "Started";
@ -169,9 +162,7 @@ void ConvertWorker::convertPdf(QNetworkRequest request, QString filename, QTempo
if(!pdftops->waitForFinished(-1)) if(!pdftops->waitForFinished(-1))
{ {
qDebug() << "pdftops failed"; qDebug() << "pdftops failed";
tempfile->deleteLater(); throw ConvertFailedException();
emit failed(tr("Conversion error"));
return;
} }
} }
else if(pdf) else if(pdf)
@ -198,9 +189,7 @@ void ConvertWorker::convertPdf(QNetworkRequest request, QString filename, QTempo
if(!pdftocairo->waitForStarted()) if(!pdftocairo->waitForStarted())
{ {
qDebug() << "pdftocairo died"; qDebug() << "pdftocairo died";
tempfile->deleteLater(); throw ConvertFailedException();
emit failed(tr("Conversion error"));
return;
} }
qDebug() << "Started"; qDebug() << "Started";
@ -208,10 +197,9 @@ void ConvertWorker::convertPdf(QNetworkRequest request, QString filename, QTempo
if(!pdftocairo->waitForFinished(-1)) if(!pdftocairo->waitForFinished(-1))
{ {
qDebug() << "pdftocairo failed"; qDebug() << "pdftocairo failed";
tempfile->deleteLater(); throw ConvertFailedException();
emit failed(tr("Conversion error"));
return;
} }
} }
else else
{ {
@ -266,23 +254,17 @@ void ConvertWorker::convertPdf(QNetworkRequest request, QString filename, QTempo
if(!pdftocairo->waitForStarted()) if(!pdftocairo->waitForStarted())
{ {
qDebug() << "pdftocairo died"; qDebug() << "pdftocairo died";
tempfile->deleteLater(); throw ConvertFailedException();
emit failed(tr("Conversion error"));
return;
} }
if(!pdftoppm->waitForStarted()) if(!pdftoppm->waitForStarted())
{ {
qDebug() << "pdftoppm died"; qDebug() << "pdftoppm died";
tempfile->deleteLater(); throw ConvertFailedException();
emit failed(tr("Conversion error"));
return;
} }
if(!ppm2pwg->waitForStarted()) if(!ppm2pwg->waitForStarted())
{ {
qDebug() << "ppm2pwg died"; qDebug() << "ppm2pwg died";
tempfile->deleteLater(); throw ConvertFailedException();
emit failed(tr("Conversion error"));
return;
} }
qDebug() << "All started"; qDebug() << "All started";
@ -311,9 +293,7 @@ void ConvertWorker::convertPdf(QNetworkRequest request, QString filename, QTempo
if(!ppm2pwgSuccess) if(!ppm2pwgSuccess)
{ {
qDebug() << "ppm2pwg failed"; qDebug() << "ppm2pwg failed";
tempfile->deleteLater(); throw ConvertFailedException();
emit failed(tr("Conversion error"));
return;
} }
} }
@ -322,12 +302,21 @@ void ConvertWorker::convertPdf(QNetworkRequest request, QString filename, QTempo
emit done(request, tempfile); emit done(request, tempfile);
qDebug() << "posted"; qDebug() << "posted";
}
catch(const ConvertFailedException& e)
{
tempfile->deleteLater();
emit failed(e.what() == QString("") ? tr("Conversion error") : e.what());
}
} }
void ConvertWorker::convertImage(QNetworkRequest request, QString filename, QTemporaryFile* tempfile, void ConvertWorker::convertImage(QNetworkRequest request, QString filename, QTemporaryFile* tempfile,
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY) quint32 HwResX, quint32 HwResY)
{ {
try {
bool urf = false; bool urf = false;
QString imageFormat = ""; QString imageFormat = "";
QStringList supportedImageFormats = {Mimer::JPEG, Mimer::PNG}; QStringList supportedImageFormats = {Mimer::JPEG, Mimer::PNG};
@ -346,25 +335,19 @@ void ConvertWorker::convertImage(QNetworkRequest request, QString filename, QTem
} }
else else
{ {
tempfile->deleteLater(); throw ConvertFailedException(tr("Unsupported target format"));
emit failed(tr("Unsupported target format"));
return;
} }
if(urf && (HwResX != HwResY)) if(urf && (HwResX != HwResY))
{ // URF only supports symmetric resolutions { // URF only supports symmetric resolutions
qDebug() << "Unsupported URF resolution" << PaperSize; qDebug() << "Unsupported URF resolution" << PaperSize;
tempfile->deleteLater(); throw ConvertFailedException(tr("Unsupported resolution (dpi)"));
emit failed(tr("Unsupported resolution (dpi)"));
return;
} }
if(!PaperSizes.contains(PaperSize)) if(!PaperSizes.contains(PaperSize))
{ {
qDebug() << "Unsupported paper size" << PaperSize; qDebug() << "Unsupported paper size" << PaperSize;
tempfile->deleteLater(); throw ConvertFailedException(tr("Unsupported paper size"));
emit failed(tr("Unsupported paper size"));
return;
} }
QPair<float,float> wh = PaperSizes[PaperSize]; QPair<float,float> wh = PaperSizes[PaperSize];
quint32 Width = qRound(wh.first/25.4*HwResX); quint32 Width = qRound(wh.first/25.4*HwResX);
@ -376,9 +359,7 @@ void ConvertWorker::convertImage(QNetworkRequest request, QString filename, QTem
if(!inImage.load(filename)) if(!inImage.load(filename))
{ {
qDebug() << "failed to load"; qDebug() << "failed to load";
tempfile->deleteLater(); throw ConvertFailedException(tr("Failed to load image"));
emit failed(tr("Failed to load image"));
return;
} }
if(inImage.width() > inImage.height()) if(inImage.width() > inImage.height())
@ -430,9 +411,7 @@ void ConvertWorker::convertImage(QNetworkRequest request, QString filename, QTem
if(!ppm2pwg->waitForStarted()) if(!ppm2pwg->waitForStarted())
{ {
qDebug() << "ppm2pwg died"; qDebug() << "ppm2pwg died";
tempfile->deleteLater(); throw ConvertFailedException();
emit failed(tr("Conversion error"));
return;
} }
qDebug() << "All started"; qDebug() << "All started";
@ -443,4 +422,11 @@ void ConvertWorker::convertImage(QNetworkRequest request, QString filename, QTem
emit done(request, tempfile); emit done(request, tempfile);
qDebug() << "posted"; qDebug() << "posted";
}
catch(const ConvertFailedException& e)
{
tempfile->deleteLater();
emit failed(e.what() == QString("") ? tr("Conversion error") : e.what());
}
} }

View file

@ -3,6 +3,20 @@
#include <QObject> #include <QObject>
#include <QtNetwork> #include <QtNetwork>
class ConvertFailedException: public std::exception
{
private:
QString _msg;
public:
ConvertFailedException(QString msg = "") : _msg(msg = msg)
{
}
virtual const char* what() const throw()
{
return _msg.toStdString().c_str();
}
};
class ConvertWorker : public QObject class ConvertWorker : public QObject
{ {
Q_OBJECT Q_OBJECT