From d95ac4a7eacf565da4a965393b62760a627bf808 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Thu, 18 Apr 2024 10:01:44 +1000 Subject: [PATCH] Better reporting for invalid field names for some algorithms --- python/plugins/processing/algs/qgis/BasicStatistics.py | 6 +++++- src/analysis/processing/qgsalgorithmjoinbyattribute.cpp | 7 +++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/python/plugins/processing/algs/qgis/BasicStatistics.py b/python/plugins/processing/algs/qgis/BasicStatistics.py index cd9d220f4f83..630d767d575f 100644 --- a/python/plugins/processing/algs/qgis/BasicStatistics.py +++ b/python/plugins/processing/algs/qgis/BasicStatistics.py @@ -134,7 +134,11 @@ def processAlgorithm(self, parameters, context, feedback): raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT_LAYER)) field_name = self.parameterAsString(parameters, self.FIELD_NAME, context) - field = source.fields().at(source.fields().lookupField(field_name)) + field_idx = source.fields().lookupField(field_name) + if field_idx < 0: + raise QgsProcessingException(self.tr("Invalid field for statistics: “{}” does not exist").format(field_name)) + + field = source.fields().at(field_idx) output_file = self.parameterAsFileOutput(parameters, self.OUTPUT_HTML_FILE, context) diff --git a/src/analysis/processing/qgsalgorithmjoinbyattribute.cpp b/src/analysis/processing/qgsalgorithmjoinbyattribute.cpp index 0fd488eccebc..7fe8d6da71d7 100644 --- a/src/analysis/processing/qgsalgorithmjoinbyattribute.cpp +++ b/src/analysis/processing/qgsalgorithmjoinbyattribute.cpp @@ -121,9 +121,12 @@ QVariantMap QgsJoinByAttributeAlgorithm::processAlgorithm( const QVariantMap &pa const QStringList fieldsToCopy = parameterAsStrings( parameters, QStringLiteral( "FIELDS_TO_COPY" ), context ); const int joinField1Index = input->fields().lookupField( field1Name ); + if ( joinField1Index < 0 ) + throw QgsProcessingException( QObject::tr( "Invalid join field from layer 1: “%1” does not exist" ).arg( field1Name ) ); + const int joinField2Index = input2->fields().lookupField( field2Name ); - if ( joinField1Index < 0 || joinField2Index < 0 ) - throw QgsProcessingException( QObject::tr( "Invalid join fields" ) ); + if ( joinField2Index < 0 ) + throw QgsProcessingException( QObject::tr( "Invalid join field from layer 2: “%1” does not exist" ).arg( field2Name ) ); QgsFields outFields2; QgsAttributeList fields2Indices;