<commit_before><?php
//declare(strict_types=1);
namespace Morpho\Web;

use Morpho\Core\Application as BaseApplication;
use Morpho\Di\IServiceManager;
use Morpho\Error\ErrorHandler;

class Application extends BaseApplication {
    protected function createServiceManager(): IServiceManager {
        $siteManager = new SiteManager();
        $siteConfig = $siteManager->getCurrentSiteConfig();
        $services = [
            'app'         => $this,
            'siteManager' => $siteManager,
        ];
        if (isset($siteConfig['serviceManager'])) {
            $serviceManager = new $siteConfig['serviceManager']($siteConfig, $services);
        } else {
            $serviceManager = new ServiceManager($siteConfig, $services);
        }
        return $serviceManager;
    }

    protected function logFailure(\Throwable $e, IServiceManager $serviceManager = null) {
        if (null !== $serviceManager) {
            try {
                // Last chance handler.
                $serviceManager->get('errorHandler')
                    ->handleException($e);
            } catch (\Throwable $e) {
                if (ErrorHandler::isErrorLogEnabled()) {
                    error_log(addslashes((string)$e));
                }
            }
        }
        $message = null;
        if (!headers_sent()) {
            //http_response_code();
            if ($e instanceof NotFoundException) {
                header(Environment::httpProtocolVersion() . ' 404 Not Found');
                $message = "The requested page was not found";
            } elseif ($e instanceof AccessDeniedException) {
                header(Environment::httpProtocolVersion() . ' 403 Forbidden');
                $message = "You don't have access to the requested resource";
            } elseif ($e instanceof BadRequestException) {
                header(Environment::httpProtocolVersion() . ' 400 Bad Request');
                $message = "Bad request, please contact site's support";
            } else {
                header(Environment::httpProtocolVersion() . ' 500 Internal Server Error');
            }
        }
        while (@ob_end_clean());
        if ($message) {
            die($message . '.');
        }
        die("Unable to handle the request. Please contact site's support and try to return to this page again later.");
    }
}<commit_msg>Better handling of Exceptions.<commit_after><?php
//declare(strict_types=1);
namespace Morpho\Web;

use Morpho\Core\Application as BaseApplication;
use Morpho\Di\IServiceManager;
use Morpho\Error\ErrorHandler;

class Application extends BaseApplication {
    protected function createServiceManager(): IServiceManager {
        $siteManager = new SiteManager();
        $siteConfig = $siteManager->getCurrentSiteConfig();
        $services = [
            'app'         => $this,
            'siteManager' => $siteManager,
        ];
        if (isset($siteConfig['serviceManager'])) {
            $serviceManager = new $siteConfig['serviceManager']($siteConfig, $services);
        } else {
            $serviceManager = new ServiceManager($siteConfig, $services);
        }
        return $serviceManager;
    }

    protected function logFailure(\Throwable $e, IServiceManager $serviceManager = null) {
        if (null !== $serviceManager) {
            try {
                // Last chance handler.
                $serviceManager->get('errorHandler')
                    ->handleException($e);
            } catch (\Throwable $e) {
                if (ErrorHandler::isErrorLogEnabled()) {
                    error_log(addslashes((string)$e));
                }
            }
        }
        $header = null;
        if ($e instanceof NotFoundException) {
            $header = Environment::httpProtocolVersion() . ' 404 Not Found';
            $message = "The requested page was not found";
        } elseif ($e instanceof AccessDeniedException) {
            $header = Environment::httpProtocolVersion() . ' 403 Forbidden';
            $message = "You don't have access to the requested resource";
        } elseif ($e instanceof BadRequestException) {
            $header = Environment::httpProtocolVersion() . ' 400 Bad Request';
            $message = "Bad request, please contact site's support";
        } else {
            $header = Environment::httpProtocolVersion() . ' 500 Internal Server Error';
            $message = "Unable to handle the request. Please contact site's support and try to return to this page again later";
        }
        if (!headers_sent()) {
            // @TODO: Use http_response_code()?
            header($header);
        }
        while (@ob_end_clean());
        die($message . '.');
    }
}
{'fraction_non_alphanumeric': 0.11477853492333902, 'fraction_numerical': 0.005536626916524702, 'mean_word_length': 2.1105960264900663, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '5261685', 'n_tokens_mistral': 1177, 'n_tokens_neox': 1129, 'n_words': 299}