From f21bb25c97c4cd092e27417d0e7e31e5be4f0881 Mon Sep 17 00:00:00 2001 From: Dave Long Date: Sun, 3 Mar 2024 10:21:09 +0000 Subject: [PATCH] Issue #3197915 by quietone, joachim, Tanuj., karishmaamin, dww: Add details to MigrateSkipRowException messages caused by stub creation --- .../migrate/process/MigrationLookup.php | 17 ++++++++- .../src/Unit/process/MigrationLookupTest.php | 36 ++++++++++++++++--- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/core/modules/migrate/src/Plugin/migrate/process/MigrationLookup.php b/core/modules/migrate/src/Plugin/migrate/process/MigrationLookup.php index 367ed28153f..194057dbf4c 100644 --- a/core/modules/migrate/src/Plugin/migrate/process/MigrationLookup.php +++ b/core/modules/migrate/src/Plugin/migrate/process/MigrationLookup.php @@ -263,7 +263,22 @@ class MigrationLookup extends ProcessPluginBase implements ContainerFactoryPlugi throw $e; } catch (MigrateSkipRowException $e) { - throw $e; + // Build a new message. + $skip_row_exception_message = $e->getMessage(); + if (empty($skip_row_exception_message)) { + $new_message = sprintf("Migration lookup for destination '%s' attempted to create a stub using migration %s, which resulted in a row skip", + $destination_property, + $stub_migration, + ); + } + else { + $new_message = sprintf("Migration lookup for destination '%s' attempted to create a stub using migration %s, which resulted in a row skip, with message '%s'", + $destination_property, + $stub_migration, + $skip_row_exception_message, + ); + } + throw new MigrateSkipRowException($new_message, 0); } catch (\Exception $e) { throw new MigrateException(sprintf('%s was thrown while attempting to stub: %s', get_class($e), $e->getMessage()), $e->getCode(), $e); diff --git a/core/modules/migrate/tests/src/Unit/process/MigrationLookupTest.php b/core/modules/migrate/tests/src/Unit/process/MigrationLookupTest.php index d054deb50d0..a849aedb41c 100644 --- a/core/modules/migrate/tests/src/Unit/process/MigrationLookupTest.php +++ b/core/modules/migrate/tests/src/Unit/process/MigrationLookupTest.php @@ -4,7 +4,7 @@ declare(strict_types=1); namespace Drupal\Tests\migrate\Unit\process; -use Drupal\migrate\MigrateException; +use Drupal\migrate\MigrateSkipRowException; use Drupal\migrate\Plugin\MigrationInterface; use Drupal\migrate\Plugin\migrate\process\MigrationLookup; use Drupal\migrate\Plugin\MigrateIdMapInterface; @@ -48,8 +48,10 @@ class MigrationLookupTest extends MigrationLookupTestCase { /** * @covers ::transform + * + * @dataProvider providerTestTransformWithStubbing */ - public function testTransformWithStubbing() { + public function testTransformWithStubbing($exception_class, $exception_message, $expected_message): void { $migration_plugin = $this->prophesize(MigrationInterface::class); $this->migrateLookup->lookup('destination_migration', [1])->willReturn(NULL); $this->migrateStub->createStub('destination_migration', [1], [], FALSE)->willReturn([2]); @@ -63,11 +65,35 @@ class MigrationLookupTest extends MigrationLookupTestCase { $result = $migration->transform(1, $this->migrateExecutable, $this->row, ''); $this->assertEquals(2, $result); - $this->migrateStub->createStub('destination_migration', [1], [], FALSE)->willThrow(new \Exception('Oh noes!')); + $this->migrateStub->createStub('destination_migration', [1], [], FALSE)->willThrow(new $exception_class($exception_message)); $migration = MigrationLookup::create($this->prepareContainer(), $configuration, '', [], $migration_plugin->reveal()); - $this->expectException(MigrateException::class); - $this->expectExceptionMessage('Exception was thrown while attempting to stub: Oh noes!'); + $this->expectException($exception_class); + $this->expectExceptionMessage($expected_message); $migration->transform(1, $this->migrateExecutable, $this->row, ''); + + } + + /** + * Provides data for testTransformWithStubbing(). + */ + public static function providerTestTransformWithStubbing(): array { + return [ + [ + \Exception::class, + 'Oh noes!', + 'Exception was thrown while attempting to stub: Oh noes!', + ], + [ + MigrateSkipRowException::class, + 'Oh noes!', + "Migration lookup for destination '' attempted to create a stub using migration destination_migration, which resulted in a row skip, with message 'Oh noes!'", + ], + [ + MigrateSkipRowException::class, + '', + "Migration lookup for destination '' attempted to create a stub using migration destination_migration, which resulted in a row skip", + ], + ]; } /**