Some weeks ago I wanted to use Bugsnag (a control for software quality) for some client using Prestashop.

After some googling it seems that nobody is using Busgnag in combination with Prestashop or at least, nobody write on it.

So on this post I will briefly describe how to implement it on Prestashop 1.6.x.

With the help of the documentation we will start by adding Bugsnag 3 to our composer.json. Unfortunately, Prestashop do not used composer to handle his dependencies so we will have to do some hacks to the core of Prestashop.

composer require "bugsnag/bugsnag:^3.0"

First, we will register Bugsnag for the front end.

The root index.php file will looks like this:

require(dirname(__FILE__).'/config/config.inc.php');

if (defined('_BUGSNAG_MODE_') && _BUGSNAG_MODE_ && _BUGSNAG_ENV_ != 'development') {
    require(dirname(__FILE__).'/vendor/autoload.php');
    $bugsnag = Bugsnag\Client::make(_BUGSNAG_API_KEY_);
    $bugsnag->setAppType('Prestashop');
    $bugsnag->setAppVersion(_PS_VERSION_);
    $bugsnag->setReleaseStage(_BUGSNAG_ENV_);
    $bugsnag->setProjectRoot(dirname(__FILE__));
    $bugsnag->setErrorReportingLevel(E_ALL);

    $bugsnag->registerCallback(function ($report) {
        $customer = Context::getContext()->customer;
        $employee = Context::getContext()->employee;

        if (is_object($employee) && $employee->id) {
            $report->setUser([
                'id' => $employee->id,
                'name' => $employee->lastname.' '.$employee->firstname,
                'email' => $employee->email,
            ]);
        }

        if (is_object($customer) && $customer->id) {
            $report->setUser([
                'id' => $customer->id,
                'name' => $customer->lastname.' '.$customer->firstname,
                'email' => $customer->email,
            ]);
        }
    });

    Bugsnag\Handler::register($bugsnag);
}

Dispatcher::getInstance()->dispatch();

We will used some configuration var to easly reused the API key and to prevent reporting errors when developing.

The config/settings.inc.php file looks like this:

define('_BUGSNAG_API_KEY_', '******');
define('_BUGSNAG_MODE_', true);
define('_BUGSNAG_ENV_', 'development'); // development - preproduction - production

Then we will register bugsnag on the back end like we did for the front end on this index.php file on your admin folder just before the line:

// Prepare and trigger admin dispatcher
Dispatcher::getInstance()->dispatch();

On last thing is to handle the Prestashop catched Exception.

We will override the PrestaShopException class on the override/classes/exception/PrestaShopException.php file:

<?php

class PrestaShopException extends PrestaShopExceptionCore
{
    /**
     * Log the error on the disk
     */
    protected function logError()
    {
        parent::logError();

        if (defined('_BUGSNAG_MODE_') && _BUGSNAG_MODE_ && _BUGSNAG_ENV_ != 'development') {
            require_once(_PS_ROOT_DIR_.'/vendor/autoload.php');

            $bugsnag = Bugsnag\Client::make(_BUGSNAG_API_KEY_);

            $bugsnag->setAppType('Prestashop');
            $bugsnag->setAppVersion(_PS_VERSION_);
            $bugsnag->setReleaseStage(_BUGSNAG_ENV_);
            $bugsnag->setProjectRoot(_PS_ROOT_DIR_);
            $bugsnag->setErrorReportingLevel(E_ALL);

            $bugsnag->registerCallback(function ($report) {
                $customer = Context::getContext()->customer;
                $employee = Context::getContext()->employee;

                if (is_object($employee) && $employee->id) {
                    $report->setUser([
                        'id' => $employee->id,
                        'name' => $employee->lastname.' '.$employee->firstname,
                        'email' => $employee->email,
                    ]);
                }

                if (is_object($customer) && $customer->id) {
                    $report->setUser([
                        'id' => $customer->id,
                        'name' => $customer->lastname.' '.$customer->firstname,
                        'email' => $customer->email,
                    ]);
                }
            });

            $bugsnag->notifyException($this);
        }
    }
}

Now it should work and start reporting some issue.
You will probably need to discard some error because Prestashop are not handling correctly cache folder.

Bugsnag will help you to find some issue on your code and will increase the quality of your product but be careful, it will throw a lot of error due to the poor quality of Prestashop.