<commit_before><?php
namespace Ouzo;

class Error
{
    static public function exceptionHandler(\Exception $exception)
    {
        self::_handleError($exception->getMessage(), $exception->getTraceAsString());
    }

    static public function errorHandler($errno, $errstr, $errfile, $errline)
    {
        self::_handleError("$errstr ERRNO($errno)", "$errfile:$errline");
    }

    private static function _clearOutputBuffers()
    {
        while (ob_get_level()) {
            ob_end_clean();
        }
    }

    private static function _handleError($errorMessage, $errorTrace)
    {
        try {
            Logger::getPanelLogger()->addError($errorMessage, array($errorTrace));
            /** @noinspection PhpIncludeInspection */
            self::_clearOutputBuffers();
            header("HTTP/1.1 500 Internal Server Error");

            require_once(ViewPathResolver::resolveViewPath('exception'));
        } catch (\Exception $e) {
            echo "Framework critical error. Exception thrown in exception handler.<br>\n";
            echo "<hr>\n";
            echo "Message: " . $e->getMessage() . "<br>\n";
            echo "Trace: " . $e->getTraceAsString() . "<br>\n";
        }
    }

    static public function shutdownHandler()
    {
        $error = error_get_last();
<commit_msg>Fix to avoid infinite loop when ob_end_clean() failed.<commit_after><?php
namespace Ouzo;

class Error
{
    static public function exceptionHandler(\Exception $exception)
    {
        self::_handleError($exception->getMessage(), $exception->getTraceAsString());
    }

    static public function errorHandler($errno, $errstr, $errfile, $errline)
    {
        self::_handleError("$errstr ERRNO($errno)", "$errfile:$errline");
    }

    private static function _clearOutputBuffers()
    {
        while (ob_get_level()) {
            if (!ob_end_clean()){
                break;
            }
        }
    }

    private static function _handleError($errorMessage, $errorTrace)
    {
        try {
            Logger::getPanelLogger()->addError($errorMessage, array($errorTrace));
            /** @noinspection PhpIncludeInspection */
            self::_clearOutputBuffers();
            header("HTTP/1.1 500 Internal Server Error");

            require_once(ViewPathResolver::resolveViewPath('exception'));
        } catch (\Exception $e) {
            echo "Framework critical error. Exception thrown in exception handler.<br>\n";
            echo "<hr>\n";
            echo "Message: " . $e->getMessage() . "<br>\n";
            echo "Trace: " . $e->getTraceAsString() . "<br>\n";
        }
    }

    static public function shutdownHandler()
    {
        $error = error_get_last();

{'fraction_non_alphanumeric': 0.1363973313565604, 'fraction_numerical': 0.0037064492216456633, 'mean_word_length': 2.4961139896373057, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 21, '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': '3781996', 'n_tokens_mistral': 740, 'n_tokens_neox': 730, 'n_words': 157}