Skip to content

Commit

Permalink
Ensure more specialised exceptions are raised in PyQGIS
Browse files Browse the repository at this point in the history
instead of the generic QgsException one
Refs warning in #55481 (comment)

```
/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)
```
  • Loading branch information
nyalldawson authored and troopa81 committed Dec 12, 2023
1 parent 7325003 commit 3dd2c38
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions python/core/qgsexception.sip
Original file line number Diff line number Diff line change
Expand Up @@ -22,51 +22,57 @@
%End
};

%Exception QgsException(SIP_Exception) /PyName=QgsException/
%Exception QgsProviderConnectionException(SIP_Exception) /PyName=QgsProviderConnectionException/
{
%TypeHeaderCode
#include <qgsexception.h>
%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 <qgsexception.h>
%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 <qgsexception.h>
%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 <qgsexception.h>
%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

0 comments on commit 3dd2c38

Please sign in to comment.