Skip to content

Commit b05f732

Browse files
committedJul 27, 2020
Better error message when no feature available for expression evaluation
(cherry picked from commit 6dbc178)
1 parent 8519e28 commit b05f732

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
 

‎src/core/expression/qgsexpressionnodeimpl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,10 @@ QVariant QgsExpressionNodeColumnRef::evalNode( QgsExpression *parent, const QgsE
13071307
else
13081308
return feature.attribute( mName );
13091309
}
1310+
else
1311+
{
1312+
parent->setEvalErrorString( tr( "No feature available for field '%1' evaluation" ).arg( mName ) );
1313+
}
13101314
}
13111315
if ( index < 0 )
13121316
parent->setEvalErrorString( tr( "Column '%1' not found" ).arg( mName ) );

‎tests/src/core/testqgsexpression.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,46 @@ class TestQgsExpression: public QObject
549549
QVERIFY( expression4.hasEvalError() );
550550
}
551551

552+
void fieldsButNoFeature()
553+
{
554+
// test evaluating an expression with fields in the context but no feature
555+
QgsExpressionContext context;
556+
QgsExpressionContextScope *scope = new QgsExpressionContextScope();
557+
558+
QgsFields fields;
559+
fields.append( QgsField( QStringLiteral( "x" ) ) );
560+
fields.append( QgsField( QStringLiteral( "y" ) ) );
561+
fields.append( QgsField( QStringLiteral( "z" ) ) );
562+
scope->setFields( fields );
563+
context.appendScope( scope );
564+
565+
// doesn't exist
566+
QgsExpression expression( "\"a\"" );
567+
QVERIFY( !expression.hasParserError() );
568+
QVERIFY( !expression.evaluate( &context ).isValid() );
569+
QVERIFY( expression.hasEvalError() );
570+
QCOMPARE( expression.evalErrorString(), QStringLiteral( "Field 'a' not found" ) );
571+
expression = QgsExpression( "\"x\"" );
572+
QVERIFY( !expression.hasParserError() );
573+
QVERIFY( !expression.evaluate( &context ).isValid() );
574+
QVERIFY( expression.hasEvalError() );
575+
QCOMPARE( expression.evalErrorString(), QStringLiteral( "No feature available for field 'x' evaluation" ) );
576+
expression = QgsExpression( "\"y\"" );
577+
QVERIFY( !expression.hasParserError() );
578+
QVERIFY( !expression.evaluate( &context ).isValid() );
579+
QVERIFY( expression.hasEvalError() );
580+
QCOMPARE( expression.evalErrorString(), QStringLiteral( "No feature available for field 'y' evaluation" ) );
581+
582+
QgsFeature f( fields );
583+
f.setValid( true );
584+
f.setAttributes( QgsAttributes() << 1 << 2 << 3 );
585+
scope->setFeature( f );
586+
expression = QgsExpression( "\"z\"" );
587+
QVERIFY( !expression.hasParserError() );
588+
QCOMPARE( expression.evaluate( &context ).toInt(), 3 );
589+
QVERIFY( !expression.hasEvalError() );
590+
}
591+
552592
void evaluation_data()
553593
{
554594
QTest::addColumn<QString>( "string" );

0 commit comments

Comments
 (0)
Please sign in to comment.