<commit_before><?php

namespace Phalcana\Core;

use Phalcon\Logger;
use Phalcon\Mvc\Dispatcher;
use Phalcon\DI;
use Phalcana\Phalcana;
use Phalcana\Exceptions\HTTP;
use Phalcana\Exceptions\HTTP404;
use Phalcana\Exceptions\HTTP500;
use Phalcana\Debug;

/**
 * Error handler
 *
 * @package     Phalcana
 * @category    Error
 */
class Error
{


    /**
     * @var  array  Types of errors to display at shutdown
     */
    public static $shutdownErrors = array(E_PARSE, E_ERROR, E_USER_ERROR);





    /**
     * This handler is for dealing with errors and converting any non HTTP errors to HTTP 500 errors
     *
     * @throws  Phalcon\Exceptions\HTTP500
     * @param   Exception The original error exception to be transformed.
     **/
    public function handler(\Exception $e)
    {
        try {
            if ($e instanceof HTTP == false) {
                throw new HTTP500($this->response($e), 500, 1, $e->getFile(), $e->getLine(), $e);
            }

        } catch (\Exception $n) {
            if (Phalcana\Phalcana::$mode >= Phalcana\Phalcana::PRODUCTION) {
                echo "<h1>Internal server error</h1>";
                echo "<p>Error handler failed with {$n->getMessage()} in {$n->getFile()} [{$n->getLine()}]</p>";
                echo "<p>Original error was {$e->getMessage()} in {$e->getFile()} [{$e->getLine()}]</p>";
            } else {
                echo "<h1>Internal server error</h1>";
                echo "<p>An Unexpected error has occured</p>";
            }
            die();
        }
    }

    /**
     * PHP error handler, converts all errors into ErrorExceptions. This handler
     * respects error_reporting settings.
     *
     * @throws  ErrorException
     * @return  true
     */
    public function errorHandler($code, $error, $file = null, $line = null)
    {
        if (error_reporting() & $code) {
            // This error is not suppressed by current error reporting settings
            // Convert the error into an ErrorException
            throw new \ErrorException($error, $code, 0, $file, $line);
        }

        // Do not execute the PHP error handler
        return true;
    }

    /**
     * Catches errors that are not caught by the error handler, such as E_PARSE.
     *
     * @uses Phalcon\Core\Error::handler
     */
    public function shutdown()
    {
        $error = error_get_last();

        if (in_array($error['type'], self::$shutdownErrors)) {
            // Clean the output buffer
            while (ob_get_level()) {
                ob_end_clean();
            }

            // Fake an exception for nice debugging
            $this->handler(new \ErrorException($error['message'], $error['type'], 0, $error['file'], $error['line']));

            // Shutdown now to avoid a "death loop"
            exit(1);
        }
    }




    /**
     * Generate error response page
     *
     * @param   Exception   The original error exception to be transformed.
     * @return  string      Debug page HTML
     **/
    public function response(\Exception $e)
    {

        // Get the exception information
        $class   = get_class($e);
        $code    = $e->getCode();
        $message = $e->getMessage();
        $file    = $e->getFile();
        $line    = $e->getLine();
        $trace   = $e->getTrace();
        $source = Debug::source($file, $line);


        try {



            if ($e instanceof HTTP) {

            } else {

                self::log($e);


                // Purge the response
                while (ob_get_level()) {
                    ob_end_clean();
                }


                ob_start();



                if (! headers_sent()) {
                    header('Content-Type: text/html; charset=utf-8', true, 500);
                }



                if (DI::getDefault()->get('request')->isAjax()) {
                    echo "$message <small>in $file [$line]</small>";
                } elseif (php_sapi_name() == 'cli' || $_SERVER['HTTP_USER_AGENT'] == 'cURL') {
                    echo self::text($e);
                    echo PHP_EOL;
                } else {
                    include Phalcana\Phalcana::$di->get('fs')->findFile('views', 'error', 'html');

                }



                return ob_get_clean();

            }
<commit_msg>fix for namespace referencing in error handler<commit_after><?php

namespace Phalcana\Core;

use Phalcon\Logger;
use Phalcon\Mvc\Dispatcher;
use Phalcon\DI;
use Phalcana\Phalcana;
use Phalcana\Exceptions\HTTP;
use Phalcana\Exceptions\HTTP404;
use Phalcana\Exceptions\HTTP500;
use Phalcana\Debug;

/**
 * Error handler
 *
 * @package     Phalcana
 * @category    Error
 */
class Error
{


    /**
     * @var  array  Types of errors to display at shutdown
     */
    public static $shutdownErrors = array(E_PARSE, E_ERROR, E_USER_ERROR);





    /**
     * This handler is for dealing with errors and converting any non HTTP errors to HTTP 500 errors
     *
     * @throws  Phalcon\Exceptions\HTTP500
     * @param   Exception The original error exception to be transformed.
     **/
    public function handler(\Exception $e)
    {
        try {
            if ($e instanceof HTTP == false) {
                throw new HTTP500($this->response($e), 500, 1, $e->getFile(), $e->getLine(), $e);
            }

        } catch (\Exception $n) {
            if (Phalcana::$mode >= Phalcana::PRODUCTION) {
                echo "<h1>Internal server error</h1>";
                echo "<p>Error handler failed with {$n->getMessage()} in {$n->getFile()} [{$n->getLine()}]</p>";
                echo "<p>Original error was {$e->getMessage()} in {$e->getFile()} [{$e->getLine()}]</p>";
            } else {
                echo "<h1>Internal server error</h1>";
                echo "<p>An Unexpected error has occured</p>";
            }
            die();
        }
    }

    /**
     * PHP error handler, converts all errors into ErrorExceptions. This handler
     * respects error_reporting settings.
     *
     * @throws  ErrorException
     * @return  true
     */
    public function errorHandler($code, $error, $file = null, $line = null)
    {
        if (error_reporting() & $code) {
            // This error is not suppressed by current error reporting settings
            // Convert the error into an ErrorException
            throw new \ErrorException($error, $code, 0, $file, $line);
        }

        // Do not execute the PHP error handler
        return true;
    }

    /**
     * Catches errors that are not caught by the error handler, such as E_PARSE.
     *
     * @uses Phalcon\Core\Error::handler
     */
    public function shutdown()
    {
        $error = error_get_last();

        if (in_array($error['type'], self::$shutdownErrors)) {
            // Clean the output buffer
            while (ob_get_level()) {
                ob_end_clean();
            }

            // Fake an exception for nice debugging
            $this->handler(new \ErrorException($error['message'], $error['type'], 0, $error['file'], $error['line']));

            // Shutdown now to avoid a "death loop"
            exit(1);
        }
    }




    /**
     * Generate error response page
     *
     * @param   Exception   The original error exception to be transformed.
     * @return  string      Debug page HTML
     **/
    public function response(\Exception $e)
    {

        // Get the exception information
        $class   = get_class($e);
        $code    = $e->getCode();
        $message = $e->getMessage();
        $file    = $e->getFile();
        $line    = $e->getLine();
        $trace   = $e->getTrace();
        $source = Debug::source($file, $line);


        try {



            if ($e instanceof HTTP) {

            } else {

                self::log($e);


                // Purge the response
                while (ob_get_level()) {
                    ob_end_clean();
                }


                ob_start();



                if (! headers_sent()) {
                    header('Content-Type: text/html; charset=utf-8', true, 500);
                }



                if (DI::getDefault()->get('request')->isAjax()) {
                    echo "$message <small>in $file [$line]</small>";
                } elseif (php_sapi_name() == 'cli' || $_SERVER['HTTP_USER_AGENT'] == 'cURL') {
                    echo self::text($e);
                    echo PHP_EOL;
                } else {
                    include Phalcana::$di->get('fs')->findFile('views', 'error', 'html');

                }



                return ob_get_clean();

            }

{'fraction_non_alphanumeric': 0.13394730706458383, 'fraction_numerical': 0.006994637444625787, 'mean_word_length': 1.721763959390863, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7112071', 'n_tokens_mistral': 2565, 'n_tokens_neox': 2368, 'n_words': 650}