Categories
Development

Exception handling in php scrapers

Suppose we want to set only one exception handler function for all exceptions in the scraper program. This exception handler might be working for a multi-level program. Here is how it works in PHP.

At the beginning of the main scraper’s code, insert this pattern:

function exception_handler($exception)
{
 header('HTTP/1.1 500 Internal Server Error');
 mail('example@gmail.com', 'Scraper Error',
  'Scraper error: '.$exception->getMessage());
 echo “Scraper exception: ” . $exception->getMessage();
}

after that insert the handler directive to set a previously-defined exception handler function:

set_exception_handler('exception_handler');

Note that the header (‘HTTP/1.1 500 Internal Server Error’) should be set before we do any output in the scraper’s code. Otherwise the header’s collision exception will be issued with no desired output in this exception_handler function.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.