From 706a8429f3f6242526877727dacb06f178cd2ad5 Mon Sep 17 00:00:00 2001 From: Etienne Trimaille Date: Wed, 15 May 2024 15:05:37 +0200 Subject: [PATCH] Improve logging when invalid vector layer found --- lizmap_server/core.py | 33 ++++++++++++++++++++++------- lizmap_server/get_legend_graphic.py | 5 ++++- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/lizmap_server/core.py b/lizmap_server/core.py index b05d3d31..22135838 100755 --- a/lizmap_server/core.py +++ b/lizmap_server/core.py @@ -7,7 +7,7 @@ from functools import lru_cache from pathlib import Path -from typing import Dict, Tuple, Union +from typing import Dict, Optional, Tuple, Union from qgis.core import ( Qgis, @@ -49,25 +49,42 @@ def find_vector_layer_from_params(params, project): return True, layer -def find_vector_layer(layer_name: str, project: QgsProject) -> Union[None, QgsVectorLayer]: +def find_vector_layer(layer_name: str, project: QgsProject) -> Optional[QgsVectorLayer]: """ Find vector layer with name, short name or layer id. """ + found = None for layer in project.mapLayers().values(): + # only vector layer if layer.type() != QgsMapLayer.VectorLayer: continue + # check name if layer.name() == layer_name: - return layer + found = layer + break + # check short name if layer.shortName() == layer_name: - return layer + found = layer + break + # check layer id if layer.id() == layer_name: - return layer + found = layer + break + + if not found: + Logger.warning( + "The vector layer '{}' has not been found in the project '{}'".format(layer_name, project.fileName())) + return None - Logger.warning( - "The vector layer '{}' has not been found in the project '{}'".format(layer_name, project.fileName())) - return None + found: QgsVectorLayer + if not found.isValid(): + Logger.warning( + f"The vector layer '{layer_name}' has been found but it is not valid in the project " + f"'{project.fileName()}'" + ) + return found def get_server_fid(feature: QgsFeature, pk_attributes: list) -> str: diff --git a/lizmap_server/get_legend_graphic.py b/lizmap_server/get_legend_graphic.py index 750016a9..117227e6 100644 --- a/lizmap_server/get_legend_graphic.py +++ b/lizmap_server/get_legend_graphic.py @@ -92,7 +92,10 @@ def responseComplete(self): return if not layer.isValid(): - + logger.warning( + f"Layer '{layer_name}' is not valid, returning a warning icon in the legend for project " + f"'{project.homePath()}'" + ) json_data = { 'title': '', 'nodes': [{