Skip to content

Commit

Permalink
added parser exception handling to the parseFeed method
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaishiyoku committed Oct 12, 2024
1 parent 4baecf3 commit 5699264
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/HeraRssCrawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Kaishiyoku\HeraRssCrawler\Models\ResponseContainer;
use Kaishiyoku\HeraRssCrawler\Models\Rss\Feed;
use Kaishiyoku\HeraRssCrawler\Models\Rss\FeedItem;
use Laminas\Feed\Reader\Exception\RuntimeException;
use Laminas\Feed\Reader\Reader;
use Psr\Log\LoggerInterface;
use ReflectionClass;
Expand Down Expand Up @@ -135,13 +136,21 @@ public function setFeedDiscoverers(Collection $feedDiscoverers): void
* @throws ConnectException
* @throws Exception
*/
public function parseFeed(string $url): Feed
public function parseFeed(string $url): ?Feed
{
return $this->withRetries(fn () => Feed::fromZendFeed(
$url,
Reader::importString($this->httpClient->get($url)->getBody()->getContents()),
$this->httpClient
));
return $this->withRetries(function () use ($url) {
try {
return Feed::fromZendFeed(
$url,
Reader::importString($this->httpClient->get($url)->getBody()->getContents()),
$this->httpClient
);
} catch (RuntimeException $exception) {
$this->logger?->error("Feed with URL {$url} not consumable: {$exception->getMessage()}");

return null;
}
});
}

/**
Expand All @@ -156,6 +165,7 @@ public function discoverAndParseFeeds(string $url): Collection
return $this->withRetries(
fn () => $this->discoverFeedUrls($url)
->map(fn ($feedUrl) => $this->parseFeed($feedUrl))
->filter(fn (?Feed $feed) => $feed !== null)
);
}

Expand Down Expand Up @@ -218,10 +228,8 @@ public function checkIfConsumableFeed(string $url): bool
{
try {
return $this->withRetries(fn () => $this->parseFeed($url) instanceof Feed);
} catch (Exception $e) {
if ($this->logger) {
$this->logger->error('Feed not consumable: '.$e->getMessage());
}
} catch (Exception $exception) {
$this->logger?->error("Feed with URL {$url} not consumable: {$exception->getMessage()}");

return false;
}
Expand Down

0 comments on commit 5699264

Please sign in to comment.