Skip to content

Commit

Permalink
Add error handling around Browsershot call and send exception to craw…
Browse files Browse the repository at this point in the history
…lFailed
  • Loading branch information
David Racovan committed Jul 29, 2024
1 parent f68bdb0 commit 0210d60
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/Handlers/CrawlRequestFulfilled.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use Spatie\Crawler\CrawlUrl;
use Spatie\Crawler\ResponseWithCachedBody;
use Spatie\Crawler\UrlParsers\UrlParser;
use Symfony\Component\Process\Exception\ProcessFailedException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Exception\RequestException;

class CrawlRequestFulfilled
{
Expand All @@ -39,7 +42,17 @@ public function __invoke(ResponseInterface $response, $index)
$crawlUrl = $this->crawler->getCrawlQueue()->getUrlById($index);

if ($this->crawler->mayExecuteJavaScript()) {
$body = $this->getBodyAfterExecutingJavaScript($crawlUrl->url);
try {
$body = $this->getBodyAfterExecutingJavaScript($crawlUrl->url);
} catch (ProcessFailedException $exception) {
$request = new Request("GET", $crawlUrl->url);
$exception = new RequestException($exception->getMessage(), $request);
$crawlUrl = $this->crawler->getCrawlQueue()->getUrlById($index);

$this->crawler->getCrawlObservers()->crawlFailed($crawlUrl, $exception);

return;
}

$response = $response->withBody(Utils::streamFor($body));
}
Expand Down

0 comments on commit 0210d60

Please sign in to comment.