Skip to content

Error handling ​

meta-scraper throws in two situations only: a page cannot be downloaded, or a parser cannot work in this PHP. Parsing itself never throws.

ExceptionExtendsThrown byWhen
Tomaj\Scraper\Exception\FetchExceptionRuntimeExceptionScraper::parseUrl()the request failed
Tomaj\Scraper\Exception\MissingExtensionExceptionLogicExceptionnew OgDomParser()ext-dom or ext-libxml is not loaded
Tomaj\Scraper\Exception\ScraperExceptioninterface (Throwable)implemented by both, to catch everything from the package

FetchException ​

php
final class FetchException extends \RuntimeException implements ScraperException
{
    public readonly string $url;                 // the URL you passed to parseUrl()
    // getPrevious(): the Guzzle exception
    // getMessage(): 'Cannot fetch "<url>": <Guzzle message>'
}

Every GuzzleHttp\Exception\GuzzleException from the request is wrapped, so these all become a FetchException:

CausePrevious exception (Guzzle 7)Previous exception (Guzzle 8)
4xx response (with http_errors on, the default)ClientExceptionClientException
5xx responseServerExceptionServerException
more redirects than allow_redirects.maxTooManyRedirectsExceptionTooManyRedirectsException
connection refused, DNS failureConnectExceptionConnectException
timeout reachedConnectExceptionNetworkTimeoutException
TLS errors, invalid URLs, other transfer errorsanother GuzzleExceptionanother GuzzleException

The exact Guzzle classes differ between Guzzle 7 and 8. To react to an HTTP status, check for BadResponseException, which both versions have:

php
use GuzzleHttp\Exception\BadResponseException;

$scraper = new Scraper(['timeout' => 5]);
$missing = str_replace('article.html', 'missing.html', $url);

try {
    $meta = $scraper->parseUrl($missing, $parsers);
} catch (FetchException $e) {
    $previous = $e->getPrevious();
    if ($previous instanceof BadResponseException) {
        echo 'HTTP ', $previous->getResponse()->getStatusCode(), ' for ', basename($e->url), "\n";
    } else {
        echo 'Network error for ', $e->url, "\n";
    }
}
text
HTTP 404 for missing.html

The fetch errors example triggers every cause against a local server and runs the same on Guzzle 7 and 8.

After a FetchException, getBody() still returns the body of the previous successful call.

Exceptions you throw in your own client ​

FetchException wraps only Guzzle exceptions. An exception thrown by your own middleware or by an on_redirect callback on your own client (not a GuzzleException) is not wrapped and leaves parseUrl() as it is. The link preview example relies on this: its BlockedUrlException from on_redirect reaches the caller unchanged.

MissingExtensionException ​

new OgDomParser() checks for ext-dom and ext-libxml and throws when one is missing, with a message such as Tomaj\Scraper\Parser\OgDomParser requires the PHP extension "dom". It is a LogicException: fix the environment, or fall back to OgParser:

php
try {
    $metaTags = new OgDomParser();
} catch (MissingExtensionException) {
    $metaTags = new OgParser();
}
echo $metaTags::class, "\n";
text
Tomaj\Scraper\Parser\OgDomParser

Catching everything ​

Both exceptions implement ScraperException:

php
try {
    $meta = new Scraper(['timeout' => 5])->parseUrl('http://127.0.0.1:9/', [new OgDomParser()]);
} catch (ScraperException $e) {
    echo $e::class, "\n";
}
text
Tomaj\Scraper\Exception\FetchException

Bad HTML does not throw ​

The parsers return what they could read and leave the rest null:

InputResult
'' (empty string)an empty Meta from every parser
unclosed tags, upper-case tags, unquoted attributesOgDomParser reads them; OgParser may miss them
invalid JSON in a JSON-LD blockthe block is skipped, other blocks are used
a JSON-LD value of an unexpected typeignored (null)
a date that is not absolute (not a date, yesterday noon, 2026-02-30)ignored (null)
a page in another charsetOgDomParser decodes a declared charset, the other parsers return the raw bytes
php
$meta = $scraper->parse('<TITLE>Broken page</TITLE><div><p>unclosed<script type="application/ld+json">{oops</script>', $parsers);
var_dump($meta->getTitle(), $meta->getAuthors());
text
string(11) "Broken page"
array(0) {
}

Check the fields you need and fall back to your own defaults: an empty title is a normal result, not an error. The broken pages example shows the parsers on real broken pages.

Released under the MIT License.