diff --git a/python/plugins/processing/algs/qgis/SelectByAttribute.py b/python/plugins/processing/algs/qgis/SelectByAttribute.py index 7c703026a9d..ec0dced4005 100644 --- a/python/plugins/processing/algs/qgis/SelectByAttribute.py +++ b/python/plugins/processing/algs/qgis/SelectByAttribute.py @@ -130,6 +130,9 @@ class SelectByAttribute(QgisAlgorithm): fields = layer.fields() idx = layer.fields().lookupField(fieldName) + if idx < 0: + raise QgsProcessingException(self.tr("Field '{}' was not found in layer").format(fieldName)) + fieldType = fields[idx].type() if fieldType != QVariant.String and operator in self.STRING_OPERATORS: diff --git a/python/plugins/processing/tests/AlgorithmsTestBase.py b/python/plugins/processing/tests/AlgorithmsTestBase.py index 70c006eeb9d..eb790726435 100644 --- a/python/plugins/processing/tests/AlgorithmsTestBase.py +++ b/python/plugins/processing/tests/AlgorithmsTestBase.py @@ -119,6 +119,9 @@ class AlgorithmsTest(object): exec(('\n'.join(defs['expectedFailure'][:-1])), globals(), locals()) expectFailure = eval(defs['expectedFailure'][-1]) + if 'expectedException' in defs: + expectFailure = True + # ignore user setting for invalid geometry handling context = QgsProcessingContext() context.setProject(QgsProject.instance()) diff --git a/python/plugins/processing/tests/testdata/custom/circular_strings.gpkg b/python/plugins/processing/tests/testdata/custom/circular_strings.gpkg index f9ffa0055a5..6e6dd4f82a0 100644 Binary files a/python/plugins/processing/tests/testdata/custom/circular_strings.gpkg and b/python/plugins/processing/tests/testdata/custom/circular_strings.gpkg differ diff --git a/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml b/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml index 91a5ea6d0e2..a3364c16d30 100644 --- a/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml +++ b/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml @@ -2086,6 +2086,20 @@ tests: name: expected/extract_by_attribute_not_null.gml type: vector + - algorithm: native:extractbyattribute + name: Extract by attribute (bad field) + params: + FIELD: notafield + INPUT: + name: polys.gml + type: vector + OPERATOR: '9' + results: + OUTPUT: + name: expected/failure.gml + type: vector + expectedException: true + - algorithm: native:extractbyattribute name: Extract by attribute (starts with) params: diff --git a/src/analysis/processing/qgsalgorithmextractbyattribute.cpp b/src/analysis/processing/qgsalgorithmextractbyattribute.cpp index f7e6a8e9190..f528b638342 100644 --- a/src/analysis/processing/qgsalgorithmextractbyattribute.cpp +++ b/src/analysis/processing/qgsalgorithmextractbyattribute.cpp @@ -101,8 +101,10 @@ QVariantMap QgsExtractByAttributeAlgorithm::processAlgorithm( const QVariantMap std::unique_ptr< QgsFeatureSink > nonMatchingSink( parameterAsSink( parameters, QStringLiteral( "FAIL_OUTPUT" ), context, nonMatchingSinkId, source->fields(), source->wkbType(), source->sourceCrs() ) ); - int idx = source->fields().lookupField( fieldName ); + if ( idx < 0 ) + throw QgsProcessingException( QObject::tr( "Field '%1' was not found in INPUT source" ).arg( fieldName ) ); + QVariant::Type fieldType = source->fields().at( idx ).type(); if ( fieldType != QVariant::String && ( op == BeginsWith || op == Contains || op == DoesNotContain ) )