From 4227938f0618dd152c16c6ee439bcb3ab01f4d4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Germ=C3=A1n=20Carrillo?= Date: Wed, 1 May 2024 12:08:29 +0200 Subject: [PATCH] Check QGIS >= 3.37 to subclass profile sources (before that version, even if we can subclass it, we won't be able to register it anyways); handle exceptions when reading service responses --- swiss_locator/core/profiles/profile_generator.py | 9 ++++++++- swiss_locator/swiss_locator_plugin.py | 11 +++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/swiss_locator/core/profiles/profile_generator.py b/swiss_locator/core/profiles/profile_generator.py index ff041f0..ff34825 100644 --- a/swiss_locator/core/profiles/profile_generator.py +++ b/swiss_locator/core/profiles/profile_generator.py @@ -64,7 +64,14 @@ def url_with_param(url, params) -> str: result = {"error": reply.errorString()} else: content = reply.content() - result = json.loads(str(content, 'utf-8')) + try: + result = json.loads(str(content, 'utf-8')) + except json.decoder.JSONDecodeError as e: + QgsMessageLog.logMessage( + "Unable to parse results from Profile service. Details: {}".format(e.msg), + "Swiss locator", + Qgis.Critical + ) return result diff --git a/swiss_locator/swiss_locator_plugin.py b/swiss_locator/swiss_locator_plugin.py index 6acd821..994c5c8 100644 --- a/swiss_locator/swiss_locator_plugin.py +++ b/swiss_locator/swiss_locator_plugin.py @@ -33,7 +33,11 @@ SwissLocatorFilterLocation, ) from swiss_locator.core.filters.swiss_locator_filter_wmts import SwissLocatorFilterWMTS -from swiss_locator.core.profiles.profile_generator import SwissProfileSource +try: + from swiss_locator.core.profiles.profile_generator import SwissProfileSource +except ImportError: + # Should fail only for QGIS < 3.26, where profiles weren't available + SwissProfileSource = None class SwissLocatorPlugin: @@ -50,7 +54,10 @@ def __init__(self, iface: QgisInterface): QCoreApplication.installTranslator(self.translator) self.locator_filters = [] - self.profile_source = SwissProfileSource() + + if Qgis.QGIS_VERSION_INT >= 33700: + # Only on QGIS 3.37+ we'll be able to register profile sources + self.profile_source = SwissProfileSource() def initGui(self): for _filter in (