<commit_before><?php

/**
 * This driver brings support for Rollbar error handling
 *
 * @package     Nails
 * @subpackage  common
 * @category    errors
 * @author      Nails Dev Team
 * @link
 */

namespace Nails\Common\Driver\ErrorHandler;

use Nails\Common\Interfaces\ErrorHandlerDriver;
use Nails\Factory;
use Nails\Environment;
use Rollbar\Payload\Level;

class Rollbar implements ErrorHandlerDriver
{
    /**
     * Sets up the driver
     * @return void
     */
    public static function init()
    {
        $oErrorHandler = Factory::service('ErrorHandler');

        if (!defined('DEPLOY_ROLLBAR_ACCESS_TOKEN')) {

            $sSubject = 'Rollbar is not configured correctly';
            $sMessage = 'Rollbar is enabled but DEPLOY_ROLLBAR_ACCESS_TOKEN is not defined.';

            $oErrorHandler->sendDeveloperMail($sSubject, $sMessage);
            $oErrorHandler->showFatalErrorScreen($sSubject, $sMessage);
        }

        if (!class_exists('\Rollbar\Rollbar')) {

            $sSubject = 'Rollbar is not configured properly.';
            $sMessage = 'Rollbar is set as the error handler, but the Rollbar class ';
            $sMessage .= 'could not be found. Ensure that it is in composer.json.';

            $oErrorHandler->sendDeveloperMail($sSubject, $sMessage);
            $oErrorHandler->showFatalErrorScreen($sSubject, $sMessage);
        }

        $aConfig = [
            'access_token' => DEPLOY_ROLLBAR_ACCESS_TOKEN,
            'environment'  => Environment::get(),
            'person_fn'    => '\Nails\Common\Driver\ErrorHandler\Rollbar\Rollbar::getPerson',
        ];

        \Rollbar\Rollbar::init($aConfig, false, false, false);
    }

    // --------------------------------------------------------------------------

    /**
     * Called when a PHP error occurs
     *
     * @param  int    $iNumber  The error number
     * @param  string $sMessage The error message
     * @param  string $sFile    The file where the error occurred
     * @param  int    $iLine    The line number where the error occurred
     *
     * @return void
     */
    public static function error($iNumber, $sMessage, $sFile, $iLine)
    {
        //  Ignore strict errors
        if ($iNumber == E_STRICT) {
            return;
        }

        //  Send report to Rollbar
        \Rollbar\Rollbar::log(
            Level::warning(),
            $sMessage,
            [
                'error_number' => $iNumber,
                'file'         => $sFile,
                'line'         => $iLine,
            ]
        );

        //  Let this bubble to the normal Nails error handler
        Nails::error($iNumber, $sMessage, $sFile, $iLine);
    }

    // --------------------------------------------------------------------------

    /**
     * Catches uncaught exceptions
     *
     * @param  \Exception $oException The caught exception
     *
     * @return void
     */
    public static function exception($oException)
    {
        \Rollbar\Rollbar::log(Level::error(), $oException);

        $oDetails       = new \stdClass();
        $oDetails->type = get_class($oException);
        $oDetails->code = $oException->getCode();
        $oDetails->msg  = $oException->getMessage();
        $oDetails->file = $oException->getFile();
        $oDetails->line = $oException->getLine();

        $sMessage = 'Uncaught Exception with message "' . $oDetails->msg . '" and code "';
        $sMessage .= $oDetails->code . '" in ' . $oDetails->file . ' on line ' . $oDetails->line;

        //  Show we log the item?
        if (config_item('log_threshold') != 0) {
            log_message('error', $sMessage, true);
        }

        //  Show something to the user
        if (Environment::not('PRODUCTION')) {
            $sSubject = 'Uncaught Exception';<commit_msg>Using Rollbar Error Constants rather than deprecated methods<commit_after><?php

/**
 * This driver brings support for Rollbar error handling
 *
 * @package     Nails
 * @subpackage  common
 * @category    errors
 * @author      Nails Dev Team
 * @link
 */

namespace Nails\Common\Driver\ErrorHandler;

use Nails\Common\Interfaces\ErrorHandlerDriver;
use Nails\Factory;
use Nails\Environment;
use Rollbar\Payload\Level;

class Rollbar implements ErrorHandlerDriver
{
    /**
     * Sets up the driver
     * @return void
     */
    public static function init()
    {
        $oErrorHandler = Factory::service('ErrorHandler');

        if (!defined('DEPLOY_ROLLBAR_ACCESS_TOKEN')) {

            $sSubject = 'Rollbar is not configured correctly';
            $sMessage = 'Rollbar is enabled but DEPLOY_ROLLBAR_ACCESS_TOKEN is not defined.';

            $oErrorHandler->sendDeveloperMail($sSubject, $sMessage);
            $oErrorHandler->showFatalErrorScreen($sSubject, $sMessage);
        }

        if (!class_exists('\Rollbar\Rollbar')) {

            $sSubject = 'Rollbar is not configured properly.';
            $sMessage = 'Rollbar is set as the error handler, but the Rollbar class ';
            $sMessage .= 'could not be found. Ensure that it is in composer.json.';

            $oErrorHandler->sendDeveloperMail($sSubject, $sMessage);
            $oErrorHandler->showFatalErrorScreen($sSubject, $sMessage);
        }

        $aConfig = [
            'access_token' => DEPLOY_ROLLBAR_ACCESS_TOKEN,
            'environment'  => Environment::get(),
            'person_fn'    => '\Nails\Common\Driver\ErrorHandler\Rollbar\Rollbar::getPerson',
        ];

        \Rollbar\Rollbar::init($aConfig, false, false, false);
    }

    // --------------------------------------------------------------------------

    /**
     * Called when a PHP error occurs
     *
     * @param  int    $iNumber  The error number
     * @param  string $sMessage The error message
     * @param  string $sFile    The file where the error occurred
     * @param  int    $iLine    The line number where the error occurred
     *
     * @return void
     */
    public static function error($iNumber, $sMessage, $sFile, $iLine)
    {
        //  Ignore strict errors
        if ($iNumber == E_STRICT) {
            return;
        }

        //  Send report to Rollbar
        \Rollbar\Rollbar::log(
            Level::WARNING,
            $sMessage,
            [
                'error_number' => $iNumber,
                'file'         => $sFile,
                'line'         => $iLine,
            ]
        );

        //  Let this bubble to the normal Nails error handler
        Nails::error($iNumber, $sMessage, $sFile, $iLine);
    }

    // --------------------------------------------------------------------------

    /**
     * Catches uncaught exceptions
     *
     * @param  \Exception $oException The caught exception
     *
     * @return void
     */
    public static function exception($oException)
    {
        \Rollbar\Rollbar::log(Level::ERROR, $oException);

        $oDetails       = new \stdClass();
        $oDetails->type = get_class($oException);
        $oDetails->code = $oException->getCode();
        $oDetails->msg  = $oException->getMessage();
        $oDetails->file = $oException->getFile();
        $oDetails->line = $oException->getLine();

        $sMessage = 'Uncaught Exception with message "' . $oDetails->msg . '" and code "';
        $sMessage .= $oDetails->code . '" in ' . $oDetails->file . ' on line ' . $oDetails->line;

        //  Show we log the item?
        if (config_item('log_threshold') != 0) {
            log_message('error', $sMessage, true);
        }

        //  Show something to the user
        if (Environment::not('PRODUCTION')) {
            $sSubject = 'Uncaught Exception';
{'fraction_non_alphanumeric': 0.15555846536598142, 'fraction_numerical': 0.00026188293832656804, 'mean_word_length': 1.8606741573033707, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2000377', 'n_tokens_mistral': 2151, 'n_tokens_neox': 2089, 'n_words': 522}