Skip to content

Commit 0a092cc

Browse files
committedNov 23, 2017
[processing] Throw an exception when sinks cannot be created
Gives users debugging information messages on why the layers could not be created Also allow overwriting existing layers in geopackage outputs
1 parent c04b91f commit 0a092cc

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed
 

‎src/core/processing/qgsprocessingutils.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,9 @@ QgsFeatureSink *QgsProcessingUtils::createFeatureSink( QString &destination, Qgs
372372
{
373373
// memory provider cannot be used with QgsVectorLayerImport - so create layer manually
374374
std::unique_ptr< QgsVectorLayer > layer( QgsMemoryProviderUtils::createMemoryLayer( destination, fields, geometryType, crs ) );
375-
if ( !layer )
376-
return nullptr;
377-
378-
if ( !layer->isValid() )
375+
if ( !layer || !layer->isValid() )
379376
{
380-
return nullptr;
377+
throw QgsProcessingException( QObject::tr( "Could not create memory layer" ) );
381378
}
382379

383380
// update destination to layer ID
@@ -403,17 +400,24 @@ QgsFeatureSink *QgsProcessingUtils::createFeatureSink( QString &destination, Qgs
403400
// use QgsVectorFileWriter for OGR destinations instead of QgsVectorLayerImport, as that allows
404401
// us to use any OGR format which supports feature addition
405402
QString finalFileName;
406-
QgsVectorFileWriter *writer = new QgsVectorFileWriter( destination, options.value( QStringLiteral( "fileEncoding" ) ).toString(), fields, geometryType, crs, format, QgsVectorFileWriter::defaultDatasetOptions( format ),
403+
std::unique_ptr< QgsVectorFileWriter > writer = qgis::make_unique< QgsVectorFileWriter >( destination, options.value( QStringLiteral( "fileEncoding" ) ).toString(), fields, geometryType, crs, format, QgsVectorFileWriter::defaultDatasetOptions( format ),
407404
QgsVectorFileWriter::defaultLayerOptions( format ), &finalFileName );
405+
406+
if ( writer->hasError() )
407+
{
408+
throw QgsProcessingException( QObject::tr( "Could not create layer %1: %2" ).arg( destination, writer->errorMessage() ) );
409+
}
408410
destination = finalFileName;
409-
return writer;
411+
return writer.release();
410412
}
411413
else
412414
{
413415
//create empty layer
414-
std::unique_ptr< QgsVectorLayerExporter > exporter( new QgsVectorLayerExporter( uri, providerKey, fields, geometryType, crs, false, options ) );
416+
std::unique_ptr< QgsVectorLayerExporter > exporter( new QgsVectorLayerExporter( uri, providerKey, fields, geometryType, crs, true, options ) );
415417
if ( exporter->errorCode() )
416-
return nullptr;
418+
{
419+
throw QgsProcessingException( QObject::tr( "Could not create layer %1: %2" ).arg( destination, exporter->errorMessage() ) );
420+
}
417421

418422
// use destination string as layer name (eg "postgis:..." )
419423
if ( !layerName.isEmpty() )

0 commit comments

Comments
 (0)
Please sign in to comment.