[ Skip to main content ]
← Back to blog

Integrating Raygun into Magento

 |  Infrastructure + stack Web + software development

Integrating Raygun.io into Magento is about as straightforward as any other PHP project (using Raygun4PHP, but there are a few gotcha’s to be aware of.

First of all, as the Raygun4PHP page states, make sure the curl binary is installed on the server (the php-curl extension is not enough, though it is also mentioned).

Next you’ll want to clone the raygun4php repository somewhere, once this is done copy the contents of the src directory (that is the Raygun4php folder) into app/code/community/.

Now we will insert the first part of our error handling.

Inside index.php, just before the last line (Mage::run($mageRunCode, $mageRunType);), insert the following code:

<?php // ignore this line

/**
 * Error tracking via Raygun
 */
require (MAGENTO_ROOT . '/app/code/community/Raygun4php/RaygunClient.php');

$client = new \Raygun4php\RaygunClient('replace me with your api key', true); // Use true for async

function error_handler($errno, $errstr, $errfile, $errline ) {
    global $client;
    $client->SendError($errno, $errstr, $errfile, $errline);
}

function exception_handler($exception)
{
    global $client;
    $client->SendException($exception);
}

function shutdown_handler()
{
   global $client;

   $err = error_get_last();

   if (isset($err['type']) && $err['type'] !== E_WARNING) {
      $client->SendError($err['type'], $err['message'], $err['file'], $err['line']);
   }
}

set_exception_handler('exception_handler');
set_error_handler('error_handler');
register_shutdown_function('shutdown_handler');

This is very similar to the example code, with the added addition of a shutdown handler. With a shutdown handler you can also catch parse errors and other fatal errors. Here we have disabled E_WARNING due to a caveat that the PHP error_get_last() function returns info about suppressed errors, and during testing we found that Magento was suppressing a warning that came up in every request.

So this will cover a few errors, but Magento runs the bulk of its code within a try/catch statement inside app/Mage.php. We’ll modify that file next to also submit to Raygun.

Inside app/Mage.php find the run method and look for the line if (self::isInstalled() || self::$_isDownloader) {, just before this line place the following:

<?php // ignore this line

/**
 * Error tracking via Raygun
 */
$client = new \Raygun4php\RaygunClient('replace me with your api key', true);
$client->SendException($e);

To test the error handler, open one of the template files under app/design and try adding <?php : ?> somewhere (to test the fatal handler), or <?php throw new Exception('test'); ?>.

With this in place you are ready to commit your code and start sending errors to Raygun!


Share this post:

We'd love to hear from you

Technology enables us to do some awesome things. We love how a simple greeting or question can turn into something amazing.

Contact us