Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add a lastError() getter to QgsFeatureSink for retrieving the last error
encountered by the sink (e.g. after adding a feature fails)
  • Loading branch information
nyalldawson committed Jul 22, 2020
1 parent 262a193 commit 3eec9a4
Show file tree
Hide file tree
Showing 16 changed files with 48 additions and 0 deletions.
7 changes: 7 additions & 0 deletions python/core/auto_generated/qgsfeaturesink.sip.in
Expand Up @@ -72,6 +72,13 @@ Adds all features from the specified ``iterator`` to the sink. Feature addition
Flushes any internal buffer which may exist in the sink, causing any buffered features to be added to the sink's destination.

:return: ``False`` if any buffered features could not be added to the sink.
%End

virtual QString lastError() const;
%Docstring
Returns the most recent error encountered by the sink, e.g. when a call to :py:func:`~QgsFeatureSink.addFeatures` returns ``False``.

.. versionadded:: 3.16
%End
};

Expand Down
1 change: 1 addition & 0 deletions python/core/auto_generated/qgsproxyfeaturesink.sip.in
Expand Up @@ -36,6 +36,7 @@ Constructs a new QgsProxyFeatureSink which forwards features onto a destination
virtual bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() );
virtual bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() );
virtual bool addFeatures( QgsFeatureIterator &iterator, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() );
virtual QString lastError() const;

QgsFeatureSink *destinationSink();
%Docstring
Expand Down
Expand Up @@ -192,6 +192,8 @@ Remaps a ``feature`` to a set of features compatible with the destination sink.

virtual bool addFeatures( QgsFeatureIterator &iterator, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() );

virtual QString lastError() const;


QgsFeatureSink *destinationSink();
%Docstring
Expand Down
2 changes: 2 additions & 0 deletions python/core/auto_generated/qgsvectordataprovider.sip.in
Expand Up @@ -234,6 +234,8 @@ or if the given attribute is not an enum type.

virtual bool addFeatures( QgsFeatureList &flist /In,Out/, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() );

virtual QString lastError() const;


virtual bool deleteFeatures( const QgsFeatureIds &id );
%Docstring
Expand Down
2 changes: 2 additions & 0 deletions python/core/auto_generated/qgsvectorfilewriter.sip.in
Expand Up @@ -572,6 +572,8 @@ Retrieves error message

virtual bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() );

virtual QString lastError() const;


bool addFeatureWithStyle( QgsFeature &feature, QgsFeatureRenderer *renderer, QgsUnitTypes::DistanceUnit outputUnit = QgsUnitTypes::DistanceMeters );
%Docstring
Expand Down
2 changes: 2 additions & 0 deletions python/core/auto_generated/qgsvectorlayerexporter.sip.in
Expand Up @@ -124,6 +124,8 @@ Returns the number of error messages encountered during the export.

virtual bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() );

virtual QString lastError() const;


~QgsVectorLayerExporter();

Expand Down
7 changes: 7 additions & 0 deletions src/core/qgsfeaturesink.h
Expand Up @@ -106,6 +106,13 @@ class CORE_EXPORT QgsFeatureSink
* \returns FALSE if any buffered features could not be added to the sink.
*/
virtual bool flushBuffer() { return true; }

/**
* Returns the most recent error encountered by the sink, e.g. when a call to addFeatures() returns FALSE.
*
* \since QGIS 3.16
*/
virtual QString lastError() const { return QString(); }
};

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsFeatureSink::Flags )
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsproxyfeaturesink.h
Expand Up @@ -47,6 +47,7 @@ class CORE_EXPORT QgsProxyFeatureSink : public QgsFeatureSink
bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override { return mSink->addFeature( feature, flags ); }
bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override { return mSink->addFeatures( features, flags ); }
bool addFeatures( QgsFeatureIterator &iterator, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override { return mSink->addFeatures( iterator, flags ); }
QString lastError() const override { return mSink->lastError(); }

/**
* Returns the destination QgsFeatureSink which the proxy will forward features to.
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsremappingproxyfeaturesink.cpp
Expand Up @@ -130,6 +130,11 @@ bool QgsRemappingProxyFeatureSink::addFeatures( QgsFeatureIterator &iterator, Qg
return res;
}

QString QgsRemappingProxyFeatureSink::lastError() const
{
return mSink->lastError();
}

QVariant QgsRemappingSinkDefinition::toVariant() const
{
QVariantMap map;
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsremappingproxyfeaturesink.h
Expand Up @@ -219,6 +219,7 @@ class CORE_EXPORT QgsRemappingProxyFeatureSink : public QgsFeatureSink
bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
bool addFeatures( QgsFeatureIterator &iterator, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
QString lastError() const override;

/**
* Returns the destination QgsFeatureSink which the proxy will forward features to.
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsvectordataprovider.cpp
Expand Up @@ -89,6 +89,11 @@ bool QgsVectorDataProvider::addFeatures( QgsFeatureList &flist, Flags flags )
return false;
}

QString QgsVectorDataProvider::lastError() const
{
return mErrors.last();
}

bool QgsVectorDataProvider::deleteFeatures( const QgsFeatureIds &ids )
{
Q_UNUSED( ids )
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsvectordataprovider.h
Expand Up @@ -260,6 +260,7 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider, public QgsFeat
virtual void enumValues( int index, QStringList &enumList SIP_OUT ) const { Q_UNUSED( index ) enumList.clear(); }

bool addFeatures( QgsFeatureList &flist SIP_INOUT, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
QString lastError() const override;

/**
* Deletes one or more features from the provider. This requires the DeleteFeatures capability.
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -2296,6 +2296,11 @@ bool QgsVectorFileWriter::addFeatures( QgsFeatureList &features, QgsFeatureSink:
return result;
}

QString QgsVectorFileWriter::lastError() const
{
return mErrorMessage;
}

bool QgsVectorFileWriter::addFeatureWithStyle( QgsFeature &feature, QgsFeatureRenderer *renderer, QgsUnitTypes::DistanceUnit outputUnit )
{
// create the feature
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsvectorfilewriter.h
Expand Up @@ -762,6 +762,7 @@ class CORE_EXPORT QgsVectorFileWriter : public QgsFeatureSink

bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
QString lastError() const override;

/**
* Adds a \a feature to the currently opened data source, using the style from a specified \a renderer.
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsvectorlayerexporter.cpp
Expand Up @@ -188,6 +188,11 @@ bool QgsVectorLayerExporter::addFeature( QgsFeature &feat, Flags )
return true;
}

QString QgsVectorLayerExporter::lastError() const
{
return mErrorMessage;
}

bool QgsVectorLayerExporter::flushBuffer()
{
if ( mFeatureBuffer.count() <= 0 )
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsvectorlayerexporter.h
Expand Up @@ -136,6 +136,7 @@ class CORE_EXPORT QgsVectorLayerExporter : public QgsFeatureSink

bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
QString lastError() const override;

/**
* Finalizes the export and closes the new created layer.
Expand Down

0 comments on commit 3eec9a4

Please sign in to comment.