@@ -372,12 +372,9 @@ QgsFeatureSink *QgsProcessingUtils::createFeatureSink( QString &destination, Qgs
372
372
{
373
373
// memory provider cannot be used with QgsVectorLayerImport - so create layer manually
374
374
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 () )
379
376
{
380
- return nullptr ;
377
+ throw QgsProcessingException ( QObject::tr ( " Could not create memory layer " ) ) ;
381
378
}
382
379
383
380
// update destination to layer ID
@@ -403,17 +400,24 @@ QgsFeatureSink *QgsProcessingUtils::createFeatureSink( QString &destination, Qgs
403
400
// use QgsVectorFileWriter for OGR destinations instead of QgsVectorLayerImport, as that allows
404
401
// us to use any OGR format which supports feature addition
405
402
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 ),
407
404
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
+ }
408
410
destination = finalFileName;
409
- return writer;
411
+ return writer. release () ;
410
412
}
411
413
else
412
414
{
413
415
// 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 ) );
415
417
if ( exporter->errorCode () )
416
- return nullptr ;
418
+ {
419
+ throw QgsProcessingException ( QObject::tr ( " Could not create layer %1: %2" ).arg ( destination, exporter->errorMessage () ) );
420
+ }
417
421
418
422
// use destination string as layer name (eg "postgis:..." )
419
423
if ( !layerName.isEmpty () )
0 commit comments