Skip to content
Permalink
Browse files

Add a bit of context to errors in QgsRelationReferenceFieldFormatter

  • Loading branch information
github-actions authored and nyalldawson committed Dec 18, 2019
1 parent fef798e commit 452752860f49acee1ea2a551828f02848d2794fe
Showing with 18 additions and 11 deletions.
  1. +18 −11 src/core/fieldformatter/qgsrelationreferencefieldformatter.cpp
@@ -35,34 +35,38 @@ QString QgsRelationReferenceFieldFormatter::representValue( QgsVectorLayer *laye
return cache.value<QMap<QVariant, QString>>().value( value );
}

const QString fieldName = fieldIndex < layer->fields().size() ? layer->fields().at( fieldIndex ).name() : QObject::tr( "<unknown>" );

// Some sanity checks
if ( !config.contains( QStringLiteral( "Relation" ) ) )
{
QgsMessageLog::logMessage( QObject::tr( "Missing Relation in configuration" ) );
QgsMessageLog::logMessage( QObject::tr( "Layer %1, field %2: Missing Relation in configuration" ).arg( layer->name(), fieldName ) );
return value.toString();
}
QgsRelation relation = QgsProject::instance()->relationManager()->relation( config[QStringLiteral( "Relation" )].toString() );

const QString relationName = config[QStringLiteral( "Relation" )].toString();
QgsRelation relation = QgsProject::instance()->relationManager()->relation( relationName );
if ( !relation.isValid() )
{
QgsMessageLog::logMessage( QObject::tr( "Invalid relation" ) );
QgsMessageLog::logMessage( QObject::tr( "Layer %1, field %2: Invalid relation %3" ).arg( layer->name(), fieldName, relationName ) );
return value.toString();
}
QgsVectorLayer *referencingLayer = relation.referencingLayer();
if ( layer != referencingLayer )
{
QgsMessageLog::logMessage( QObject::tr( "representValue() with inconsistent layer parameter w.r.t relation referencingLayer" ) );
QgsMessageLog::logMessage( QObject::tr( "Layer %1, field %2: representValue() with inconsistent layer parameter w.r.t relation referencingLayer" ).arg( layer->name(), fieldName ) );
return value.toString();
}
int referencingFieldIdx = referencingLayer->fields().lookupField( relation.fieldPairs().at( 0 ).first );
if ( referencingFieldIdx != fieldIndex )
{
QgsMessageLog::logMessage( QObject::tr( "representValue() with inconsistent fieldIndex parameter w.r.t relation referencingFieldIdx" ) );
QgsMessageLog::logMessage( QObject::tr( "Layer %1, field %2: representValue() with inconsistent fieldIndex parameter w.r.t relation referencingFieldIdx" ).arg( layer->name(), fieldName ) );
return value.toString();
}
QgsVectorLayer *referencedLayer = relation.referencedLayer();
if ( !referencedLayer )
{
QgsMessageLog::logMessage( QObject::tr( "Cannot find referenced layer" ) );
QgsMessageLog::logMessage( QObject::tr( "Layer %1, field %2: Cannot find referenced layer" ).arg( layer->name(), fieldName ) );
return value.toString();
}

@@ -99,34 +103,37 @@ QVariant QgsRelationReferenceFieldFormatter::createCache( QgsVectorLayer *layer,
Q_UNUSED( fieldIndex )
QMap<QVariant, QString> cache;

const QString fieldName = fieldIndex < layer->fields().size() ? layer->fields().at( fieldIndex ).name() : QObject::tr( "<unknown>" );

// Some sanity checks
if ( !config.contains( QStringLiteral( "Relation" ) ) )
{
QgsMessageLog::logMessage( QObject::tr( "Missing Relation in configuration" ) );
QgsMessageLog::logMessage( QObject::tr( "Layer %1, field %2: Missing Relation in configuration" ).arg( layer->name(), fieldName ) );
return QVariant();
}
const QString relationName = config[QStringLiteral( "Relation" )].toString();
QgsRelation relation = QgsProject::instance()->relationManager()->relation( config[QStringLiteral( "Relation" )].toString() );
if ( !relation.isValid() )
{
QgsMessageLog::logMessage( QObject::tr( "Invalid relation" ) );
QgsMessageLog::logMessage( QObject::tr( "Layer %1, field %2: Invalid relation %3" ).arg( layer->name(), fieldName, relationName ) );
return QVariant();
}
QgsVectorLayer *referencingLayer = relation.referencingLayer();
if ( layer != referencingLayer )
{
QgsMessageLog::logMessage( QObject::tr( "representValue() with inconsistent layer parameter w.r.t relation referencingLayer" ) );
QgsMessageLog::logMessage( QObject::tr( "Layer %1, field %2: representValue() with inconsistent layer parameter w.r.t relation referencingLayer" ).arg( layer->name(), fieldName ) );
return QVariant();
}
QgsVectorLayer *referencedLayer = relation.referencedLayer();
if ( !referencedLayer )
{
QgsMessageLog::logMessage( QObject::tr( "Cannot find referenced layer" ) );
QgsMessageLog::logMessage( QObject::tr( "Layer %1, field %2: Cannot find referenced layer" ).arg( layer->name(), fieldName ) );
return QVariant();
}
int referencedFieldIdx = referencedLayer->fields().lookupField( relation.fieldPairs().at( 0 ).second );
if ( referencedFieldIdx == -1 )
{
QgsMessageLog::logMessage( QObject::tr( "Invalid referenced field (%1) configured in relation %2" ).arg( relation.fieldPairs().at( 0 ).second, relation.name() ) );
QgsMessageLog::logMessage( QObject::tr( "Layer %1, field %2: Invalid referenced field (%3) configured in relation %4" ).arg( layer->name(), fieldName, relation.fieldPairs().at( 0 ).second, relation.name() ) );
return QVariant();
}

0 comments on commit 4527528

Please sign in to comment.
You can’t perform that action at this time.