Question

I'm scraping some data from a website using QueryPath. However, every so often I receive the error message below and the script terminates.

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

The error doesn't give me any clues as to which line of my code the error is coming from, but assuming it was coming from $outHtml = htmlqp($outHtml); I tried prefixing the htmlqp command with @htmlqp.

This didn't work, so I then tried wrapping htmlqp in a catch{} statement which didn't seem to help either.

All I want to do is ignore the error and continue rather than having the script bomb out. Help!

Was it helpful?

Solution

It's a catchable fatal error .. so catch it.

If you catch it you can get a full stacktrace.

Ex:

try {
   thisfunctionthrowsanexception();
} catch (Exception $e) {
    var_dump(get_class($e));
    echo $e->getTraceAsString();
}

@ hides errors. You don't ever want to have to use that.

OTHER TIPS

I was just trying to solve the opposite problem in some production code: the type hinting is not working at all. I traced the culprit down to the following code:

set_error_handler('errorHandler');

function errorHandler($errno, $errstr, $errfile, $errline) {
    // Whole bunch of irrelevant code
    // ...

    return;
}

It handles the error... by essentially doing nothing!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top