Skip to content

Commit 5d495b8

Browse files
feat: fill logs with meaningful data src/Migration (#46)
1 parent d077e38 commit 5d495b8

File tree

10 files changed

+68
-37
lines changed

10 files changed

+68
-37
lines changed

src/Migration/Logging/Log/Builder/SwagMigrationLogBuilder.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,11 @@ public function withExceptionTrace(array $exceptionTrace): self
128128
*/
129129
public function build(string $logClass): AbstractSwagMigrationLogEntry
130130
{
131-
$log = new $logClass(
131+
if (!class_exists($logClass) || !is_subclass_of($logClass, AbstractSwagMigrationLogEntry::class)) {
132+
throw MigrationException::failedToCreateMigrationLog($logClass);
133+
}
134+
135+
return new $logClass(
132136
$this->runId,
133137
$this->profileName,
134138
$this->gatewayName,
@@ -141,11 +145,5 @@ public function build(string $logClass): AbstractSwagMigrationLogEntry
141145
$this->exceptionMessage,
142146
$this->exceptionTrace,
143147
);
144-
145-
if ($log instanceof AbstractSwagMigrationLogEntry) {
146-
return $log;
147-
}
148-
149-
throw MigrationException::failedToCreateMigrationLog($logClass);
150148
}
151149
}

src/Migration/Media/Processor/HttpDownloadServiceBase.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
use Doctrine\DBAL\Connection;
1111
use GuzzleHttp\Exception\RequestException;
12-
use GuzzleHttp\Promise;
1312
use GuzzleHttp\Promise\PromiseInterface;
1413
use GuzzleHttp\Promise\Utils;
1514
use Psr\Http\Message\ResponseInterface;
1615
use Shopware\Core\Content\Media\File\FileSaver;
1716
use Shopware\Core\Content\Media\File\MediaFile;
17+
use Shopware\Core\Content\Media\MediaDefinition;
1818
use Shopware\Core\Content\Media\MediaException;
1919
use Shopware\Core\Framework\Context;
2020
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
@@ -77,10 +77,11 @@ public function process(MigrationContextInterface $migrationContext, Context $co
7777
if ($client === null) {
7878
$exception = new \Exception('Http download client can not be constructed.');
7979

80-
$this->loggingService->addLogEntry( // TODO: add optional fields
80+
$this->loggingService->addLogEntry(
8181
SwagMigrationLogBuilder::fromMigrationContext($migrationContext)
8282
->withExceptionMessage($exception->getMessage())
8383
->withExceptionTrace($exception->getTrace())
84+
->withEntityName(MediaDefinition::ENTITY_NAME)
8485
->build(ExceptionRunLog::class)
8586
);
8687
$this->loggingService->saveLogging($context);
@@ -122,8 +123,9 @@ function (MediaProcessWorkloadStruct $work) use ($uuid) {
122123
$failureUuids[] = $uuid;
123124
$work->setState(MediaProcessWorkloadStruct::ERROR_STATE);
124125

125-
$this->loggingService->addLogEntry( // TODO: add optional fields
126+
$this->loggingService->addLogEntry(
126127
SwagMigrationLogBuilder::fromMigrationContext($migrationContext)
128+
->withEntityName(MediaDefinition::ENTITY_NAME)
127129
->build(CannotGetFileRunLog::class)
128130
);
129131
}
@@ -141,8 +143,9 @@ function (MediaProcessWorkloadStruct $work) use ($uuid) {
141143
$failureUuids[] = $uuid;
142144
$work->setState(MediaProcessWorkloadStruct::ERROR_STATE);
143145

144-
$this->loggingService->addLogEntry( // TODO: add optional fields
146+
$this->loggingService->addLogEntry(
145147
SwagMigrationLogBuilder::fromMigrationContext($migrationContext)
148+
->withEntityName(MediaDefinition::ENTITY_NAME)
146149
->build(TemporaryFileErrorLog::class)
147150
);
148151

@@ -181,10 +184,11 @@ function (MediaProcessWorkloadStruct $work) use ($uuid) {
181184
} catch (\Exception $e) {
182185
$failureUuids[] = $uuid;
183186
$work->setState(MediaProcessWorkloadStruct::ERROR_STATE);
184-
$this->loggingService->addLogEntry( // TODO: add optional fields
187+
$this->loggingService->addLogEntry(
185188
SwagMigrationLogBuilder::fromMigrationContext($migrationContext)
186189
->withExceptionMessage($e->getMessage())
187190
->withExceptionTrace($e->getTrace())
191+
->withEntityName(MediaDefinition::ENTITY_NAME)
188192
->build(ExceptionRunLog::class)
189193
);
190194
} finally {
@@ -279,10 +283,11 @@ private function doNormalDownloadRequest(MigrationContextInterface $migrationCon
279283
$workload->setState(MediaProcessWorkloadStruct::FINISH_STATE);
280284
} catch (\Throwable $exception) {
281285
// this should never happen because of Promises, but just in case something is wrong with request construction
282-
$this->loggingService->addLogEntry( // TODO: add optional fields
286+
$this->loggingService->addLogEntry(
283287
SwagMigrationLogBuilder::fromMigrationContext($migrationContext)
284288
->withExceptionMessage($exception->getMessage())
285289
->withExceptionTrace($exception->getTrace())
290+
->withEntityName(MediaDefinition::ENTITY_NAME)
286291
->build(ExceptionRunLog::class)
287292
);
288293

@@ -302,10 +307,11 @@ private function persistFileToMedia(string $filePath, string $uuid, string $name
302307
if ($fileSize === false || $fileSize === 0 || $mimeType === false) {
303308
$exception = new \Exception('Downloaded file is empty or could not determine mime type.');
304309

305-
$this->loggingService->addLogEntry( // TODO: add optional fields
310+
$this->loggingService->addLogEntry(
306311
SwagMigrationLogBuilder::fromMigrationContext($migrationContext)
307312
->withExceptionMessage($exception->getMessage())
308313
->withExceptionTrace($exception->getTrace())
314+
->withEntityName(MediaDefinition::ENTITY_NAME)
309315
->build(ExceptionRunLog::class)
310316
);
311317

@@ -337,10 +343,11 @@ private function persistFileToMedia(string $filePath, string $uuid, string $name
337343
} elseif (\in_array($mediaException->getErrorCode(), [MediaException::MEDIA_ILLEGAL_FILE_NAME, MediaException::MEDIA_EMPTY_FILE_NAME], true)) {
338344
$this->fileSaver->persistFileToMedia($mediaFile, Uuid::randomHex(), $uuid, $context);
339345
} else {
340-
$this->loggingService->addLogEntry( // TODO: add optional fields
346+
$this->loggingService->addLogEntry(
341347
SwagMigrationLogBuilder::fromMigrationContext($migrationContext)
342348
->withExceptionMessage($mediaException->getMessage())
343349
->withExceptionTrace($mediaException->getTrace())
350+
->withEntityName(MediaDefinition::ENTITY_NAME)
344351
->build(ExceptionRunLog::class)
345352
);
346353
}

src/Migration/MessageQueue/Handler/ProcessMediaHandler.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace SwagMigrationAssistant\Migration\MessageQueue\Handler;
99

10+
use Shopware\Core\Content\Media\MediaDefinition;
1011
use Shopware\Core\Framework\Context;
1112
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
1213
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
@@ -84,18 +85,22 @@ public function __invoke(ProcessMediaMessage $message): void
8485
$processor = $this->mediaFileProcessorRegistry->getProcessor($migrationContext);
8586
$workload = $processor->process($migrationContext, $context, $workload);
8687
$this->processFailures($context, $migrationContext, $processor, $workload);
87-
} catch (NoConnectionFoundException) {
88-
$this->loggingService->addLogEntry( // TODO: add optional fields
88+
} catch (NoConnectionFoundException $exception) {
89+
$this->loggingService->addLogEntry(
8990
SwagMigrationLogBuilder::fromMigrationContext($migrationContext)
91+
->withExceptionMessage($exception->getMessage())
92+
->withExceptionTrace($exception->getTrace())
93+
->withEntityName(MediaDefinition::ENTITY_NAME)
9094
->build(ProcessorNotFoundLog::class)
9195
);
9296

9397
$this->loggingService->saveLogging($context);
9498
} catch (\Exception $e) {
95-
$this->loggingService->addLogEntry( // TODO: add optional fields
99+
$this->loggingService->addLogEntry(
96100
SwagMigrationLogBuilder::fromMigrationContext($migrationContext)
97101
->withExceptionMessage($e->getMessage())
98102
->withExceptionTrace($e->getTrace())
103+
->withEntityName(MediaDefinition::ENTITY_NAME)
99104
->build(ExceptionRunLog::class)
100105
);
101106

src/Migration/Run/RunService.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,17 @@ public function assignThemeToSalesChannel(string $runUuid, Context $context): vo
254254
foreach ($salesChannels as $salesChannel) {
255255
try {
256256
$this->themeService->assignTheme($defaultTheme, $salesChannel, $context);
257-
} catch (\Throwable) {
258-
$this->loggingService->addLogEntry( // TODO: add optional fields
257+
} catch (\Throwable $exception) {
258+
$this->loggingService->addLogEntry(
259259
(new SwagMigrationLogBuilder(
260260
$runUuid,
261261
$connection->getProfileName(),
262262
$connection->getGatewayName(),
263-
))->build(ThemeCompilingErrorRunLog::class)
263+
))
264+
->withExceptionMessage($exception->getMessage())
265+
->withExceptionTrace($exception->getTrace())
266+
->withEntityName(SalesChannelDefinition::ENTITY_NAME)
267+
->build(ThemeCompilingErrorRunLog::class)
264268
);
265269
}
266270
}

src/Migration/Service/MediaFileProcessorService.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public function processMediaFiles(MigrationContextInterface $migrationContext, C
4545
if ($currentDataSet === null) {
4646
try {
4747
$currentDataSet = $this->dataSetRegistry->getDataSet($migrationContext, $mediaFile['entity']);
48-
} catch (DataSetNotFoundException) {
49-
$this->logDataSetNotFoundException($migrationContext);
48+
} catch (DataSetNotFoundException $exception) {
49+
$this->logDataSetNotFoundException($migrationContext, $exception);
5050

5151
continue;
5252
}
@@ -59,8 +59,8 @@ public function processMediaFiles(MigrationContextInterface $migrationContext, C
5959
$messageMediaUuids = [];
6060
$currentCount = 0;
6161
$currentDataSet = $this->dataSetRegistry->getDataSet($migrationContext, $mediaFile['entity']);
62-
} catch (DataSetNotFoundException) {
63-
$this->logDataSetNotFoundException($migrationContext);
62+
} catch (DataSetNotFoundException $exception) {
63+
$this->logDataSetNotFoundException($migrationContext, $exception);
6464

6565
continue;
6666
}
@@ -129,10 +129,14 @@ private function addMessageToBus(string $runUuid, Context $context, DataSet $dat
129129
$this->messageBus->dispatch($message);
130130
}
131131

132-
private function logDataSetNotFoundException(MigrationContextInterface $migrationContext): void
133-
{
134-
$this->loggingService->addLogEntry( // TODO: add optional fields
132+
private function logDataSetNotFoundException(
133+
MigrationContextInterface $migrationContext,
134+
\Throwable $exception
135+
): void {
136+
$this->loggingService->addLogEntry(
135137
SwagMigrationLogBuilder::fromMigrationContext($migrationContext)
138+
->withExceptionMessage($exception->getMessage())
139+
->withExceptionTrace($exception->getTrace())
136140
->build(DataSetNotFoundLog::class)
137141
);
138142
}

src/Migration/Service/MigrationDataConverter.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ public function convert(array $data, MigrationContextInterface $migrationContext
6868
$this->mediaFileService->writeMediaFile($context);
6969
}
7070
} catch (\Throwable $exception) {
71-
$this->loggingService->addLogEntry( // TODO: add optional fields
71+
$this->loggingService->addLogEntry(
7272
SwagMigrationLogBuilder::fromMigrationContext($migrationContext)
7373
->withExceptionMessage($exception->getMessage())
7474
->withExceptionTrace($exception->getTrace())
75+
->withEntityName($dataSet::getEntity())
7576
->build(ExceptionRunLog::class)
7677
);
7778

@@ -104,10 +105,12 @@ private function convertData(
104105
'convertFailure' => $convertFailureFlag,
105106
];
106107
} catch (\Throwable $exception) {
107-
$this->loggingService->addLogEntry( // TODO: add optional fields
108+
$this->loggingService->addLogEntry(
108109
SwagMigrationLogBuilder::fromMigrationContext($migrationContext)
109110
->withExceptionMessage($exception->getMessage())
110111
->withExceptionTrace($exception->getTrace())
112+
->withEntityName($dataSet::getEntity())
113+
->withSourceData($item)
111114
->build(ExceptionRunLog::class)
112115
);
113116

src/Migration/Service/MigrationDataFetcher.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ public function fetchData(MigrationContextInterface $migrationContext, Context $
3535
try {
3636
return $this->gatewayRegistry->getGateway($migrationContext)->read($migrationContext);
3737
} catch (\Throwable $exception) {
38-
$this->loggingService->addLogEntry( // TODO: add optional fields
38+
$this->loggingService->addLogEntry(
3939
SwagMigrationLogBuilder::fromMigrationContext($migrationContext)
4040
->withExceptionMessage($exception->getMessage())
4141
->withExceptionTrace($exception->getTrace())
42+
->withEntityName($dataSet::getEntity())
4243
->build(ExceptionRunLog::class)
4344
);
4445
$this->loggingService->saveLogging($context);

src/Migration/Service/MigrationDataWriter.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,12 @@ public function writeData(MigrationContextInterface $migrationContext, Context $
105105
$currentWriter = $this->writerRegistry->getWriter($dataSet::getEntity());
106106
$currentWriter->writeData(\array_values($converted), $this->writeContext);
107107
} catch (WriterNotFoundException $writerNotFoundException) {
108-
$this->loggingService->addLogEntry( // TODO: add optional fields
108+
$this->loggingService->addLogEntry(
109109
SwagMigrationLogBuilder::fromMigrationContext($migrationContext)
110110
->withExceptionMessage($writerNotFoundException->getMessage())
111111
->withExceptionTrace($writerNotFoundException->getTrace())
112+
->withConvertedData([$converted])
113+
->withEntityName($dataSet::getEntity())
112114
->build(ExceptionRunLog::class)
113115
);
114116
$this->loggingService->saveLogging($context);
@@ -181,8 +183,12 @@ private function handleWriteException(
181183
$updateWrittenData[$dataId]['written'] = false;
182184
$updateWrittenData[$dataId]['writeFailure'] = true;
183185

184-
$this->loggingService->addLogEntry( // TODO: add optional fields
186+
$this->loggingService->addLogEntry(
185187
SwagMigrationLogBuilder::fromMigrationContext($migrationContext)
188+
->withExceptionMessage($exception->getMessage())
189+
->withExceptionTrace($exception->getTrace())
190+
->withEntityName($entityName)
191+
->withConvertedData($entity)
186192
->build(WriteExceptionRunLog::class)
187193
);
188194

@@ -234,10 +240,12 @@ private function writePerEntity(
234240
$currentWriter = $this->writerRegistry->getWriter($entityName);
235241
$currentWriter->writeData([$entity], $this->writeContext);
236242
} catch (\Throwable $exception) {
237-
$this->loggingService->addLogEntry( // TODO: add optional fields
243+
$this->loggingService->addLogEntry(
238244
SwagMigrationLogBuilder::fromMigrationContext($migrationContext)
239245
->withExceptionMessage($exception->getMessage())
240246
->withExceptionTrace($exception->getTrace())
247+
->withEntityName($entityName)
248+
->withConvertedData([$entity])
241249
->build(ExceptionRunLog::class)
242250
);
243251

src/Migration/Subscriber/MessageQueueSubscriber.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function onWorkerMessageFailed(WorkerMessageFailedEvent $event): void
8989
* Raise exception counter and log the exception
9090
*/
9191
$progress->raiseExceptionCount();
92-
$this->loggingService->addLogEntry( // TODO: add optional fields
92+
$this->loggingService->addLogEntry(
9393
(new SwagMigrationLogBuilder(
9494
$run->getId(),
9595
$connection?->getProfileName() ?? 'unknown',
@@ -108,7 +108,7 @@ public function onWorkerMessageFailed(WorkerMessageFailedEvent $event): void
108108
$progress->setIsAborted(true);
109109
$this->updateRun($run->getId(), $progress, $message->getContext());
110110

111-
$this->loggingService->addLogEntry( // TODO: add optional fields
111+
$this->loggingService->addLogEntry(
112112
(new SwagMigrationLogBuilder(
113113
$run->getId(),
114114
$connection?->getProfileName() ?? 'unknown',

tests/Migration/Media/Process/HttpDownloadServiceBaseTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Psr\Http\Message\StreamInterface;
1818
use Shopware\Core\Content\Media\File\FileSaver;
1919
use Shopware\Core\Content\Media\File\MediaFile;
20+
use Shopware\Core\Content\Media\MediaDefinition;
2021
use Shopware\Core\Framework\Context;
2122
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\QueryBuilder;
2223
use Shopware\Core\Framework\Log\Package;
@@ -203,7 +204,7 @@ public function testProcessWithRequestFailure(): void
203204
'profileName' => '',
204205
'gatewayName' => '',
205206
'userFixable' => false,
206-
'entityName' => null,
207+
'entityName' => MediaDefinition::ENTITY_NAME,
207208
'fieldName' => null,
208209
'fieldSourcePath' => null,
209210
'sourceData' => null,

0 commit comments

Comments
 (0)