Skip to content

Commit

Permalink
Make saveLayerMetadata raise QgsNotSupportedException if saving metad…
Browse files Browse the repository at this point in the history
…ata for uri is not supported

This allows us to disambiguate between "metadata storage for the
layer SHOULD be supported, but an error occurred while doing this"
vs "metadata storage for the layer will NEVER be supported" (eg for
a WFS layer or other read-only format)
  • Loading branch information
nyalldawson committed May 4, 2021
1 parent 64a23d2 commit 667fe63
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 9 deletions.
6 changes: 5 additions & 1 deletion python/core/auto_generated/qgsprovidermetadata.sip.in
Expand Up @@ -426,7 +426,7 @@ Loads a layer style defined by ``uri``
.. versionadded:: 3.10
%End

virtual bool saveLayerMetadata( const QString &uri, const QgsLayerMetadata &metadata, QString &errorMessage /Out/ );
virtual bool saveLayerMetadata( const QString &uri, const QgsLayerMetadata &metadata, QString &errorMessage /Out/ ) throw( QgsNotSupportedException );
%Docstring
Saves ``metadata`` to the layer corresponding to the specified ``uri``.

Expand All @@ -437,6 +437,10 @@ Saves ``metadata`` to the layer corresponding to the specified ``uri``.
- errorMessage: descriptive string of error if encountered


:raises :: py:class:`QgsNotSupportedException` if the provider does not support saving layer metadata for the
specified ``uri``.


.. versionadded:: 3.20
%End

Expand Down
6 changes: 5 additions & 1 deletion python/core/auto_generated/qgsproviderregistry.sip.in
Expand Up @@ -233,7 +233,7 @@ Loads a layer style defined by ``uri``
.. versionadded:: 3.10
%End

bool saveLayerMetadata( const QString &providerKey, const QString &uri, const QgsLayerMetadata &metadata, QString &errorMessage /Out/ );
bool saveLayerMetadata( const QString &providerKey, const QString &uri, const QgsLayerMetadata &metadata, QString &errorMessage /Out/ ) throw( QgsNotSupportedException );
%Docstring
Saves ``metadata`` to the layer corresponding to the specified ``uri``.

Expand All @@ -245,6 +245,10 @@ Saves ``metadata`` to the layer corresponding to the specified ``uri``.
- errorMessage: descriptive string of error if encountered


:raises :: py:class:`QgsNotSupportedException` if the provider does not support saving layer metadata for the
specified ``uri``.


.. versionadded:: 3.20
%End

Expand Down
5 changes: 2 additions & 3 deletions src/core/qgsprovidermetadata.cpp
Expand Up @@ -232,10 +232,9 @@ QString QgsProviderMetadata::loadStyle( const QString &, QString &errCause )
return QString();
}

bool QgsProviderMetadata::saveLayerMetadata( const QString &, const QgsLayerMetadata &, QString &errorMessage )
bool QgsProviderMetadata::saveLayerMetadata( const QString &, const QgsLayerMetadata &, QString & )
{
errorMessage = QObject::tr( "Provider %1 does not support writing layer metadata" ).arg( key() );
return false;
throw QgsNotSupportedException( QObject::tr( "Provider %1 does not support writing layer metadata" ).arg( key() ) );
}

bool QgsProviderMetadata::createDb( const QString &, QString &errCause )
Expand Down
5 changes: 4 additions & 1 deletion src/core/qgsprovidermetadata.h
Expand Up @@ -483,9 +483,12 @@ class CORE_EXPORT QgsProviderMetadata : public QObject
*
* \returns TRUE if the metadata was successfully saved.
*
* \throws QgsNotSupportedException if the provider does not support saving layer metadata for the
* specified \a uri.
*
* \since QGIS 3.20
*/
virtual bool saveLayerMetadata( const QString &uri, const QgsLayerMetadata &metadata, QString &errorMessage SIP_OUT );
virtual bool saveLayerMetadata( const QString &uri, const QgsLayerMetadata &metadata, QString &errorMessage SIP_OUT ) SIP_THROW( QgsNotSupportedException );

/**
* Creates database by the provider on the path
Expand Down
3 changes: 1 addition & 2 deletions src/core/qgsproviderregistry.cpp
Expand Up @@ -668,8 +668,7 @@ bool QgsProviderRegistry::saveLayerMetadata( const QString &providerKey, const Q
return meta->saveLayerMetadata( uri, metadata, errorMessage );
else
{
errorMessage = QObject::tr( "Unable to load %1 provider" ).arg( providerKey );
return false;
throw QgsNotSupportedException( QObject::tr( "Unable to load %1 provider" ).arg( providerKey ) );
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/core/qgsproviderregistry.h
Expand Up @@ -271,9 +271,12 @@ class CORE_EXPORT QgsProviderRegistry
*
* \returns TRUE if the metadata was successfully saved.
*
* \throws QgsNotSupportedException if the provider does not support saving layer metadata for the
* specified \a uri.
*
* \since QGIS 3.20
*/
bool saveLayerMetadata( const QString &providerKey, const QString &uri, const QgsLayerMetadata &metadata, QString &errorMessage SIP_OUT );
bool saveLayerMetadata( const QString &providerKey, const QString &uri, const QgsLayerMetadata &metadata, QString &errorMessage SIP_OUT ) SIP_THROW( QgsNotSupportedException );

/**
* Creates database by the provider on the path
Expand Down

0 comments on commit 667fe63

Please sign in to comment.