Unable to catch a 'catchable fatal error' from QueryPath

3.2k Views Asked by At

I have a script that scrapes some old HTML. It does about 1000 pages a day, and every so often it chokes for some reason and throws up the following error:

PHP Catchable fatal error:  Argument 1 passed to DOMXPath::__construct() must be an instance of DOMDocument, null given, called in /var/scraper/autotrader/inc/QueryPath/QueryPath/CSS/DOMTraverser.php on line 417 and defined in /var/scraper/autotrader/inc/QueryPath/QueryPath/CSS/DOMTraverser.php on line 467

At first I thought it was the error was generated when htmlqp($html) was called, but I have wrapped it in a try{} statement and it didnt catch anything:

UPDATE:

I've found the offending line of code by using @ to see when the script would terminate without error. It's this line:

    try {
        $items = $html->find('.searchResultHeader')->find('.vehTitle'); //this one
    } catch (Exception $e) {
        var_dump(get_class($e));
        echo 'big dump'.$e->getTraceAsString();

    }

When it bombs out, it doesn't even echo 'big dump', so it really doesn't seem to be catching it.

I'm wondering if this is maybe a fault with QueryPath's error handling rather than my own?

2

There are 2 best solutions below

0
On

This:

$html->find('.searchResultHeader')->find('.vehTitle');

is the same as this:

$html->find('.searchResultHeader .vehTitle');

But without the risk of calling null->find();

If you really want to do it in 2 steps, use an if, not a try:

if($el = $html->find('.searchResultHeader')) $items = $el->find('.vehTitle');

Or maybe a ternary:

$items = ($el = $html->find('.searchResultHeader')) ? $el->find('.vehTitle') : null;
0
On

It is not catching because a standard try catch block will not catch errors of this type. In order to catch a 'Catchable' fatal error a Set Error Handler for the E_RECOVERABLE_ERROR is needed.

See Also: How can I catch a “catchable fatal error” on PHP type hinting?.