From 3dd2c381727739408c81dff2af8ab261498e1ed6 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Tue, 12 Dec 2023 09:01:13 +1000 Subject: [PATCH] Ensure more specialised exceptions are raised in PyQGIS instead of the generic QgsException one Refs warning in https://github.com/qgis/QGIS/issues/55481#issuecomment-1849001802 ``` /tmp/work/geography/qgis/work/qgis-3.28.13/build/python/core/build/_core/sip_corepart0.cpp:38168: warning: exception of type 'QgsProviderConnectionException' will be caught 38168 | catch (QgsProviderConnectionException &sipExceptionRef) | /tmp/work/geography/qgis/work/qgis-3.28.13/build/python/core/build/_core/sip_corepart0.cpp:38158: warning: by earlier handler for 'QgsException' 38158 | catch (QgsException &sipExceptionRef) ``` --- python/core/qgsexception.sip | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/python/core/qgsexception.sip b/python/core/qgsexception.sip index 404175d98aa3..94200a1027ac 100644 --- a/python/core/qgsexception.sip +++ b/python/core/qgsexception.sip @@ -22,51 +22,57 @@ %End }; -%Exception QgsException(SIP_Exception) /PyName=QgsException/ +%Exception QgsProviderConnectionException(SIP_Exception) /PyName=QgsProviderConnectionException/ { %TypeHeaderCode #include %End %RaiseCode SIP_BLOCK_THREADS - PyErr_SetString(sipException_QgsException, sipExceptionRef.what().toUtf8().constData() ); + PyErr_SetString(sipException_QgsProviderConnectionException, sipExceptionRef.what().toUtf8().constData() ); SIP_UNBLOCK_THREADS %End }; - -%Exception QgsProviderConnectionException(SIP_Exception) /PyName=QgsProviderConnectionException/ +%Exception QgsNotSupportedException(SIP_Exception) /PyName=QgsNotSupportedException/ { %TypeHeaderCode #include %End %RaiseCode SIP_BLOCK_THREADS - PyErr_SetString(sipException_QgsProviderConnectionException, sipExceptionRef.what().toUtf8().constData() ); + PyErr_SetString(sipException_QgsNotSupportedException, sipExceptionRef.what().toUtf8().constData() ); SIP_UNBLOCK_THREADS %End }; -%Exception QgsNotSupportedException(SIP_Exception) /PyName=QgsNotSupportedException/ +%Exception QgsSettingsException(SIP_Exception) /PyName=QgsSettingsException/ { %TypeHeaderCode #include %End %RaiseCode SIP_BLOCK_THREADS - PyErr_SetString(sipException_QgsNotSupportedException, sipExceptionRef.what().toUtf8().constData() ); + PyErr_SetString(sipException_QgsSettingsException, sipExceptionRef.what().toUtf8().constData() ); SIP_UNBLOCK_THREADS %End }; -%Exception QgsSettingsException(SIP_Exception) /PyName=QgsSettingsException/ +// IMPORTANT -- QgsException MUST be last listed, or it will greedily prevent the more +// specialized exceptions from being raised + +%Exception QgsException(SIP_Exception) /PyName=QgsException/ { %TypeHeaderCode #include %End %RaiseCode SIP_BLOCK_THREADS - PyErr_SetString(sipException_QgsSettingsException, sipExceptionRef.what().toUtf8().constData() ); + PyErr_SetString(sipException_QgsException, sipExceptionRef.what().toUtf8().constData() ); SIP_UNBLOCK_THREADS %End }; + +// IMPORTANT -- QgsException MUST be last listed, or it will greedily prevent the more +// specialized exceptions from being raised +