Skip to content

Commit 93b7c28

Browse files
committed
Report success or failure after running algs
And auto catch python exceptions and report failure of model
1 parent a4f23fd commit 93b7c28

9 files changed

Lines changed: 51 additions & 20 deletions

File tree

‎python/core/processing/qgsprocessingalgorithm.sip‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,16 @@ class QgsProcessingAlgorithm
215215
%End
216216
217217
QVariantMap run( const QVariantMap &parameters,
218-
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const;
218+
QgsProcessingContext &context, QgsProcessingFeedback *feedback, bool *ok /Out/ = 0 ) const;
219219
%Docstring
220220
Executes the algorithm using the specified ``parameters``.
221221
222222
The ``context`` argument specifies the context in which the algorithm is being run.
223223
224224
Algorithm progress should be reported using the supplied ``feedback`` object.
225225
226+
If specified, ``ok`` will be set to true if algorithm was successfully run.
227+
226228
:return: A map of algorithm outputs. These may be output layer references, or calculated
227229
values such as statistical calculations.
228230
:rtype: QVariantMap

‎python/plugins/processing/core/GeoAlgorithm.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ def executeAlgorithm(alg, parameters, context=None, feedback=None, model=None):
335335
#self.resolveOutputs()
336336
#self.evaluateParameterValues()
337337
#self.runPreExecutionScript(feedback)
338-
result = alg.run(parameters, context, feedback)
338+
result, ok = alg.run(parameters, context, feedback)
339339
#self.processAlgorithm(parameters, context, feedback)
340340
feedback.setProgress(100)
341341
return result

‎python/plugins/processing/gui/AlgorithmExecutor.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ def execute(alg, parameters, context=None, feedback=None):
5959
context = dataobjects.createContext()
6060

6161
try:
62-
results = alg.run(parameters, context, feedback)
63-
return True, results
62+
results, ok = alg.run(parameters, context, feedback)
63+
return ok, results
6464
except GeoAlgorithmExecutionException as e:
6565
QgsMessageLog.logMessage(str(sys.exc_info()[0]), 'Processing', QgsMessageLog.CRITICAL)
6666
if feedback is not None:

‎python/plugins/processing/tests/AlgorithmsTestBase.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,15 @@ def check_algorithm(self, name, defs):
128128

129129
if expectFailure:
130130
try:
131-
results = alg.run(parameters, context, feedback)
131+
results, ok = alg.run(parameters, context, feedback)
132132
self.check_results(results, context, defs['params'], defs['results'])
133+
if ok:
134+
raise _UnexpectedSuccess
133135
except Exception:
134136
pass
135-
else:
136-
raise _UnexpectedSuccess
137137
else:
138-
results = alg.run(parameters, context, feedback)
138+
results, ok = alg.run(parameters, context, feedback)
139+
self.assertTrue(ok)
139140
self.check_results(results, context, defs['params'], defs['results'])
140141

141142
def load_params(self, params):

‎src/core/processing/qgsprocessingalgorithm.cpp‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#include "qgsrectangle.h"
2424
#include "qgsprocessingcontext.h"
2525
#include "qgsprocessingutils.h"
26+
#include "qgsprocessingexception.h"
27+
#include "qgsmessagelog.h"
28+
#include "qgsprocessingfeedback.h"
2629

2730
QgsProcessingAlgorithm::~QgsProcessingAlgorithm()
2831
{
@@ -311,9 +314,24 @@ bool QgsProcessingAlgorithm::hasHtmlOutputs() const
311314
return false;
312315
}
313316

314-
QVariantMap QgsProcessingAlgorithm::run( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const
317+
QVariantMap QgsProcessingAlgorithm::run( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback, bool *ok ) const
315318
{
316-
return processAlgorithm( parameters, context, feedback );
319+
if ( ok )
320+
*ok = false;
321+
322+
QVariantMap results;
323+
try
324+
{
325+
results = processAlgorithm( parameters, context, feedback );
326+
if ( ok )
327+
*ok = true;
328+
}
329+
catch ( QgsProcessingException &e )
330+
{
331+
QgsMessageLog::logMessage( e.what(), QObject::tr( "Processing" ), QgsMessageLog::CRITICAL );
332+
feedback->reportError( e.what() );
333+
}
334+
return results;
317335
}
318336

319337
QString QgsProcessingAlgorithm::parameterAsString( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const

‎src/core/processing/qgsprocessingalgorithm.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,13 @@ class CORE_EXPORT QgsProcessingAlgorithm
223223
*
224224
* Algorithm progress should be reported using the supplied \a feedback object.
225225
*
226+
* If specified, \a ok will be set to true if algorithm was successfully run.
227+
*
226228
* \returns A map of algorithm outputs. These may be output layer references, or calculated
227229
* values such as statistical calculations.
228230
*/
229231
QVariantMap run( const QVariantMap &parameters,
230-
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const;
232+
QgsProcessingContext &context, QgsProcessingFeedback *feedback, bool *ok SIP_OUT = nullptr ) const;
231233

232234
/**
233235
* If an algorithm subclass implements a custom parameters widget, a copy of this widget

‎src/core/processing/qgsprocessingalgrunnertask.cpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ void QgsProcessingAlgRunnerTask::cancel()
3939
bool QgsProcessingAlgRunnerTask::run()
4040
{
4141
connect( mFeedback.get(), &QgsFeedback::progressChanged, this, &QgsProcessingAlgRunnerTask::setProgress );
42-
mResults = mAlgorithm->run( mParameters, mContext, mFeedback.get() );
43-
return !mFeedback->isCanceled();
42+
bool ok = false;
43+
mResults = mAlgorithm->run( mParameters, mContext, mFeedback.get(), &ok );
44+
return ok && !mFeedback->isCanceled();
4445
}
4546

4647
void QgsProcessingAlgRunnerTask::finished( bool result )

‎src/core/processing/qgsprocessingexception.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,15 @@
2525
* \class QgsProcessingException
2626
* \ingroup core
2727
* Custom exception class for processing related exceptions.
28+
* \since QGIS 3.0
2829
*/
2930
class CORE_EXPORT QgsProcessingException : public QgsException
3031
{
3132
public:
33+
34+
/**
35+
* Constructor for QgsProcessingException.
36+
*/
3237
QgsProcessingException( const QString &what ) : QgsException( what ) {}
3338

3439
};

‎src/core/processing/qgsprocessingmodelalgorithm.cpp‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "qgsprocessingregistry.h"
2020
#include "qgsprocessingfeedback.h"
2121
#include "qgsxmlutils.h"
22+
#include "qgsprocessingexception.h"
2223
#include <QFile>
2324
#include <QTextStream>
2425

@@ -437,7 +438,14 @@ QVariantMap QgsProcessingModelAlgorithm::processAlgorithm( const QVariantMap &pa
437438
QTime childTime;
438439
childTime.start();
439440

440-
QVariantMap results = child.algorithm()->run( childParams, context, feedback );
441+
bool ok = false;
442+
QVariantMap results = child.algorithm()->run( childParams, context, feedback, &ok );
443+
if ( !ok )
444+
{
445+
QString error = QObject::tr( "Error encountered while running %1" ).arg( child.description() );
446+
feedback->reportError( error );
447+
throw QgsProcessingException( error );
448+
}
441449
childResults.insert( childId, results );
442450

443451
// look through child alg's outputs to determine whether any of these should be copied
@@ -451,12 +459,6 @@ QVariantMap QgsProcessingModelAlgorithm::processAlgorithm( const QVariantMap &pa
451459

452460
executed.insert( childId );
453461
feedback->pushDebugInfo( QObject::tr( "OK. Execution took %1 s (%2 outputs)." ).arg( childTime.elapsed() / 1000.0 ).arg( results.count() ) );
454-
#if 0
455-
except GeoAlgorithmExecutionException as e:
456-
feedback.pushDebugInfo( self.tr( 'Failed', 'ModelerAlgorithm' ) )
457-
raise GeoAlgorithmExecutionException(
458-
self.tr( 'Error executing algorithm {0}\n{1}', 'ModelerAlgorithm' ).format( alg.description, e.msg ) )
459-
#endif
460462
}
461463
}
462464
feedback->pushDebugInfo( QObject::tr( "Model processed ok. Executed %1 algorithms total in %2 s." ).arg( executed.count() ).arg( totalTime.elapsed() / 1000.0 ) );

0 commit comments

Comments
 (0)