Couple of weeks ago I noticed PDO exceptions in my Habari logs, Google's crawler was most often the originator and I couldn't read the full message since Habari's error handling system is still very basic.
After checking the Webmaster Tools on my Google Account, I noticed their crawler kept requesting high-numbered pages on the Atom feed. Found out the 'last-page' parameter was calculated with the site's pagination option rather the Atom feed's. Reported: Ticket #1112.
Couple days later, I still had the PDO exceptions in my Habari logs. Annoyed, I asked on the IRC channel for a plugin to view log entry's backtraces, thankfully mikelietz pointed out the Extended Log plugin. At last, I could trace back the PDO exceptions to 404 errors.
As you can see below, Habari first checks if the current theme has a 404 template. If it exists, Habari will change the request to a 404 and send the header accordingly. If it does not exist, it will compose its own using your header and footer. This post continues after the code.
elseif( ( $posts === false ) ||
( isset( $where_filters['page'] ) && $where_filters['page'] > 1 && count( $posts ) == 0 ) ) {
if ( $this->template_exists( '404' ) ) {
$fallback = array( '404' );
// Replace template variables with the 404 rewrite rule
$this->request->{URL::get_matched_rule()->name} = false;
$this->request->{URL::set_404()->name} = true;
$this->matched_rule = URL::get_matched_rule();
// 404 status header sent in act_display_404, but we're past
// that, so send it now.
header( 'HTTP/1.1 404 Not Found' );
}
else {
$this->display( 'header' );
echo '<h2>';
_e( "Whoops! 404. The page you were trying to access is not really there. Please try again." );
echo '</h2>';
header( 'HTTP/1.1 404 Not Found' );
$this->display( 'footer' );
die;
}
}
system/theme.php:215-237
On most websites, this process is probably fine, but my header uses the Post object or other variables. That is what was causing the PDO exceptions. After I've created a 404 template and removed the erroneous code, all is fine in Habari land! Let this be a warning to y'all who push the envelope!
Makes you wonder how other content management systems handle 404 errors. Challenge accepted.