[QGIS-Developer] How to notify an error from QgsProcessingAlgorithm.processAlgorithm ?

Nyall Dawson nyall.dawson at gmail.com
Tue Feb 4 15:56:53 PST 2020


On Wed, 5 Feb 2020 at 01:04, Jean-Charles Quillet
<jeancharles.quillet at gmail.com> wrote:
>
> Hi !
>
> I'm trying to implement a processing algorithm in Python.
>
> It's not clear how I am supposed to handle errors during the execution of the algorithms. The documentation is talking about raising the exception GeoAlgorithmExecutionException
> https://qgis.org/pyqgis/3.8/core/QgsProcessingAlgorithm.html#qgis.core.QgsProcessingAlgorithm.processAlgorithm

Ouch, outdated dox. This should say "QgsProcessingException". I'll fix.

> unfortunately this exception doesn't exist. I've tried to raise QgsProcessingException. But from the output window, the exception does not seem to be well handled. I also tried to return an empty dictionary and then the output window doesn't seem to notify any problem at all. I've checked out GDAL and GRASS processing plugins but they seem handle errors differently in a very specific way. GDAL raise an IOError and GRASS report error with: feedback.reportError

It depends on the severity of the error.
- If it's a critical error, raise QgsProcessingException. This will
completely terminate the algorithm execution, **AND TERMINATE ANY
MODEL IT IS RUNNING AS PART OF**. Be wary of raising exceptions --
don't raise them for things like "found no features in layer", because
that's potentially a situation which may arise when running an
algorithm in a model, and by raising the exception you effectively
prevent users from utilising your algorithm in their model. So you
only raise exceptions when things are definitely wrong, say for
something like "selected grouping field 'CATEGORY' does not exist in
input layer".

- Otherwise, use reportError. This doesn't terminate your algorithm
(or its parent model), but shows a warning error in the log so that
users know something unexpected happened (e.g. "found no features in
layer").

Nyall


>
> So what is the right way to handle an error in processAlgorithm ?
>
> It's important for me as I want to be able to create new models with the algorithms from the graphical modeler. So if something goes wrong in the middle of a set of algorithms, it definitely need to be reported correctly.
>
> Thanks for your help,
>
> Jean-Charles
>
> PS: I'm using QGIS 3.8.3-Zanzibar on Windows
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer


More information about the QGIS-Developer mailing list