Skip to content

Commit

Permalink
Use a safer approach to showing friendly error messages in expression
Browse files Browse the repository at this point in the history
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 db74013
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/gui/qgsexpressionpreviewwidget.cpp
Expand Up @@ -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,
Expand All @@ -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>"
Expand Down

0 comments on commit db74013

Please sign in to comment.