Recently I had to debug an old website I developed in 2011, an ecommerce that runs using PrestaShop 1.4.0.17 (at the time I wrote this post, PrestaShop is in version 1.7.3.3). My client contacted me for an odd bug that occurs sometimes but not all the time. I didn’t want to spend much time on this in order not to charge him too much. So I ended up adding Sentry to this website using their free plan to track any error that could occur. Here is how I did it.
As written in the Sentry documentation I started by adding sentry/sentry
using Composer. Then I’ve added the Composer autoload and a bit of configuration in config/config.inc.php
(a file that is required everywhere in PrestaShop):
// After line 66
// Autoload
require(dirname(__FILE__).'/autoload.php');
// Vendor Autoload
require_once(dirname(__FILE__).'/../vendor/autoload.php');
// Add Sentry
Raven_Autoloader::register();
$client = new Raven_Client('your-secret-key@sentry.io/1072234');
$error_handler = new Raven_ErrorHandler($client);
$error_handler->registerExceptionHandler();
$error_handler->registerErrorHandler();
$error_handler->registerShutdownFunction();
Back in 2011, you may find many PHP websites using the autoload function which is now deprecated and doesn’t offer the same flexibility as spl_autoload_register. I didn’t find a proper way to make that autoload work with the autoload from composer. So I ended up replacing the original autoload of PrestaShop located in config/autoload.php
:
function __autoload($className)
{
// PrestaShop declares how to find and load stuff here.
}
function prestashop_autoload($className)
{
// PrestaShop declares how to find and load stuff here.
}
spl_autoload_register('prestashop_autoload');
Finally, I just changed the _PS_MODE_DEV_
located in config/defines.inc.php
from false
to true
to send all errors to Sentry.
That’s all!
David Authier
Développeur freelance