Skip to content

Commit a42bf80

Browse files
authored
Merge pull request #8283 from elpaso/bugfix-20193-dbmanager-vlayer
[db-manager] Store exception text in the task and pass it over to the…
2 parents f346dce + a51d7f5 commit a42bf80

File tree

6 files changed

+63
-10
lines changed

6 files changed

+63
-10
lines changed

python/core/auto_generated/qgsvirtuallayertask.sip.in

+14
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ Reloads the data.
5858

5959
%Docstring
6060
Cancels the pending query and the parent task.
61+
%End
62+
63+
QString exceptionText() const;
64+
%Docstring
65+
Returns the exception text or an empty string if no exceptions were raised
66+
67+
.. versionadded:: 3.4
68+
%End
69+
70+
void setExceptionText( const QString &exceptionText );
71+
%Docstring
72+
Sets the ``exceptionText``
73+
74+
.. versionadded:: 3.4
6175
%End
6276

6377
};

python/plugins/db_manager/db_plugins/plugin.py

-3
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ def __init__(self, e):
5050
def __unicode__(self):
5151
return self.msg
5252

53-
def __str__(self):
54-
return str(self).encode('utf-8')
55-
5653

5754
class InvalidDataException(BaseError):
5855
pass

python/plugins/db_manager/db_plugins/vlayers/data_model.py

+2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ def __init__(self, db, sql, parent=None):
113113
def modelDone(self):
114114
self.status = self.task.status
115115
self.model = self.task.model
116+
if self.task.subtask.exceptionText():
117+
self.error = BaseError(self.task.subtask.exceptionText())
116118
self.done.emit()
117119

118120

src/core/qgsvirtuallayertask.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ bool QgsVirtualLayerTask::run()
3737
catch ( std::exception &e )
3838
{
3939
QgsDebugMsg( QStringLiteral( "Reload error: %1" ).arg( e.what() ) );
40+
setExceptionText( e.what() );
4041
rc = false;
4142
}
4243
return rc;
@@ -62,3 +63,13 @@ void QgsVirtualLayerTask::cancel()
6263
mLayer->dataProvider()->cancelReload();
6364
QgsTask::cancel();
6465
}
66+
67+
QString QgsVirtualLayerTask::exceptionText() const
68+
{
69+
return mExceptionText;
70+
}
71+
72+
void QgsVirtualLayerTask::setExceptionText( const QString &exceptionText )
73+
{
74+
mExceptionText = exceptionText;
75+
}

src/core/qgsvirtuallayertask.h

+13
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,20 @@ class CORE_EXPORT QgsVirtualLayerTask : public QgsTask
6868
*/
6969
void cancel() override;
7070

71+
/**
72+
* Returns the exception text or an empty string if no exceptions were raised
73+
* \since QGIS 3.4
74+
*/
75+
QString exceptionText() const;
76+
77+
/**
78+
* Sets the \a exceptionText
79+
* \since QGIS 3.4
80+
*/
81+
void setExceptionText( const QString &exceptionText );
82+
7183
private:
84+
QString mExceptionText;
7285
QgsVirtualLayerDefinition mDefinition;
7386
std::unique_ptr<QgsVectorLayer> mLayer;
7487
};

tests/src/python/test_qgsvirtuallayertask.py

+23-7
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,18 @@ class TestQgsVirtualLayerTask(unittest.TestCase):
3333

3434
def setUp(self):
3535
self.testDataDir = unitTestDataPath()
36-
self.success = False
37-
self.fail = False
36+
self._success = False
37+
self._fail = False
3838
self.ids = None
3939
self.task = None
4040

4141
def onSuccess(self):
42-
self.success = True
42+
self._success = True
4343
self.ids = [f.id() for f in self.task.layer().getFeatures()]
4444

4545
def onFail(self):
46-
self.fail = True
46+
self._fail = True
47+
self._exceptionText = self.task.exceptionText()
4748

4849
def test(self):
4950
l1 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "françéà", "ogr", QgsVectorLayer.LayerOptions(False))
@@ -61,14 +62,29 @@ def test(self):
6162
self.task.taskTerminated.connect(self.onFail)
6263

6364
QgsApplication.taskManager().addTask(self.task)
64-
while not self.success and not self.fail:
65+
while not self._success and not self._fail:
6566
QCoreApplication.processEvents()
6667

67-
self.assertTrue(self.success)
68-
self.assertFalse(self.fail)
68+
self.assertTrue(self._success)
69+
self.assertFalse(self._fail)
6970

7071
self.assertEqual(len(self.ids), 4)
7172

73+
# Test exception
74+
self._success = False
75+
self._fail = False
76+
df.setQuery('select *')
77+
self.task = QgsVirtualLayerTask(df)
78+
self.task.taskCompleted.connect(self.onSuccess)
79+
self.task.taskTerminated.connect(self.onFail)
80+
QgsApplication.taskManager().addTask(self.task)
81+
while not self._success and not self._fail:
82+
QCoreApplication.processEvents()
83+
84+
self.assertFalse(self._success)
85+
self.assertTrue(self._fail)
86+
self.assertEqual(self._exceptionText, 'Query preparation error on PRAGMA table_info(_tview): no tables specified', self._exceptionText)
87+
7288

7389
if __name__ == '__main__':
7490
unittest.main()

0 commit comments

Comments
 (0)