Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Catch exception from script and pipe it into feedback
  • Loading branch information
elpaso committed Feb 19, 2019
1 parent e735d9f commit 03cc355
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
16 changes: 13 additions & 3 deletions src/core/processing/qgsprocessingalgrunnertask.cpp
Expand Up @@ -27,20 +27,30 @@ QgsProcessingAlgRunnerTask::QgsProcessingAlgRunnerTask( const QgsProcessingAlgor
, mParameters( parameters )
, mContext( context )
, mFeedback( feedback )
, mAlgorithm( algorithm->create() )
{
if ( !mFeedback )
{
mOwnedFeedback.reset( new QgsProcessingFeedback() );
mFeedback = mOwnedFeedback.get();
}
if ( !( mAlgorithm && mAlgorithm->prepare( mParameters, context, mFeedback ) ) )
try
{
mAlgorithm.reset( algorithm->create() );
if ( !( mAlgorithm && mAlgorithm->prepare( mParameters, context, mFeedback ) ) )
cancel();
}
catch ( QgsProcessingException &e )
{
QgsMessageLog::logMessage( e.what(), QObject::tr( "Processing" ), Qgis::Critical );
mFeedback->reportError( e.what() );
cancel();
}
}

void QgsProcessingAlgRunnerTask::cancel()
{
mFeedback->cancel();
if ( mFeedback )
mFeedback->cancel();
QgsTask::cancel();
}

Expand Down
17 changes: 14 additions & 3 deletions tests/src/python/test_qgsprocessingalgrunner.py
Expand Up @@ -28,11 +28,21 @@
QgsProcessingAlgRunnerTask,
QgsProcessingAlgorithm,
QgsProject,
QgsProcessingFeedback,
)

start_app()


class ConsoleFeedBack(QgsProcessingFeedback):

_error = ''

def reportError(self, error, fatalError=False):
self._error = error
print(error)


class CrashingProcessingAlgorithm(QgsProcessingAlgorithm):
"""
Wrong class in factory createInstance()
Expand Down Expand Up @@ -89,10 +99,11 @@ def test_bad_script_dont_crash(self): # spellok

context = QgsProcessingContext()
context.setProject(QgsProject.instance())
with self.assertRaises(Exception) as e:
QgsProcessingAlgRunnerTask(CrashingProcessingAlgorithm(), {}, context=context)
feedback = ConsoleFeedBack()

self.assertEqual(str(e.exception), 'unknown')
task = QgsProcessingAlgRunnerTask(CrashingProcessingAlgorithm(), {}, context=context, feedback=feedback)
self.assertTrue(task.isCanceled())
self.assertEqual(feedback._error, 'Error creating algorithm from createInstance()')


if __name__ == '__main__':
Expand Down

0 comments on commit 03cc355

Please sign in to comment.