Skip to content

Commit 722b964

Browse files
committed
fix: improve error handling for invalid profile parameters
1 parent 1e0e3ff commit 722b964

9 files changed

+136
-51
lines changed

ORStools/proc/directions_lines_proc.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
)
5151

5252
from qgis.PyQt.QtGui import QIcon
53-
from ORStools.common import directions_core, PROFILES, PREFERENCES, OPTIMIZATION_MODES, EXTRA_INFOS
53+
from ORStools.common import directions_core, PREFERENCES, OPTIMIZATION_MODES, EXTRA_INFOS
5454
from ORStools.utils import transform, exceptions, logger
5555
from .base_processing_algorithm import ORSBaseProcessingAlgorithm
5656
from ..utils.processing import get_params_optimize
@@ -245,7 +245,16 @@ def processAlgorithm(
245245
)
246246
)
247247
except (exceptions.ApiError, exceptions.InvalidKey, exceptions.GenericServerError) as e:
248-
msg = f"Feature ID {num} caused a {e.__class__.__name__}:\n{str(e)}"
248+
if (
249+
isinstance(e, exceptions.ApiError)
250+
and "Parameter 'profile' has incorrect value" in e.message
251+
):
252+
provider = self.providers[parameters[self.IN_PROVIDER]]["name"]
253+
msg = self.tr(
254+
f'The selected profile "{profile}" is not available in the chosen provider "{provider}"'
255+
)
256+
else:
257+
msg = f"Feature ID {num} caused a {e.__class__.__name__}:\n{str(e)}"
249258
feedback.reportError(msg)
250259
logger.log(msg)
251260
continue

ORStools/proc/directions_points_layer_proc.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
QgsProcessingFeedback,
5050
)
5151

52-
from ORStools.common import directions_core, PROFILES, PREFERENCES, OPTIMIZATION_MODES, EXTRA_INFOS
52+
from ORStools.common import directions_core, PREFERENCES, OPTIMIZATION_MODES, EXTRA_INFOS
5353
from ORStools.utils import transform, exceptions, logger
5454
from .base_processing_algorithm import ORSBaseProcessingAlgorithm
5555
from ..utils.gui import GuiUtils
@@ -286,7 +286,16 @@ def sort(f):
286286
)
287287
)
288288
except (exceptions.ApiError, exceptions.InvalidKey, exceptions.GenericServerError) as e:
289-
msg = f"Feature ID {from_value} caused a {e.__class__.__name__}:\n{str(e)}"
289+
if (
290+
isinstance(e, exceptions.ApiError)
291+
and "Parameter 'profile' has incorrect value" in e.message
292+
):
293+
provider = self.providers[parameters[self.IN_PROVIDER]]["name"]
294+
msg = self.tr(
295+
f'The selected profile "{profile}" is not available in the chosen provider "{provider}"'
296+
)
297+
else:
298+
msg = f"Feature ID {from_value} caused a {e.__class__.__name__}:\n{str(e)}"
290299
feedback.reportError(msg)
291300
logger.log(msg)
292301
continue

ORStools/proc/directions_points_layers_proc.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
QgsProcessingFeedback,
4747
)
4848

49-
from ORStools.common import directions_core, PROFILES, PREFERENCES, EXTRA_INFOS
49+
from ORStools.common import directions_core, PREFERENCES, EXTRA_INFOS
5050
from ORStools.utils import transform, exceptions, logger
5151
from .base_processing_algorithm import ORSBaseProcessingAlgorithm
5252

@@ -246,9 +246,21 @@ def sort_end(f):
246246
f"/v2/{endpoint}/{profile}/geojson", {}, post_json=params
247247
)
248248
except (exceptions.ApiError, exceptions.InvalidKey, exceptions.GenericServerError) as e:
249-
msg = f"Route from {values[0]} to {values[1]} caused a {e.__class__.__name__}:\n{str(e)}"
250-
feedback.reportError(msg)
251-
logger.log(msg)
249+
if (
250+
isinstance(e, exceptions.ApiError)
251+
and "Parameter 'profile' has incorrect value" in e.message
252+
):
253+
provider = self.providers[parameters[self.IN_PROVIDER]]["name"]
254+
msg = self.tr(
255+
f'The selected profile "{profile}" is not available in the chosen provider "{provider}"'
256+
)
257+
feedback.reportError(msg)
258+
logger.log(msg)
259+
break
260+
else:
261+
msg = f"Route from {values[0]} to {values[1]} caused a {e.__class__.__name__}:\n{str(e)}"
262+
feedback.reportError(msg)
263+
logger.log(msg)
252264
continue
253265

254266
if extra_info:

ORStools/proc/export_proc.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
from ..utils.wrapper import create_qgs_field
5050

5151

52-
from ORStools.common import PROFILES
5352
from ORStools.utils import exceptions, logger
5453
from .base_processing_algorithm import ORSBaseProcessingAlgorithm
5554

@@ -150,7 +149,16 @@ def processAlgorithm(
150149
sink_point.addFeature(point_feat)
151150

152151
except (exceptions.ApiError, exceptions.InvalidKey, exceptions.GenericServerError) as e:
153-
msg = f"{e.__class__.__name__}: {str(e)}"
152+
if (
153+
isinstance(e, exceptions.ApiError)
154+
and "Parameter 'profile' has incorrect value" in e.message
155+
):
156+
provider = self.providers[parameters[self.IN_PROVIDER]]["name"]
157+
msg = self.tr(
158+
f'The selected profile "{profile}" is not available in the chosen provider "{provider}"'
159+
)
160+
else:
161+
msg = f"{e.__class__.__name__}: {str(e)}"
154162
feedback.reportError(msg)
155163
logger.log(msg)
156164

ORStools/proc/isochrones_layer_proc.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
QgsProcessingFeedback,
4646
)
4747

48-
from ORStools.common import isochrones_core, PROFILES, DIMENSIONS, LOCATION_TYPES
48+
from ORStools.common import isochrones_core, DIMENSIONS, LOCATION_TYPES
4949
from ORStools.proc.base_processing_algorithm import ORSBaseProcessingAlgorithm
5050
from ORStools.utils import transform, exceptions, logger
5151
from ORStools.utils.gui import GuiUtils
@@ -206,9 +206,21 @@ def processAlgorithm(
206206
sink.addFeature(isochrone)
207207

208208
except (exceptions.ApiError, exceptions.InvalidKey, exceptions.GenericServerError) as e:
209-
msg = f"Feature ID {params['id']} caused a {e.__class__.__name__}:\n{str(e)}"
210-
feedback.reportError(msg)
211-
logger.log(msg, 2)
209+
if (
210+
isinstance(e, exceptions.ApiError)
211+
and "Parameter 'profile' has incorrect value" in e.message
212+
):
213+
provider = self.providers[parameters[self.IN_PROVIDER]]["name"]
214+
msg = self.tr(
215+
f'The selected profile "{profile}" is not available in the chosen provider "{provider}"'
216+
)
217+
feedback.reportError(msg)
218+
logger.log(msg, 2)
219+
break
220+
else:
221+
msg = f"Feature ID {params['id']} caused a {e.__class__.__name__}:\n{str(e)}"
222+
feedback.reportError(msg)
223+
logger.log(msg, 2)
212224
continue
213225
feedback.setProgress(int(100.0 / source.featureCount() * num))
214226

ORStools/proc/isochrones_point_proc.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
QgsProcessingFeedback,
4343
)
4444

45-
from ORStools.common import isochrones_core, PROFILES, DIMENSIONS, LOCATION_TYPES
45+
from ORStools.common import isochrones_core, DIMENSIONS, LOCATION_TYPES
4646
from ORStools.utils import exceptions, logger
4747
from .base_processing_algorithm import ORSBaseProcessingAlgorithm
4848
from ..utils.gui import GuiUtils
@@ -164,7 +164,16 @@ def processAlgorithm(
164164
sink.addFeature(isochrone)
165165

166166
except (exceptions.ApiError, exceptions.InvalidKey, exceptions.GenericServerError) as e:
167-
msg = f"Feature ID {params['id']} caused a {e.__class__.__name__}:\n{str(e)}"
167+
if (
168+
isinstance(e, exceptions.ApiError)
169+
and "Parameter 'profile' has incorrect value" in e.message
170+
):
171+
provider = self.providers[parameters[self.IN_PROVIDER]]["name"]
172+
msg = self.tr(
173+
f'The selected profile "{profile}" is not available in the chosen provider "{provider}"'
174+
)
175+
else:
176+
msg = f"Feature ID {params['id']} caused a {e.__class__.__name__}:\n{str(e)}"
168177
feedback.reportError(msg)
169178
logger.log(msg, 2)
170179

ORStools/proc/matrix_proc.py

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646

4747
from qgis.PyQt.QtCore import QMetaType
4848

49-
from ORStools.common import PROFILES
5049
from ORStools.utils import transform, exceptions, logger
5150
from .base_processing_algorithm import ORSBaseProcessingAlgorithm
5251
from ..utils.gui import GuiUtils
@@ -182,41 +181,52 @@ def processAlgorithm(
182181
endpoint = self.get_endpoint_names_from_provider(parameters[self.IN_PROVIDER])["matrix"]
183182
response = ors_client.request(f"/v2/{endpoint}/{profile}", {}, post_json=params)
184183

185-
except (exceptions.ApiError, exceptions.InvalidKey, exceptions.GenericServerError) as e:
186-
msg = f"{e.__class__.__name__}: {str(e)}"
187-
feedback.reportError(msg)
188-
logger.log(msg)
184+
(sink, dest_id) = self.parameterAsSink(
185+
parameters, self.OUT, context, sink_fields, QgsWkbTypes.Type.NoGeometry
186+
)
189187

190-
(sink, dest_id) = self.parameterAsSink(
191-
parameters, self.OUT, context, sink_fields, QgsWkbTypes.Type.NoGeometry
192-
)
188+
sources_attributes = [
189+
feat.attribute(source_field_name) if source_field_name else feat.id()
190+
for feat in sources_features
191+
]
192+
destinations_attributes = [
193+
feat.attribute(destination_field_name) if destination_field_name else feat.id()
194+
for feat in destination_features
195+
]
193196

194-
sources_attributes = [
195-
feat.attribute(source_field_name) if source_field_name else feat.id()
196-
for feat in sources_features
197-
]
198-
destinations_attributes = [
199-
feat.attribute(destination_field_name) if destination_field_name else feat.id()
200-
for feat in destination_features
201-
]
197+
for s, source in enumerate(sources_attributes):
198+
for d, destination in enumerate(destinations_attributes):
199+
duration = response["durations"][s][d]
200+
distance = response["distances"][s][d]
201+
feat = QgsFeature()
202+
feat.setAttributes(
203+
[
204+
source,
205+
destination,
206+
duration / 3600 if duration is not None else None,
207+
distance / 1000 if distance is not None else None,
208+
]
209+
)
202210

203-
for s, source in enumerate(sources_attributes):
204-
for d, destination in enumerate(destinations_attributes):
205-
duration = response["durations"][s][d]
206-
distance = response["distances"][s][d]
207-
feat = QgsFeature()
208-
feat.setAttributes(
209-
[
210-
source,
211-
destination,
212-
duration / 3600 if duration is not None else None,
213-
distance / 1000 if distance is not None else None,
214-
]
215-
)
211+
sink.addFeature(feat)
216212

217-
sink.addFeature(feat)
213+
return {self.OUT: dest_id}
214+
215+
except (exceptions.ApiError, exceptions.InvalidKey, exceptions.GenericServerError) as e:
216+
if (
217+
isinstance(e, exceptions.ApiError)
218+
and "Parameter 'profile' has incorrect value" in e.message
219+
):
220+
provider = self.providers[parameters[self.IN_PROVIDER]]["name"]
221+
msg = self.tr(
222+
f'The selected profile "{profile}" is not available in the chosen provider "{provider}"'
223+
)
224+
else:
225+
msg = f"Feature ID {params['id']} caused a {e.__class__.__name__}:\n{str(e)}"
226+
feedback.reportError(msg)
227+
logger.log(msg)
218228

219-
return {self.OUT: dest_id}
229+
return {self.OUT: ""}
220230

221231
# TODO working source_type and destination_type differ in both name and type from get_fields in directions_core.
222232
# Change to be consistent

ORStools/proc/snap_layer_proc.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
QgsCoordinateReferenceSystem,
4343
)
4444

45-
from ORStools.common import PROFILES
4645
from ORStools.utils.gui import GuiUtils
4746
from ORStools.utils.processing import get_snapped_point_features
4847
from ORStools.proc.base_processing_algorithm import ORSBaseProcessingAlgorithm
@@ -131,7 +130,16 @@ def processAlgorithm(
131130
sink.addFeature(feat)
132131

133132
except (exceptions.ApiError, exceptions.InvalidKey, exceptions.GenericServerError) as e:
134-
msg = f"{e.__class__.__name__}: {str(e)}"
133+
if (
134+
isinstance(e, exceptions.ApiError)
135+
and "Parameter 'profile' has incorrect value" in e.message
136+
):
137+
provider = self.providers[parameters[self.IN_PROVIDER]]["name"]
138+
msg = self.tr(
139+
f'The selected profile "{profile}" is not available in the chosen provider "{provider}"'
140+
)
141+
else:
142+
msg = f"{e.__class__.__name__}: {str(e)}"
135143
feedback.reportError(msg)
136144
logger.log(msg)
137145

ORStools/proc/snap_point_proc.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
QgsCoordinateReferenceSystem,
4242
)
4343

44-
from ORStools.common import PROFILES
4544
from ORStools.utils.gui import GuiUtils
4645
from ORStools.utils.processing import get_snapped_point_features
4746
from ORStools.proc.base_processing_algorithm import ORSBaseProcessingAlgorithm
@@ -116,7 +115,16 @@ def processAlgorithm(
116115
sink.addFeature(feat)
117116

118117
except (exceptions.ApiError, exceptions.InvalidKey, exceptions.GenericServerError) as e:
119-
msg = f"{e.__class__.__name__}: {str(e)}"
118+
if (
119+
isinstance(e, exceptions.ApiError)
120+
and "Parameter 'profile' has incorrect value" in e.message
121+
):
122+
provider = self.providers[parameters[self.IN_PROVIDER]]["name"]
123+
msg = self.tr(
124+
f'The selected profile "{profile}" is not available in the chosen provider "{provider}"'
125+
)
126+
else:
127+
msg = f"Feature ID {params['id']} caused a {e.__class__.__name__}:\n{str(e)}"
120128
feedback.reportError(msg)
121129
logger.log(msg)
122130

0 commit comments

Comments
 (0)