Skip to content

Commit 7ed4ab6

Browse files
committed
fix: show the error when basal contact extraction fails
Before, an error in map2loop gave an empty result and the user did not see the cause. Now extract_basal_contacts raises a RuntimeError with the cause. The GUI and the Processing algorithms show this message.
1 parent 19c49ba commit 7ed4ab6

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎loopstructural/main/m2l_api.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,11 @@ def extract_basal_contacts(
295295
-------
296296
dict
297297
Dictionary containing 'basal_contacts' GeoDataFrame and optionally 'all_contacts' GeoDataFrame.
298+
299+
Raises
300+
------
301+
RuntimeError
302+
If map2loop cannot extract the contacts.
298303
"""
299304
geology = qgsLayerToGeoDataFrame(geology, target_crs=target_crs)
300305
if unit_name_field and unit_name_field in geology.columns:
@@ -400,10 +405,11 @@ def extract_basal_contacts(
400405
all_contacts_result.shape,
401406
basal_contacts.shape,
402407
)
403-
except Exception:
408+
except Exception as err:
409+
# Do not hide the error with an empty result. The caller shows the
410+
# message to the user.
404411
logger.exception("Error during contact extraction")
405-
basal_contacts = pd.DataFrame()
406-
all_contacts_result = pd.DataFrame()
412+
raise RuntimeError(f"Could not extract the basal contacts: {err}") from err
407413

408414
if ignore_units and basal_contacts.empty is False:
409415
basal_contacts = basal_contacts[

‎tests/qgis/test_basal_contacts.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22
from pathlib import Path
3+
from unittest.mock import patch
34
from qgis.core import (
45
QgsFeature,
56
QgsGeometry,
@@ -212,6 +213,15 @@ def test_intrusion_in_column(self):
212213
self.assertAlmostEqual(self._length(contacts, 'D'), 300, delta=10)
213214
self.assertAlmostEqual(self._length(contacts, 'A'), 100, delta=5)
214215

216+
def test_extraction_error_is_raised(self):
217+
# An error in map2loop must not give an empty result.
218+
with patch(
219+
'loopstructural.main.m2l_api.ContactExtractor.extract_all_contacts',
220+
side_effect=ValueError('bad geology'),
221+
):
222+
with self.assertRaisesRegex(RuntimeError, 'bad geology'):
223+
self._extract(['A', 'B', 'C'])
224+
215225

216226
if __name__ == '__main__':
217227
unittest.main()

0 commit comments

Comments
 (0)