Skip to content
Permalink
Browse files
Use a safer approach to showing friendly error messages in expression
builder when a feature isn't available

Wait till we know that the expression evaluation failed before showing
the message -- there's some cases where we get false positives for
referenced columns/geometry tests in expressions so we can't
definitively know upfront that an expression requires a feature
to evaluate

Fixes #42884
  • Loading branch information
nyalldawson committed Jun 8, 2021
1 parent cd94bfa commit db74013c7fc5966436fc41b3d912e93367bbec23
Showing with 17 additions and 11 deletions.
  1. +17 −11 src/gui/qgsexpressionpreviewwidget.cpp
@@ -80,16 +80,6 @@ void QgsExpressionPreviewWidget::refreshPreview()
{
mExpression = QgsExpression( mExpressionText );

if ( !mExpressionContext.feature().isValid() )
{
if ( !mExpression.referencedColumns().isEmpty() || mExpression.needsGeometry() )
{
mPreviewLabel->setText( tr( "No feature was found on this layer to evaluate the expression." ) );
mPreviewLabel->setStyleSheet( QStringLiteral( "color: rgba(220, 125, 0, 255);" ) );
return;
}
}

if ( mUseGeomCalculator )
{
// only set an explicit geometry calculator if a call to setGeomCalculator was made. If not,
@@ -106,7 +96,23 @@ void QgsExpressionPreviewWidget::refreshPreview()

if ( mExpression.hasParserError() || mExpression.hasEvalError() )
{
QString errorString = mExpression.parserErrorString().replace( "\n", "<br>" );
// if parser error was a result of missing feature, then skip the misleading parser error message
// and instead show a friendly message, and allow the user to accept the expression anyway
// refs https://github.com/qgis/QGIS/issues/42884
if ( !mExpressionContext.feature().isValid() )
{
if ( !mExpression.referencedColumns().isEmpty() || mExpression.needsGeometry() )
{
mPreviewLabel->setText( tr( "No feature was found on this layer to evaluate the expression." ) );
mPreviewLabel->setStyleSheet( QStringLiteral( "color: rgba(220, 125, 0, 255);" ) );
emit expressionParsed( true );
setParserError( false );
setEvalError( false );
return;
}
}

QString errorString = mExpression.parserErrorString().replace( QLatin1String( "\n" ), QLatin1String( "<br>" ) );
QString tooltip;
if ( mExpression.hasParserError() )
tooltip = QStringLiteral( "<b>%1:</b>"

0 comments on commit db74013

Please sign in to comment.