fix(podcast-import): rollback transaction before exception is thrown

This allows errors' messages to resurface and prevent the script of having the generic "Process was
killed." error.

fixes #429, closes #319, #443, #438
This commit is contained in:
Yassine Doghri 2024-02-19 11:08:00 +00:00
parent d0a94dd2cb
commit 419bb04716
3 changed files with 24 additions and 11 deletions

View File

@ -28,7 +28,7 @@ if (! function_exists('hint_tooltip')) {
{ {
$tooltip = $tooltip =
'<span data-tooltip="bottom" tabindex="0" title="' . '<span data-tooltip="bottom" tabindex="0" title="' .
$hintText . esc($hintText) .
'" class="inline-block align-middle opacity-75 focus:ring-accent'; '" class="inline-block align-middle opacity-75 focus:ring-accent';
if ($class !== '') { if ($class !== '') {

View File

@ -100,9 +100,9 @@ class PodcastImport extends BaseCommand
// FIXME: getting named routes doesn't work from v4.3 anymore, so loading all routes before importing // FIXME: getting named routes doesn't work from v4.3 anymore, so loading all routes before importing
Services::routes()->loadRoutes(); Services::routes()->loadRoutes();
try {
$this->init(); $this->init();
try {
CLI::write('All good! Feed was parsed successfully!'); CLI::write('All good! Feed was parsed successfully!');
CLI::write( CLI::write(
@ -173,7 +173,7 @@ class PodcastImport extends BaseCommand
$this->error($exception->getMessage()); $this->error($exception->getMessage());
log_message( log_message(
'critical', 'critical',
'Error when importing ' . $this->importTask->feed_url . PHP_EOL . $exception->getTraceAsString() 'Error when importing ' . $this->importTask?->feed_url . PHP_EOL . $exception->getMessage() . PHP_EOL . $exception->getTraceAsString()
); );
} }
} }
@ -196,9 +196,6 @@ class PodcastImport extends BaseCommand
private function importPodcast(): Podcast private function importPodcast(): Podcast
{ {
$db = db_connect();
$db->transStart();
$location = null; $location = null;
if ($this->podcastFeed->channel->podcast_location->getValue() !== null) { if ($this->podcastFeed->channel->podcast_location->getValue() !== null) {
$location = new Location( $location = new Location(
@ -213,7 +210,19 @@ class PodcastImport extends BaseCommand
} }
if (($coverUrl = $this->getCoverUrl($this->podcastFeed->channel)) === null) { if (($coverUrl = $this->getCoverUrl($this->podcastFeed->channel)) === null) {
throw new Exception('Missing podcast cover. Please include an <itunes:image> tag'); throw new Exception('Missing podcast cover. Please include an <itunes:image> tag.');
}
if (($ownerName = $this->podcastFeed->channel->itunes_owner->itunes_name->getValue() === null)) {
throw new Exception(
'Missing podcast owner name. Please include an <itunes:name> tag inside the <itunes:owner> tag.'
);
}
if (($ownerEmail = $this->podcastFeed->channel->itunes_owner->itunes_email->getValue() === null)) {
throw new Exception(
'Missing podcast owner email. Please include an <itunes:email> tag inside the <itunes:owner> tag.'
);
} }
$parentalAdvisory = null; $parentalAdvisory = null;
@ -221,6 +230,9 @@ class PodcastImport extends BaseCommand
$parentalAdvisory = $this->podcastFeed->channel->itunes_explicit->getValue() ? 'explicit' : 'clean'; $parentalAdvisory = $this->podcastFeed->channel->itunes_explicit->getValue() ? 'explicit' : 'clean';
} }
$db = db_connect();
$db->transStart();
$htmlConverter = new HtmlConverter(); $htmlConverter = new HtmlConverter();
$podcast = new Podcast([ $podcast = new Podcast([
'created_by' => $this->user->id, 'created_by' => $this->user->id,
@ -237,8 +249,8 @@ class PodcastImport extends BaseCommand
'language_code' => $this->importTask->language, 'language_code' => $this->importTask->language,
'category_id' => $this->importTask->category, 'category_id' => $this->importTask->category,
'parental_advisory' => $parentalAdvisory, 'parental_advisory' => $parentalAdvisory,
'owner_name' => $this->podcastFeed->channel->itunes_owner->itunes_name->getValue(), 'owner_name' => $ownerName,
'owner_email' => $this->podcastFeed->channel->itunes_owner->itunes_email->getValue(), 'owner_email' => $ownerEmail,
'publisher' => $this->podcastFeed->channel->itunes_author->getValue(), 'publisher' => $this->podcastFeed->channel->itunes_author->getValue(),
'type' => $this->podcastFeed->channel->itunes_type->getValue(), 'type' => $this->podcastFeed->channel->itunes_type->getValue(),
'copyright' => $this->podcastFeed->channel->copyright->getValue(), 'copyright' => $this->podcastFeed->channel->copyright->getValue(),
@ -446,6 +458,7 @@ class PodcastImport extends BaseCommand
} }
if (($showNotes = $this->getShowNotes($item)) === null) { if (($showNotes = $this->getShowNotes($item)) === null) {
$db->transRollback();
throw new Exception('Missing item show notes. Please include a <description> tag to item ' . $key); throw new Exception('Missing item show notes. Please include a <description> tag to item ' . $key);
} }

View File

@ -38,7 +38,7 @@ use Modules\PodcastImport\Entities\TaskStatus;
'passed' => '', 'passed' => '',
]; ];
$errorHint = $importTask->status === TaskStatus::Failed ? hint_tooltip($importTask->error, 'ml-1') : ''; $errorHint = $importTask->status === TaskStatus::Failed ? hint_tooltip(esc($importTask->error), 'ml-1') : '';
return '<div class="flex items-center"><Pill variant="' . $pillVariantMap[$importTask->status->value] . '" icon="' . $pillIconMap[$importTask->status->value] . '" iconClass="' . $pillIconClassMap[$importTask->status->value] . '" hint="' . lang('PodcastImport.queue.status.' . $importTask->status->value . '_hint') . '">' . lang('PodcastImport.queue.status.' . $importTask->status->value) . '</Pill>' . $errorHint . '</div>'; return '<div class="flex items-center"><Pill variant="' . $pillVariantMap[$importTask->status->value] . '" icon="' . $pillIconMap[$importTask->status->value] . '" iconClass="' . $pillIconClassMap[$importTask->status->value] . '" hint="' . lang('PodcastImport.queue.status.' . $importTask->status->value . '_hint') . '">' . lang('PodcastImport.queue.status.' . $importTask->status->value) . '</Pill>' . $errorHint . '</div>';
}, },