mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-10 01:03:13 +08:00
Added errorhandling for individual imported URLs, so that one failing doe snot make the whole process fail
This commit is contained in:
@@ -15,7 +15,9 @@ use Shlinkio\Shlink\Importer\ImportedLinksProcessorInterface;
|
||||
use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl;
|
||||
use Shlinkio\Shlink\Importer\Params\ImportParams;
|
||||
use Shlinkio\Shlink\Importer\Sources\ImportSources;
|
||||
use Symfony\Component\Console\Style\OutputStyle;
|
||||
use Symfony\Component\Console\Style\StyleInterface;
|
||||
use Throwable;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
@@ -33,7 +35,7 @@ class ImportedLinksProcessor implements ImportedLinksProcessorInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @param iterable|ImportedShlinkUrl[] $shlinkUrls
|
||||
* @param iterable<ImportedShlinkUrl> $shlinkUrls
|
||||
*/
|
||||
public function process(StyleInterface $io, iterable $shlinkUrls, ImportParams $params): void
|
||||
{
|
||||
@@ -43,22 +45,26 @@ class ImportedLinksProcessor implements ImportedLinksProcessorInterface
|
||||
|
||||
/** @var ImportedShlinkUrl $importedUrl */
|
||||
foreach ($iterable as $importedUrl) {
|
||||
$skipOnShortCodeConflict = static function () use ($io, $importedUrl): bool {
|
||||
$action = $io->choice(sprintf(
|
||||
'Failed to import URL "%s" because its short-code "%s" is already in use. Do you want to generate '
|
||||
. 'a new one or skip it?',
|
||||
$importedUrl->longUrl(),
|
||||
$importedUrl->shortCode(),
|
||||
), ['Generate new short-code', 'Skip'], 1);
|
||||
|
||||
return $action === 'Skip';
|
||||
};
|
||||
$skipOnShortCodeConflict = static fn (): bool => $io->choice(sprintf(
|
||||
'Failed to import URL "%s" because its short-code "%s" is already in use. Do you want to generate '
|
||||
. 'a new one or skip it?',
|
||||
$importedUrl->longUrl(),
|
||||
$importedUrl->shortCode(),
|
||||
), ['Generate new short-code', 'Skip'], 1) === 'Skip';
|
||||
$longUrl = $importedUrl->longUrl();
|
||||
|
||||
try {
|
||||
$shortUrlImporting = $this->resolveShortUrl($importedUrl, $importShortCodes, $skipOnShortCodeConflict);
|
||||
} catch (NonUniqueSlugException) {
|
||||
$io->text(sprintf('%s: <fg=red>Error</>', $longUrl));
|
||||
continue;
|
||||
} catch (Throwable $e) {
|
||||
$io->text(sprintf('%s: <comment>Skipped</comment>. Reason: %s.', $longUrl, $e->getMessage()));
|
||||
|
||||
if ($io instanceof OutputStyle && $io->isVerbose()) {
|
||||
$io->text($e->__toString());
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ final class ShortUrlImporting
|
||||
}
|
||||
|
||||
/**
|
||||
* @param iterable|ImportedShlinkVisit[] $visits
|
||||
* @param iterable<ImportedShlinkVisit> $visits
|
||||
*/
|
||||
public function importVisits(iterable $visits, EntityManagerInterface $em): string
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user