1

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?

James
  • 656
  • 2
  • 10
  • 24
  • Can anybody offer some insight into why this is not catching? – James Jul 31 '13 at 10:25
  • i don't know if it still necessary to someone, who need the solution requested to chekc http://stackoverflow.com/questions/2468487/how-can-i-catch-a-catchable-fatal-error-on-php-type-hinting – Shahadat Hossain Khan May 21 '15 at 07:23

2 Answers2

0

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;
pguardiario
  • 53,827
  • 19
  • 119
  • 159
0

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?.

Community
  • 1
  • 1
Kyle Wiering
  • 444
  • 5
  • 15