Skip to content
Permalink
Browse files

Catch exception from script and pipe it into feedback

  • Loading branch information
elpaso committed Feb 19, 2019
1 parent e735d9f commit 03cc355f8e5785169bed7b1341b6228e585f3e8b
@@ -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();
}

@@ -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()
@@ -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__':

0 comments on commit 03cc355

Please sign in to comment.
You can’t perform that action at this time.