Skip to content

Commit

Permalink
[processing] Avoid generic exceptions
Browse files Browse the repository at this point in the history
Makes the full Python exception available when running algorithm,
which provides much more useful error messages
  • Loading branch information
nyalldawson committed May 15, 2018
1 parent 0f78277 commit c9e5a36
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ Creates a new expression context scope for a child algorithm within the model.
virtual QgsProcessingAlgorithm *createInstance() const /Factory/;


virtual QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback );
virtual QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) throw( QgsProcessingException );


};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,7 @@ manner.
Returns true if this algorithm generates HTML outputs.
%End

QVariantMap run( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback, bool *ok /Out/ = 0, const QVariantMap &configuration = QVariantMap() ) const;
QVariantMap run( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback, bool *ok /Out/ = 0, const QVariantMap &configuration = QVariantMap() ) const;
%Docstring
Executes the algorithm using the specified ``parameters``. This method internally
creates a copy of the algorithm before running it, so it is safe to call
Expand Down Expand Up @@ -312,7 +311,7 @@ calling runPrepared() (which is safe to do in any thread).
of the algorithm should be created with clone() and prepare()/runPrepared() called on the copy.
%End

QVariantMap runPrepared( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback );
QVariantMap runPrepared( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) throw( QgsProcessingException );
%Docstring
Runs the algorithm, which has been prepared by an earlier call to prepare().
This method is safe to call from any thread. Returns true if the algorithm was successfully executed.
Expand Down Expand Up @@ -455,7 +454,7 @@ See the notes in addParameter() for a description of when this occurs.
.. seealso:: :py:func:`initAlgorithm`
%End

virtual bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) /VirtualErrorHandler=processing_exception_handler/;
virtual bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) throw( QgsProcessingException ) /VirtualErrorHandler=processing_exception_handler/;
%Docstring
Prepares the algorithm to run using the specified ``parameters``. Algorithms should implement
their logic for evaluating parameter values here. The evaluated parameter results should
Expand Down Expand Up @@ -489,7 +488,7 @@ will be canceled.
.. seealso:: :py:func:`postProcessAlgorithm`
%End

virtual QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) = 0 /VirtualErrorHandler=processing_exception_handler/;
virtual QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) throw( QgsProcessingException ) = 0 /VirtualErrorHandler=processing_exception_handler/;
%Docstring
Runs the algorithm using the specified ``parameters``. Algorithms should implement
their custom processing logic here.
Expand Down Expand Up @@ -522,7 +521,7 @@ algorithm.
.. seealso:: :py:func:`postProcessAlgorithm`
%End

virtual QVariantMap postProcessAlgorithm( QgsProcessingContext &context, QgsProcessingFeedback *feedback ) /VirtualErrorHandler=processing_exception_handler/;
virtual QVariantMap postProcessAlgorithm( QgsProcessingContext &context, QgsProcessingFeedback *feedback ) throw( QgsProcessingException ) /VirtualErrorHandler=processing_exception_handler/;
%Docstring
Allows the algorithm to perform any required cleanup tasks. The returned variant map
includes the results evaluated by the algorithm. These may be output layer references, or calculated
Expand Down Expand Up @@ -907,7 +906,7 @@ Returns the source's coordinate reference system. This will only return a valid
called from a subclasses' processFeature() implementation.
%End

virtual QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) = 0 /VirtualErrorHandler=processing_exception_handler/;
virtual QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) throw( QgsProcessingException ) = 0 /VirtualErrorHandler=processing_exception_handler/;
%Docstring
Processes an individual input ``feature`` from the source. Algorithms should implement their
logic in this method for performing the algorithm's operation (e.g. replacing the feature's
Expand All @@ -932,8 +931,8 @@ can break valid model execution - so use with extreme caution, and consider usin
``feedback`` to instead report non-fatal processing failures for features instead.
%End

virtual QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback );
virtual QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) throw( QgsProcessingException );


virtual QgsFeatureRequest request() const;
%Docstring
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,7 @@ Converts a string to a Python string literal. E.g. by replacing ' with \'.
%End


static void createFeatureSinkPython(
QgsFeatureSink **sink /Out,TransferBack/,
QString &destination /In,Out/,
QgsProcessingContext &context,
const QgsFields &fields,
QgsWkbTypes::Type geometryType,
const QgsCoordinateReferenceSystem &crs,
const QVariantMap &createOptions = QVariantMap() ) /PyName=createFeatureSink/;
static void createFeatureSinkPython( QgsFeatureSink **sink /Out,TransferBack/, QString &destination /In,Out/, QgsProcessingContext &context, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs, const QVariantMap &createOptions = QVariantMap() ) throw( QgsProcessingException ) /PyName=createFeatureSink/;
%Docstring
Creates a feature sink ready for adding features. The ``destination`` specifies a destination
URI for the resultant layer. It may be updated in place to reflect the actual destination
Expand Down
2 changes: 1 addition & 1 deletion src/core/processing/models/qgsprocessingmodelalgorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm

QgsProcessingAlgorithm *createInstance() const override SIP_FACTORY;

QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override SIP_THROW( QgsProcessingException );

private:

Expand Down
16 changes: 7 additions & 9 deletions src/core/processing/qgsprocessingalgorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,7 @@ class CORE_EXPORT QgsProcessingAlgorithm
* \note this method can only be called from the main thread. Use prepare(), runPrepared() and postProcess()
* if you need to run algorithms from a background thread, or use the QgsProcessingAlgRunnerTask class.
*/
QVariantMap run( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback, bool *ok SIP_OUT = nullptr, const QVariantMap &configuration = QVariantMap() ) const;
QVariantMap run( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback, bool *ok SIP_OUT = nullptr, const QVariantMap &configuration = QVariantMap() ) const;

/**
* Prepares the algorithm for execution. This must be run in the main thread, and allows the algorithm
Expand All @@ -336,7 +335,7 @@ class CORE_EXPORT QgsProcessingAlgorithm
* on algorithms directly retrieved from QgsProcessingRegistry and QgsProcessingProvider. Instead, a copy
* of the algorithm should be created with clone() and prepare()/runPrepared() called on the copy.
*/
QVariantMap runPrepared( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback );
QVariantMap runPrepared( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException );

/**
* Should be called in the main thread following the completion of runPrepared(). This method
Expand Down Expand Up @@ -487,7 +486,7 @@ class CORE_EXPORT QgsProcessingAlgorithm
* \see processAlgorithm()
* \see postProcessAlgorithm()
*/
virtual bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_VIRTUALERRORHANDLER( processing_exception_handler );
virtual bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) SIP_VIRTUALERRORHANDLER( processing_exception_handler );

/**
* Runs the algorithm using the specified \a parameters. Algorithms should implement
Expand Down Expand Up @@ -519,7 +518,7 @@ class CORE_EXPORT QgsProcessingAlgorithm
* \see prepareAlgorithm()
* \see postProcessAlgorithm()
*/
virtual QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) = 0 SIP_VIRTUALERRORHANDLER( processing_exception_handler );
virtual QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) = 0 SIP_VIRTUALERRORHANDLER( processing_exception_handler );

/**
* Allows the algorithm to perform any required cleanup tasks. The returned variant map
Expand Down Expand Up @@ -548,7 +547,7 @@ class CORE_EXPORT QgsProcessingAlgorithm
* \see prepareAlgorithm()
* \see processAlgorithm()
*/
virtual QVariantMap postProcessAlgorithm( QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_VIRTUALERRORHANDLER( processing_exception_handler );
virtual QVariantMap postProcessAlgorithm( QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) SIP_VIRTUALERRORHANDLER( processing_exception_handler );

/**
* Evaluates the parameter with matching \a name to a static string value.
Expand Down Expand Up @@ -939,10 +938,9 @@ class CORE_EXPORT QgsProcessingFeatureBasedAlgorithm : public QgsProcessingAlgor
* can break valid model execution - so use with extreme caution, and consider using
* \a feedback to instead report non-fatal processing failures for features instead.
*/
virtual QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) = 0 SIP_VIRTUALERRORHANDLER( processing_exception_handler );
virtual QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) = 0 SIP_VIRTUALERRORHANDLER( processing_exception_handler );

QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override SIP_THROW( QgsProcessingException );

/**
* Returns the feature request used for fetching features to process from the
Expand Down
9 changes: 1 addition & 8 deletions src/core/processing/qgsprocessingutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,7 @@ class CORE_EXPORT QgsProcessingUtils
* SIP bindings. c++ code should call the other createFeatureSink() version.
* \note available in Python bindings as createFeatureSink()
*/
static void createFeatureSinkPython(
QgsFeatureSink **sink SIP_OUT SIP_TRANSFERBACK,
QString &destination SIP_INOUT,
QgsProcessingContext &context,
const QgsFields &fields,
QgsWkbTypes::Type geometryType,
const QgsCoordinateReferenceSystem &crs,
const QVariantMap &createOptions = QVariantMap() ) SIP_PYNAME( createFeatureSink );
static void createFeatureSinkPython( QgsFeatureSink **sink SIP_OUT SIP_TRANSFERBACK, QString &destination SIP_INOUT, QgsProcessingContext &context, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs, const QVariantMap &createOptions = QVariantMap() ) SIP_THROW( QgsProcessingException ) SIP_PYNAME( createFeatureSink );

/**
* Combines the extent of several map \a layers. If specified, the target \a crs
Expand Down

0 comments on commit c9e5a36

Please sign in to comment.